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

[dart2js] List.unmodifiable fails to run on web #59790

Open
sunarya-thito opened this issue Dec 21, 2024 · 1 comment
Open

[dart2js] List.unmodifiable fails to run on web #59790

sunarya-thito opened this issue Dec 21, 2024 · 1 comment
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-ssa type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@sunarya-thito
Copy link

Steps to Reproduce

  1. Add this code
for (int i = 0; i < List.unmodifiable([]).length; i++) {}

for example

void main() {
  for (int i = 0; i < List.unmodifiable([]).length; i++) {}
  runApp(const MyApp());
}
  1. Run it with flutter web (on profile or release mode)
flutter run -d chrome --release

or you can also compile it with dart2js, which generate the js code below

    main() {
      var t1, i, result;
      result.$flags = 3;
      for (t1 = type$.dynamic, i = 0; result = A.List_List$from([], false, t1), i < result.length; ++i)
        ;
    }
  1. You will get this error
Uncaught Error: Error: Cannot set properties of undefined (setting '$flags')
    at main.dart:114:65
    at _wrapJsFunctionForAsync_closure.$protected (async_patch.dart:311:19)
    at _wrapJsFunctionForAsync_closure.call$2 (async_patch.dart:336:23)
    at _awaitOnObject_closure.call$1 (async_patch.dart:287:19)
    at _RootZone.runUnary$2$2 (zone.dart:1676:46)
    at _RootZone.runUnary$2 (zone.dart:1675:5)
    at _Future__propagateToListeners_handleValueCallback.call$0 (future_impl.dart:169:29)
    at Object._Future__propagateToListeners (future_impl.dart:931:13)
    at _Future._completeWithValue$1 (future_impl.dart:707:5)
    at _Future__asyncCompleteWithValue_closure.call$0 (future_impl.dart:777:7)

from

    main() {
      var t1, i, result;
      result.$flags = 3;
      for (t1 = type$.dynamic, i = 0; result = A.List_List$from([], false, t1), i < result.length; ++i)
        ;
    }

the result variable is not initialized with a value, but it tries to set its flag to 3

Dart Version

Dart SDK version: 3.6.0 (stable) (Thu Dec 5 07:46:24 2024 -0800) on "windows_x64"

I understand that the code isn't good or anything, but this shouldn't happen, right? I don't know what else it might cause, it runs perfectly on debug mode and other platform

@sunarya-thito sunarya-thito added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Dec 21, 2024
@lrhn lrhn added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 22, 2024
@rakudrama rakudrama self-assigned this Dec 26, 2024
@rakudrama
Copy link
Member

Thanks for filing this bug and producing a nice repro. Definitely a bug.

The bug probably happens when List.unmodifiable was inlined into the for-loop condition.
A work-around would be based on preventing the inlining from happening.
This could be done in the SDK by putting @pragma('dart2js:never-inline') on the implementation of List.unmodifiable.
Perhaps the application has a method that returns List.unmodifiable and the pragma could be placed there.
That does not fix the bug, but prevents the result.$flags assignment from being inlined into the condition.

The assignment result.$flags = 3 should happen after the assignment to result.
It is very strange that this has been hoisted out of the loop.
I suspect that the bug is in some part of the 'codegen' phase after SSA optimizations, perhaps the part that tries to generate JavaScript for-loops from the SSA CFG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-ssa type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

3 participants