dart2js wrongly optimize away null check on field when using forEach() early in constructor

78 views
Skip to first unread message

Martin Nordholts

unread,
Sep 7, 2014, 6:54:54 PM9/7/14
to compil...@dartlang.org
Hi,

I think I've found a bug in dart2js. Consider this code. I have simplified it as much as I can:

// main.dart

class SomeClass {
 
Object someField;

 
SomeClass() {
   
[1].forEach((o) => someMethod());
    someField
= new Object();
 
}

 
void someMethod() {
   
if (someField != null) {
     
print("FAIL");
   
} else {
     
print("PASS");
   
}
 
}
}

void main() {
 
new SomeClass();
}


If you run it with dart, you get the expected "PASS" output:

$ dart main.dart
PASS

However, if you run the generated dart2js output in a browser:

dart2js main.dart
Dart file (main.dart) compiled to JavaScript: out.js
cat '<script src="out.js"></script>' > main.html
$ # Open main.html in e.g. Google Chrome and watch the JavaScript console => it says FAIL

You get the "FAIL" output. Surely it is a bug when dart and dart2js produces different output? When looking at the generated JavaScript, it seems as if dart2js has optimized away the null check for some reason. Interestingly, if you replace this line:

[1].forEach((o) => someMethod());


with this line:

someMethod();

You get "PASS" in both cases.

I'm running Dart 1.6.0 on Mac OS X 10.9.4:

$ dart --version
Dart VM version: 1.6.0 (Tue Aug 26 22:30:33 2014) on "macos_x64"
$ dart2js --version
Dart-to-JavaScript compiler (dart2js) version: 1.6.0

Are you aware of this bug?

BR,
Martin


Karl Klose

unread,
Sep 8, 2014, 2:58:28 AM9/8/14
to Martin Nordholts, compil...@dartlang.org
Thank you for the bug report, this is indeed a bug in the compiler!

I have filed a bug at dartbug.com/20840 to keep track of it.


To unsubscribe from this group and stop receiving emails from it, send an email to compiler-dev...@dartlang.org.

Stephan Herhut

unread,
Sep 9, 2014, 10:59:27 AM9/9/14
to Karl Klose, Martin Nordholts, compil...@dartlang.org
This is now fixed on bleeding_edge, revision 40028.

Stephan
Reply all
Reply to author
Forward
0 new messages