HaxeFoundation / HaxeFoundation/haxe

dependent on order of @:from function Haxe generates wrong code

Open
#4,711 8 comments 0 reactions 1 assignee Claimed by @Simn View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

the following Haxe code generates wrong code (tested in JS and neko with 3.2++) depending on the position of the fromInt function. The assignment from an IDispatch to Bug takes the fromDate function, which is totally wrong.
http://try.haxe.org/#68925

``` haxe
abstract Bug(Float) from Float to Float
{
#if false
public inline function new(v: Float) this = v;
@:to public inline function toInt(): Int { return Math.floor(this); }
@:to public function toDate(): Date {
return new Date(Std.int(this), 1, 1, 0, 0, 0);
}

@:from static public inline function fromInt(v: Int): Bug return new Bug(v);
// if fromInt comes before fromDate the code is correct, but
// the return value has to be casted - return v; does not compile

@:from static public function fromDate(d: Date): Bug return d.getFullYear();

#else
public inline function new(v: Float) {
this = v;
}

@:to public inline function toInt(): Int { return Math.floor(this); }

@:to public function toDate(): Date {
return new Date(Std.int(this), 1, 1, 0, 0, 0);
}

@:from public static function fromDate(d: Date): Bug {
return d != null ? d.getFullYear() : 0.0;
}

@:from public static inline function fromInt(v: Int): Bug return v;
// fromInt comes after fromDate - now the wrong code is generated

#end
}

abstract IDispatch(Dynamic) to(Dynamic) {
public inline function new(v) this = v;
}

class Test {
static function main() {
var disp = new IDispatch(123);
var dyn: Dynamic = 123;
var t = new Bug(123);
trace(t);
t = disp; // with an abstract wrapping a Dynamic not !
t = dyn; // with a Dynamic the code is OK
trace(t);
}
}
```

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.