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

JS array.copy() being compiled to null() #3967

Closed
andyli opened this issue Mar 4, 2015 · 13 comments
Closed

JS array.copy() being compiled to null() #3967

andyli opened this issue Mar 4, 2015 · 13 comments
Assignees
Milestone

Comments

@andyli
Copy link
Member

@andyli andyli commented Mar 4, 2015

class Test {
    static function main():Void {
        var a = ["a", "b"];
        var b = a.copy();
    }
}

compile with:

-main Test
-js Test.js

output:

(function (console) { "use strict";
var Test = function() { };
Test.main = function() {
    var a_0 = "a";
    var a_1 = "b";
    var b = null();
};
Test.main();
})(typeof console != "undefined" ? console : {log:function(){}});
@nadako nadako added this to the 3.2 milestone Mar 4, 2015
@Simn
Copy link
Member

@Simn Simn commented Mar 4, 2015

IMO this should be fixed in Array.copy by using untyped __js__("{0}.slice()", this). Doing (untyped this).slice() is quite bad because it generates an anonymous structure field access. The inliner doesn't cancel here because the field of that access defaults to Var instead of Method.

@ncannasse
Copy link
Member

@ncannasse ncannasse commented Mar 5, 2015

I think that's a huge problem if the analyzer breaks on "untyped this"

@ncannasse
Copy link
Member

@ncannasse ncannasse commented Mar 5, 2015

Ah, reading again I understand a bit better.

@Simn
Copy link
Member

@Simn Simn commented Mar 5, 2015

It's not the analyzer, it's the constructor inlining logic.

@ncannasse
Copy link
Member

@ncannasse ncannasse commented Mar 5, 2015

Actually I don't understand how it breaks exactly, but I think we should find a way to fix it without changing Array.copy implementation since users might have similar cases in their code

@Simn
Copy link
Member

@Simn Simn commented Mar 5, 2015

I have clarified this somewhere before but I can do it again: The problem with (untyped this).field is that (untyped this) is typed as a monomorph and then turned into an open structure upon field access.

[insert long train of thought why we may want to consider disallowing field access on unknown types]

The problem is related to this match in inline_constructors in some way: | TField(e1, (FInstance(_, _, {cf_kind = Var _; cf_name = s}) | FAnon({cf_kind = Var _; cf_name = s}))) ->. On sanely typed code this would always cancel because the cf_kind would be Method, but here due to the open structure thing the field access is Var and the inliner thinks that we are just accessing some field on some structure.

@Simn
Copy link
Member

@Simn Simn commented Mar 5, 2015

... plus the fact that you originally designed inlined constructors to default to null upon unknown field access.

@ncannasse
Copy link
Member

@ncannasse ncannasse commented Mar 5, 2015

But then you can get the same result by doing (perfectly typed):

var _this : { var method(default,never) : Void -> Void; } = this;
_this.method();
@ncannasse ncannasse mentioned this issue Mar 5, 2015
@Simn
Copy link
Member

@Simn Simn commented Mar 5, 2015

Yes, that's probably a problem with the constructor inlining implementation since the get go. So we indeed have to do something about that (though I still think the untyped this code is bad).

@ncannasse
Copy link
Member

@ncannasse ncannasse commented Mar 5, 2015

class Test {
    static function main():Void {
        var a = ["a", "b"];
        (untyped a).slice();        
    }
}

SInce a is used as a value, we should cancel constructor inlining here.

Simn added a commit that referenced this issue Mar 5, 2015
@Simn Simn self-assigned this Mar 5, 2015
@Simn
Copy link
Member

@Simn Simn commented Mar 5, 2015

I have fixed the original issue, but the sub-typing one remains for now.

Simn added a commit that referenced this issue Mar 8, 2015
@Simn Simn modified the milestones: 3.3, 3.2 Mar 8, 2015
@Simn
Copy link
Member

@Simn Simn commented Mar 8, 2015

We sidestep the issue now by not inlining constructors if the variable type is different. This is good enough for 3.2, but we can improve the distinction of constructor/structure inlines after that.

@Simn
Copy link
Member

@Simn Simn commented Feb 23, 2016

I looked at this again and think that my type-based fix does exactly the right thing. If we do new C() but assign it to a variable that's typed differently canceling inlining is the right thing to do. I cannot reproduce any other problem, so I'll consider this closed.

@Simn Simn closed this Feb 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.