haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Std.int in abstract

Open kevinresol opened this issue 9 years ago • 9 comments

package;

class Main
{
    static function main() 
    {
        trace((1.5555:R));
    }
}

abstract R(Float) to Float {

    private inline function new(v:Float) 
        this = Std.int(v);

    @:from
    public static inline function fromFloat(v:Float):R
        return new R(v);
}

1 is expected, but got 1.5555 on java & cs @ 7230d00

This is regression, it worked in 3.2.1

kevinresol avatar Apr 16 '16 03:04 kevinresol

@waneck: I think this one is yours because the dump looks fine to me:

            [Call:Void]
                [Field:v : Dynamic -> ?infos : Null<haxe.PosInfos> -> Void]
                    [TypeExpr haxe.Log:Class<haxe.Log>]
                    [FStatic:v : Dynamic -> ?infos : Null<haxe.PosInfos> -> Void]
                        haxe.Log
                        trace
                [Cast:R]
                    [Meta:R]
                        :implicitCast
                        [Cast:R] [Const:Float] 1.5555
                [ObjectDecl:{ methodName : String, lineNumber : Int, fileName : String, className : String }]
                    fileName: [Const:String] "Main.hx"
                    lineNumber: [Const:Int] 7
                    className: [Const:String] "Main"
                    methodName: [Const:String] "main"

Generated Java:

        haxe.Log.trace.__hx_invoke2_o(((double) (1.5555) ), haxe.lang.Runtime.undefined, 0.0, new haxe.lang.DynamicObject(new java.lang.String[]{"className", "fileName", "methodName"}, new java.lang.Object[]{"Main", "Main.hx", "main"}, new java.lang.String[]{"lineNumber"}, new double[]{((double) (((double) (7) )) )}));

This requires attention for the RC, I can't make a release with this being broken...

Simn avatar Apr 24 '16 07:04 Simn

as talked with @Simn, the problem is with the analyzer/inliner which changes the cast to R instead of Int. However, I've added a workaround which fixes the issue on java/c# anyway. Performance should be the same, since they are transformed into Std.int later

waneck avatar May 26 '16 18:05 waneck

The good news is that this is not a general inlining problem and indeed comes from the analyzer, namely const propagation. The bad news is that I get some failing tests in other places after fixing it. Gonna have to investigate this further.

Simn avatar Jun 02 '16 11:06 Simn

This is too hard for me to get right for 3.3 and since the regression has been addressed I'll move it to 3.4.

Simn avatar Jun 17 '16 07:06 Simn

package;

class Main
{
	static function main() {
		run((1.5555:R));
	}

	static public inline function int(v:Float):Int {
		return cast v;
	}

	static dynamic function run(t:Dynamic) {
		trace(t);
	}
}

abstract R(Float) to Float {

	private inline function new(v:Float)
		this = Main.int(v);

	@:from
	public static inline function fromFloat(v:Float):R
		return new R(v);
}

This works without analyzer. Analyzer removes a cast to int. Without analyzer:

[Block:Void]
	[Var this1(3610):Float] [Cast:Int] [Const:Float] 1.5555 // here is the cast
	[Call:Void]
		[Field:(t : Dynamic) -> Void]
			[TypeExpr Main:Class<Main>]
			[FStatic:(t : Dynamic) -> Void]
				Main
				run:(t : Dynamic) -> Void
		[Meta:R]
			:implicitCast
			[Cast:R] [Local this1(3610):Float:Float]

With analyzer

[Block:Void]
	[Call:Void]
		[Field:(t : Dynamic) -> Void]
			[TypeExpr Main:Class<Main>]
			[FStatic:(t : Dynamic) -> Void]
				Main
				run:(t : Dynamic) -> Void
		[Meta:R]
			:implicitCast
			[Cast:R] [Const:Float] 1.5555 // it's gone

RealyUniqueName avatar Sep 03 '19 16:09 RealyUniqueName

Actually it's broken for all static targets (cpp & hl too)

RealyUniqueName avatar Nov 23 '19 17:11 RealyUniqueName

Working fine in 4.1.2

kevinresol avatar Jul 04 '20 14:07 kevinresol

I can still reproduce it using this sample with Haxe 4.1.2 and the latest dev Haxe

RealyUniqueName avatar Jul 06 '20 09:07 RealyUniqueName

Problem only for static targets when using cast instead of Std.int, i updated unit test: Issue5124.txt

RblSb avatar May 25 '25 14:05 RblSb