HaxeFoundation / HaxeFoundation/haxe

invalid code generated when specifying broader type for an override method argument

Open
#6,325 14 comments 0 reactions 2 assignees Claimed by @hughsando View on GitHub
bug
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
class Entity {}
class Building extends Entity {}

class Unit extends Entity {
function shoot(target:Unit) {}
}

class HeavyUnit extends Unit {
override function shoot(target:Entity) {
trace(target);
}

function shootBuilding(building:Building) {
shoot(building);
}
}
```

this generates wrong code for the HeavyUnit methods that only fails at run-time:

```cs
public class HeavyUnit : global::Unit {

public override void shoot(global::Unit __temp_target1) {
global::Entity target = ((global::Entity) (__temp_target1) );
global::haxe.Log.trace.__hx_invoke2_o(default(double), target, default(double), new global::haxe.lang.DynamicObject(new int[]{302979532, 1547539107, 1648581351}, new object[]{"shoot", "HeavyUnit", "Main.hx"}, new int[]{1981972957}, new double[]{((double) (10) )}));
}


public virtual void shootBuilding(global::Building building) {
this.shoot(((global::Unit) (((object) (building) )) ));
}
}
```

I guess we could generate an overload for the `shoot` that takes `Entity`, override the `shoot` that takes `Unit` and calls the `Entity` one...

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.