Sdk: Null-coalescing operator with a "throw" expression messes with dart2js ineterceptors

Created on 2 Jun 2017  路  2Comments  路  Source: dart-lang/sdk

Reduced test case (see on Dartpad: https://dartpad.dartlang.org/35b1478eec218cf4275a588ff7e679c8)

main() {
  var mayBeNull = new DateTime.now().millisecondsSinceEpoch != 1 ? 'foo bar baz' : null;
  var nullCoalescedString = mayBeNull ?? (throw new Error());
  print(nullCoalescedString.runtimeType); // prints 'String'
  print(nullCoalescedString.indexOf('bar')); // prints `null` unexpectedly
}

Replacing (throw new Error()) with other expressions results in correct behavior, with the indexOf line printing 4 instead of null. Expressions I tried:

  • a number literal (this is a strong mode error, so it's not completely relevant)
  • a call to a void function (this is a strong mode error, so it's not completely relevant)
  • a string literal
  • null
P2 type-bug web-dart2js

Most helpful comment

On DartPad today the second example prints 4 instead of null so it looks like this was fixed.

All 2 comments

Interestingly enough, it also happens in this simplified case... (thanks @georgelesica-wf)

main() {
  var nullCoalescedString = 'foo bar baz' ?? (throw new Error());
  print(nullCoalescedString.runtimeType); // prints 'String'
  print(nullCoalescedString.indexOf('bar')); // prints `null` unexpectedly
}

On DartPad today the second example prints 4 instead of null so it looks like this was fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DartBot picture DartBot  路  3Comments

Hixie picture Hixie  路  3Comments

xster picture xster  路  3Comments

DartBot picture DartBot  路  3Comments

sgrekhov picture sgrekhov  路  3Comments