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 crashes on extension with records and type arguments #53358

Closed
Levi-Lesches opened this issue Aug 28, 2023 · 5 comments
Closed

dart2js crashes on extension with records and type arguments #53358

Levi-Lesches opened this issue Aug 28, 2023 · 5 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-crash web-dart2js

Comments

@Levi-Lesches
Copy link

Levi-Lesches commented Aug 28, 2023

I was running flutter build web --web-renderer html. I would post on Flutter's issues, but the error messages say it's an error with the compiler and to post here.

Warnings during compilation
Target dart2js failed: ProcessException: Process exited abnormally:
../../../AppData/Local/Pub/Cache/hosted/pub.dev/go_router-9.1.1/lib/src/information_provider.dart:125:34:
Warning: Operand of null-aware operation '!' has type 'String' which excludes null.
      location: routeInformation.location!,
                                 ^
../../../AppData/Local/Pub/Cache/hosted/pub.dev/go_router-9.1.1/lib/src/parser.dart:87:50:
Warning: Operand of null-aware operation '!' has type 'String' which excludes null.
        configuration.findMatch(routeInformation.location!, extra: state.extra);
Compiler crash
.dart_tool/flutter_build/49d81d521a48ddef28aae02d9dba2ea9/main.dart:
Internal Error: The compiler crashed when compiling this element.

The compiler is broken.

When compiling the above element, the compiler crashed. It is not
possible to tell if this is caused by a problem in your program or
not. Regardless, the compiler should not crash.

The Dart team would greatly appreciate if you would take a moment to
report this problem at http://dartbug.com/new.

Please include the following information:

* the name and version of your operating system,

* the Dart SDK build number (3.2.0-97.0.dev), and

* the entire message you see here (including the full stack trace
  below as well as the source location above).

The compiler crashed: Crash when compiling:
Bad state: No element

#0      List.last (dart:core-patch/growable_array.dart:348:5)
#1      LateLowering._addToCurrentScope (package:compiler/src/kernel/transformations/late_lowering.dart:212:28)
#2      LateLowering._initializedVariableCell (package:compiler/src/kernel/transformations/late_lowering.dart:262:12)
#3      LateLowering._variableCell (package:compiler/src/kernel/transformations/late_lowering.dart:221:11)
#4      LateLowering.transformVariableDeclaration (package:compiler/src/kernel/transformations/late_lowering.dart:275:12)
#5      _Lowering.visitVariableDeclaration (package:compiler/src/kernel/transformations/lowering.dart:73:26)
#6      VariableDeclaration.accept (package:kernel/ast.dart:10532:43)
#7      Transformer.transform (package:kernel/visitor.dart:1773:17)
#8      Transformer.transformList (package:kernel/visitor.dart:1790:18)
#9      Block.transformChildren (package:kernel/ast.dart:9008:7)
#10     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#11     TreeVisitor.defaultStatement (package:kernel/visitor.dart:415:41)
#12     TreeVisitor.visitBlock (package:kernel/visitor.dart:420:31)
#13     Block.accept (package:kernel/ast.dart:8996:43)
#14     Transformer.transform (package:kernel/visitor.dart:1773:17)
#15     Transformer.transformList (package:kernel/visitor.dart:1790:18)
#16     Block.transformChildren (package:kernel/ast.dart:9008:7)
#17     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#18     TreeVisitor.defaultStatement (package:kernel/visitor.dart:415:41)
#19     TreeVisitor.visitBlock (package:kernel/visitor.dart:420:31)
#20     Block.accept (package:kernel/ast.dart:8996:43)
#21     Transformer.transform (package:kernel/visitor.dart:1773:17)
#22     ForInStatement.transformChildren (package:kernel/ast.dart:9531:14)
#23     _Lowering.visitForInStatement (package:compiler/src/kernel/transformations/lowering.dart:112:15)
#24     ForInStatement.accept (package:kernel/ast.dart:9512:43)
#25     Transformer.transform (package:kernel/visitor.dart:1773:17)
#26     Transformer.transformList (package:kernel/visitor.dart:1790:18)
#27     Block.transformChildren (package:kernel/ast.dart:9008:7)
#28     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#29     TreeVisitor.defaultStatement (package:kernel/visitor.dart:415:41)
#30     TreeVisitor.visitBlock (package:kernel/visitor.dart:420:31)
#31     Block.accept (package:kernel/ast.dart:8996:43)
#32     Transformer.transform (package:kernel/visitor.dart:1773:17)
#33     BlockExpression.transformChildren (package:kernel/ast.dart:8611:14)
#34     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#35     TreeVisitor.defaultExpression (package:kernel/visitor.dart:223:43)
#36     TreeVisitor.visitBlockExpression (package:kernel/visitor.dart:350:51)
#37     BlockExpression.accept (package:kernel/ast.dart:8597:44)
#38     Transformer.transform (package:kernel/visitor.dart:1773:17)
#39     FieldInitializer.transformChildren (package:kernel/ast.dart:3302:15)
#40     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#41     TreeVisitor.defaultInitializer (package:kernel/visitor.dart:487:45)
#42     TreeVisitor.visitFieldInitializer (package:kernel/visitor.dart:492:53)
#43     FieldInitializer.accept (package:kernel/ast.dart:3288:45)
#44     Transformer.transform (package:kernel/visitor.dart:1773:17)
#45     Transformer.transformList (package:kernel/visitor.dart:1790:18)
#46     Constructor.transformChildren (package:kernel/ast.dart:2579:7)
#47     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#48     TreeVisitor.defaultMember (package:kernel/visitor.dart:471:35)
#49     _Lowering.defaultMember (package:compiler/src/kernel/transformations/lowering.dart:44:18)
#50     TreeVisitor.visitConstructor (package:kernel/visitor.dart:473:43)
#51     Constructor.accept (package:kernel/ast.dart:2558:40)
#52     Transformer.transform (package:kernel/visitor.dart:1773:17)
#53     Transformer.transformList (package:kernel/visitor.dart:1790:18)
#54     Class.transformChildren (package:kernel/ast.dart:1479:7)
#55     Transformer.defaultTreeNode (package:kernel/visitor.dart:1807:10)
#56     TreeVisitor.visitClass (package:kernel/visitor.dart:480:31)
#57     Class.accept (package:kernel/ast.dart:1426:38)
#58     Transformer.transform (package:kernel/visitor.dart:1773:17)
#59     Transformer.transformList (package:kernel/visitor.dart:1790:18)
#60     Library.transformChildren (package:kernel/ast.dart:605:7)
#61     _Lowering.visitLibrary (package:compiler/src/kernel/transformations/lowering.dart:49:10)
#62     List.forEach (dart:core-patch/growable_array.dart:416:8)
#63     transformLibraries (package:compiler/src/kernel/transformations/lowering.dart:22:13)
#64     Dart2jsTarget.performModularTransformationsOnLibraries (package:compiler/src/kernel/dart2js_target.dart:181:14)
#65     KernelTarget.runBuildTransformations (package:front_end/src/fasta/kernel/kernel_target.dart:1559:19)
#66     KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:626:7)
<asynchronous suspension>
#67     withCrashReporting (package:front_end/src/fasta/crash.dart:133:12)
<asynchronous suspension>
#68     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:576:12)
<asynchronous suspension>
#69     _buildInternal (package:front_end/src/kernel_generator_impl.dart:210:19)
<asynchronous suspension>
#70     withCrashReporting (package:front_end/src/fasta/crash.dart:133:12)
<asynchronous suspension>
#71     compile.<anonymous closure> (package:front_end/src/api_unstable/dart2js.dart:198:37)
<asynchronous suspension>
#72     CompilerContext.clear (package:front_end/src/fasta/compiler_context.dart:139:3)
<asynchronous suspension>
#73     compile (package:front_end/src/api_unstable/dart2js.dart:196:36)
<asynchronous suspension>
#74     _loadFromSource (package:compiler/src/phase/load_kernel.dart:304:29)
<asynchronous suspension>
#75     run (package:compiler/src/phase/load_kernel.dart:414:36)
<asynchronous suspension>
#76     Compiler.loadKernel (package:compiler/src/compiler.dart:388:9)
<asynchronous suspension>
#77     Compiler.produceKernel (package:compiler/src/compiler.dart:395:36)
<asynchronous suspension>
#78     Compiler.runSequentialPhases (package:compiler/src/compiler.dart:711:20)
<asynchronous suspension>
#79     Compiler.runInternal.<anonymous closure> (package:compiler/src/compiler.dart:311:7)
<asynchronous suspension>
#80     Compiler.runInternal (package:compiler/src/compiler.dart:310:5)
<asynchronous suspension>
#81     Compiler.run.<anonymous closure> (package:compiler/src/compiler.dart:231:11)
<asynchronous suspension>
#82     compile.<anonymous closure> (package:compiler/compiler_api.dart:249:30)
<asynchronous suspension>
#83     compile.compilationDone (package:compiler/src/dart2js.dart:738:3)
<asynchronous suspension>
#84     main (package:compiler/src/dart2js.dart:1241:3)
<asynchronous suspension>
  Command: C:\Users\Levi\flutter\bin\cache\dart-sdk\bin\dart.exe --disable-dart-dev C:\Users\Levi\flutter\bin\cache\dart-sdk\bin\snapshots\dart2js.dart.snapshot
  --platform-binaries=C:\Users\Levi\flutter\bin\cache\flutter_web_sdk\kernel --invoker=flutter_tool -Ddart.vm.product=true -DFLUTTER_WEB_AUTO_DETECT=false
  -DFLUTTER_WEB_USE_SKIA=false -DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/f613b678245991e74839f201ecd3f22b75465813/ --native-null-assertions
  --no-source-maps -o C:\Users\Levi\Coding\Flutter\chabad\.dart_tool\flutter_build\49d81d521a48ddef28aae02d9dba2ea9\app.dill --packages=.dart_tool/package_config.json --cfe-only  
  C:\Users\Levi\Coding\Flutter\chabad\.dart_tool\flutter_build\49d81d521a48ddef28aae02d9dba2ea9\main.dart
#0      RunResult.throwException (package:flutter_tools/src/base/process.dart:125:5)
#1      _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart:278:19)
<asynchronous suspension>
#2      Dart2JSTarget.build (package:flutter_tools/src/build_system/targets/web.dart:189:5)
<asynchronous suspension>
#3      _BuildInstance._invokeInternal (package:flutter_tools/src/build_system/build_system.dart:853:9)
<asynchronous suspension>
#4      Future.wait.<anonymous closure> (dart:async/future.dart:523:21)
<asynchronous suspension>
#5      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:791:32)
<asynchronous suspension>
#6      Future.wait.<anonymous closure> (dart:async/future.dart:523:21)
<asynchronous suspension>
#7      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:791:32)
<asynchronous suspension>
#8      FlutterBuildSystem.build (package:flutter_tools/src/build_system/build_system.dart:620:16)
<asynchronous suspension>
#9      WebBuilder.buildWeb (package:flutter_tools/src/web/compile.dart:89:34)
<asynchronous suspension>
#10     BuildWebCommand.runCommand (package:flutter_tools/src/commands/build_web.dart:198:5)
<asynchronous suspension>
#11     FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1323:27)
<asynchronous suspension>
#12     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#13     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#14     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:339:9)
<asynchronous suspension>
#15     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#16     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:285:5)
<asynchronous suspension>
#17     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:115:9)
<asynchronous suspension>
#18     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#19     main (package:flutter_tools/executable.dart:90:3)
<asynchronous suspension>

Compiling lib\main.dart for the Web...                             13.9s
Exception: Failed to compile application for the Web.
Flutter Doctor
What would you like me to do? flutter doctor -v
[✓] Flutter (Channel beta, 3.14.0-0.1.pre, on Microsoft Windows [Version 10.0.22621.2134],
    locale en-US)
    • Flutter version 3.14.0-0.1.pre on channel beta at C:\Users\Levi\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 120d54d33a (10 days ago), 2023-08-17 14:10:34 -0500
    • Engine revision f613b67824
    • Dart version 3.2.0 (build 3.2.0-42.1.beta)
    • DevTools version 2.26.1

[✓] Windows Version (Installed version of Windows is version 10 or higher)

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\Levi\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop Windows apps (Visual Studio Build Tools 2022 17.7.1)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.7.34009.444
    • Windows 10 SDK version 10.0.22621.0

[✓] Android Studio (version 2022.3)
    • Android Studio at C:\Program Files\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[✓] VS Code, 64-bit edition (version 1.81.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.70.0

[✓] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version
      10.0.22621.2134]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 116.0.5845.111
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 116.0.1938.62

[✓] Network resources
    • All expected network resources are available.

• No issues found!

I am on Windows 11 build 22621.2134. The crash occurred on Flutter stable, beta, and master, up to Dart version 3.2.0-97.0.dev.

I'm not sure how much of the code I can share so I will try to get an MVP

Update 1: Creating a new project with the same exact pubspec.yaml AND pubspec.lock does not crash. I am running flutter clean before compiling and still getting the error, despite using the same version of Flutter/Dart on both projects

Update 2. Upgrading to the latest versions of all my packages did not help. Copying all the code under lib to the new project broke the new project as well.

@Levi-Lesches
Copy link
Author

Levi-Lesches commented Aug 28, 2023

Update 3. I found the source of the crash. Definitely a Dart issue, not Flutter.

It's something to do with records, which makes sense given that it must have broken recently:

extension MapUtils<K, V> on Map<K, V> {
  Iterable<(K, V)> get records => entries.map((entry) => (entry.key, entry.value));
}

/* JSON structure for an "event": 
{
  "id": "123",
  "name": "Event name",
  "start": <Firestore Timestamp>,
  "end": <Firestore Timestamp>,
  "attendance": {
    "123": {  // Guest ID: Guest JSON object
      "id": "123",
      "name": "First, Last",
      "email": "email@domain.com",
      // ...
    },
},
*/
  Event.fromJson(Json json) : 
    id = json["id"],
    name = json["name"]!,
    start = (json["start"] as Timestamp).toDate(),
    end = (json["end"] as Timestamp).toDate(),
    attendance = {
      // This does NOT work
      for (final (String name, guestJson) in MapUtils<String, Map<String, dynamic>>(json["attendance"]).records) 
      // This DOES work
      for (final (name, guestJson) in MapUtils<String, Map<String, dynamic>>(json["attendance"]).records) 
        name: Guest.fromJson(guestJson),
    };

Notice that all I added to crash the compiler was (name -> (String name. I can try to get a reproducible sample but I don't know if it depends on more of my code or why it fails. I just know that removing the explicit type arguments makes Dart stop crashing. I don't really have a lot of time so I hope this is enough to start debugging but if not I can try to make an MVP.

@Levi-Lesches Levi-Lesches changed the title Compiler crashes when compiling Flutter Web app Compiler crashes on extension with records and type arguments Aug 28, 2023
@Levi-Lesches Levi-Lesches changed the title Compiler crashes on extension with records and type arguments dart2js crashes on extension with records and type arguments Aug 28, 2023
@srawlins srawlins added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Aug 28, 2023
@sigmundch
Copy link
Member

Thank you so much for the bug report and for isolating the root cause, it helps a lot!

After I few iterations, it appears that the core issue here is with late-lowering on field initializers, but that we can repro the same failure even without extension methods and without type arguments. Here is a smaller repro that illustrates it:

Iterable<(Object?, dynamic)> get values => [('a', 1), ('b', 'two')];

class A {
  Map field = {
    for (var (Object? n, v) in values) n: v,
  };
}

main() => A();

@biggs0125
Copy link

I looked into this a bit too and see that the Dart2JS LateLowering assumes that every time a late field is read it will be in the context of a Function body. Initializer bodies are an exception to this but we should be able to treat initializer bodies as a similar scoping level:
https://github.com/dart-lang/sdk/blob/main/pkg/compiler/lib/src/kernel/transformations/late_lowering.dart#L191

@biggs0125
Copy link

A fix for this has been submitted in https://dart-review.googlesource.com/c/sdk/+/323020

@sigmundch
Copy link
Member

Thanks @biggs0125

copybara-service bot pushed a commit that referenced this issue Sep 6, 2023
The CFE's lowering for record patterns can sometimes make use of late local fields. Dart2JS's late lowerer assumes that all late local fields live in the scope of some function body. Record patterns allow late locals to be created in other contexts though. Some examples that break this assumption include field initializers and expressions in constructor initializer lists.

To fix this we add a late local scope in some extra contexts where these record patterns can show up.

Fixes: 53358

Bug: #53358
Change-Id: Ic730f41253782241b3394c0b0353d1731e122c85
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/323020
Cherry-pick-request: #53449
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324600
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
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-crash web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants