dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

dart2js: rti assertion triggers using C.JSInt_methods before it is defined #41890

Closed sigmundch closed 4 years ago

sigmundch commented 4 years ago

Consider this example:

class A {}
class B {}
typedef _F = String Function(A, {B myname});
main() {
  String f1(A a, {B myname}) => "$a $myname";
  print(f1 is _F);
}

If you compile it via dart2js with --enable-asserts, it produces this error:

/tmp/out.js:2000: TypeError: Cannot read property '$mod' of undefined
      H.assertHelper(C.JSInt_methods.$mod($length, 3) === 0);
                                     ^
TypeError: Cannot read property '$mod' of undefined
    at Object._Parser_toTypesNamed (/tmp/out.js:2000:38)
    at Object._Parser_parse (/tmp/out.js:1908:17)
    at Object._Universe_eval (/tmp/out.js:1453:15)
    at findType (/tmp/out.js:840:16)
    at rtii (/tmp/out.js:2888:70)
    at dartProgram (/tmp/out.js:2893:5)
    at /tmp/out.js:2940:3

@rakudrama @fishythefish

sigmundch commented 4 years ago

Note this is only reproducible after the unfork. The code that triggers this is this assertion:

assert(length % 3 == 0);

pre-nnbd, we generate code like: H.assertHelper(($length & 1) === 0);, while post-nnbd, we instead generate: H.assertHelper(C.JSInt_methods.$mod($length, 3) === 0);

rakudrama commented 4 years ago

The code to construct types is used before constants are created (since constants refer to types). We will have to take care to ensure that code does not directly or indirectly use constants.

The old code worked only because int.isEven was inlined. If inlining is disabled, it would have failed. There is a similar issue, but requires -O0: https://github.com/dart-lang/sdk/issues/41484

rakudrama commented 4 years ago

Will be fixed by: https://dart-review.googlesource.com/c/sdk/+/148006

rakudrama commented 4 years ago

Fixed by e0257265d34efffc845ea88643e183dc3cc5bb0e