Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with inlining iterators #8240

Closed
Aurel300 opened this issue May 1, 2019 · 3 comments
Closed

Issues with inlining iterators #8240

Aurel300 opened this issue May 1, 2019 · 3 comments
Assignees
Labels
platform-eval Everything related to the Haxe 4 eval macro interpreter platform-java Everything related to Java
Milestone

Comments

@Aurel300
Copy link
Member

Aurel300 commented May 1, 2019

import haxe.macro.Expr;

class Suite {
  public function new() {}
  macro public static function foo(expr:Expr) {
    return macro var f = function() $expr;
  }
}

class InlineLoop {
  public static function main():Void {
    var suite = new Suite();
    Suite.foo(for (c in inline new haxe.iterators.StringIteratorUnicode("")) {});
  }
}

This breaks during compile-time on Java and eval. Note:

  • removing the new Suite() line prevents the error
  • removing the inline also prevents the error

On Java, I get a couple of errors like:

src/haxe/root/InlineLoop_main_6__Fun.java:21: error: cannot find symbol
		_g_offset = 0;
		^
  symbol:   variable _g_offset
  location: class InlineLoop_main_6__Fun

On eval I get:

InlineLoop.hx:13: characters 25-76 : Uncaught exception Unbound variable

On Python there is a duplicate nonlocal, but this is probably fine.


On a similar note, but only causing a warning:

import haxe.macro.Expr;

class Suite {
  public function new() {}
}

class InlineLoop2 {
  public static function main():Void {
    var suite = new Suite();
    for (c in inline new haxe.iterators.StringIteratorUnicode("")) {}
  }
}

With the new Suite() line I get:

InlineLoop2.hx:10: characters 15-66 : Warning : Type String is being cast to the unrelated type Suite

Without it it compiles fine.

Edit: the Java errors seem to also happen on C#.

@Aurel300 Aurel300 added platform-eval Everything related to the Haxe 4 eval macro interpreter platform-java Everything related to Java labels May 1, 2019
@Simn Simn self-assigned this May 17, 2019
@Simn Simn added this to the Release 4.0 milestone May 17, 2019
@Simn
Copy link
Member

Simn commented May 17, 2019

Something is really fucked up here. Let's at least lose the macro:

class Suite {
	public function new() {}
}

class Main {
	public static function main() {
		var suite = new Suite();
		var f = function() (for (c in inline new haxe.iterators.StringIteratorUnicode("")) {});
	}
}

This is what arrives at inline_constructors:

function() {
        var suite = new Suite();
        var f = function() ({
                var ` = @:inline new haxe.iterators.StringIteratorUnicode("");
                while (`.offset < `.s.length) {
                        var c = {
                                var c = StringTools.fastCodeAt(`.s, `.offset ++);
                                c;
                        };
                };
        });
}

And it is transformed to this:

function() {
        var suite = {
                var `_s;
                var `_offset;
                `_offset = 0;
                `_s = "";
        };
        var f = function() ({
                `_offset = 0;
                `_s = "";
                while (`_offset < `_s.length) {
                        var c = {
                                var c = StringTools.fastCodeAt(`_s, `_offset ++);
                                c;
                        };
                };
        });
}

For some reason, the new Suite() ends up being inlined to the constructor of StringIterator...

This is probably my fault for misunderstanding some logic in inline_constructors, but I would appreciate some advice from @basro regardless.

@Simn
Copy link
Member

Simn commented May 17, 2019

This can be further reduced:

class Suite {
	public function new() {}
}

class InlineSuite {
	var a = "foo";
	public inline function new() { }
}

class Main {
	public static function main() {
		var suite = new Suite();
		var inlineSuite = inline new InlineSuite();
	}
}

Before inline_constructors:

function() {
        var suite = new Suite();
        var inlineSuite = @:inline new InlineSuite();
}

After:

function() {
        var suite = {
                var inlineSuite_a;
                inlineSuite_a = "foo";
        };
        inlineSuite_a = "foo";
}

@Simn Simn closed this as completed in 84711c8 May 17, 2019
@andyli
Copy link
Member

andyli commented May 18, 2019

Reopen since the cpp target failed at the test.

@andyli andyli reopened this May 18, 2019
@Simn Simn closed this as completed in 9e96990 May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-eval Everything related to the Haxe 4 eval macro interpreter platform-java Everything related to Java
Projects
None yet
Development

No branches or pull requests

3 participants