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.27k stars 1.58k forks source link

dart2wasm CRASH with demo app #56234

Closed kevmoo closed 4 months ago

kevmoo commented 4 months ago

Source: https://github.com/flutter/engine/compare/6b36113c4ab7...63dd50788d4a Dart SDK version: 3.6.0-36.0.dev (dev) (Fri Jul 12 09:05:30 2024 -0700) on "macos_arm64"

Exception in InstanceInvocation at file:///Users/kevmoo/github/purrfect_code/lib/src/sokoban_view/components/world.dart:186:31

Unhandled exception:
Conversion between non-reference types
#0      Translator.convertType (package:dart2wasm/translator.dart:873)
#1      CodeGenerator.wrap (package:dart2wasm/code_generator.dart:970)
#2      CodeGenerator.visitAsExpression (package:dart2wasm/code_generator.dart:3218)
#3      AsExpression.accept1 (package:kernel/ast.dart:7611)
#4      CodeGenerator.wrap (package:dart2wasm/code_generator.dart:969)
#5      CodeGenerator.visitVariableDeclaration (package:dart2wasm/code_generator.dart:1154)
#6      VariableDeclaration.accept (package:kernel/ast.dart:10739)
#7      CodeGenerator.visitStatement (package:dart2wasm/code_generator.dart:986)
#8      CodeGenerator.visitBlock (package:dart2wasm/code_generator.dart:1111)
#9      Block.accept (package:kernel/ast.dart:9181)
#10     CodeGenerator.visitStatement (package:dart2wasm/code_generator.dart:986)
#11     CodeGenerator.visitForStatement (package:dart2wasm/code_generator.dart:1583)
#12     ForStatement.accept (package:kernel/ast.dart:9612)
#13     CodeGenerator.visitStatement (package:dart2wasm/code_generator.dart:986)
#14     CodeGenerator.visitBlock (package:dart2wasm/code_generator.dart:1111)
#15     Block.accept (package:kernel/ast.dart:9181)
#16     CodeGenerator.visitStatement (package:dart2wasm/code_generator.dart:986)
#17     CodeGenerator.generateBody (package:dart2wasm/code_generator.dart:672)
#18     CodeGenerator.generate (package:dart2wasm/code_generator.dart:300)
#19     Translator.translate (package:dart2wasm/translator.dart:374)
#20     compileToModule (package:dart2wasm/compile.dart:211)
<asynchronous suspension>
#21     generateWasm (package:dart2wasm/generate_wasm.dart:34)
<asynchronous suspension>
#22     main (file:///Volumes/Work/s/w/ir/x/w/sdk/pkg/dart2wasm/bin/dart2wasm.dart:10)
<asynchronous suspension>
  Command: /Users/kevmoo/github/flutter/bin/cache/dart-sdk/bin/dart compile wasm --packages=.dart_tool/package_config.json
  --extra-compiler-option=--platform=/Users/kevmoo/github/flutter/bin/cache/flutter_web_sdk/kernel/dart2wasm_platform.dill --extra-compiler-option=--delete-tostring-package-uri=dart:ui
  --extra-compiler-option=--delete-tostring-package-uri=package:flutter --extra-compiler-option=--import-shared-memory --extra-compiler-option=--shared-memory-max-pages=32768 -Ddart.vm.product=true -DFLUTTER_WEB_AUTO_DETECT=false
  -DFLUTTER_WEB_USE_SKIA=false -DFLUTTER_WEB_USE_SKWASM=true -DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/d4df5638e5df99861310bb413724a512b2258de0/
  --extra-compiler-option=--depfile=/Users/kevmoo/github/purrfect_code/.dart_tool/flutter_build/2576a4133cf0f67649d43ff80cd24146/dart2wasm.d -O4 --strip-wasm -o
  /Users/kevmoo/github/purrfect_code/.dart_tool/flutter_build/2576a4133cf0f67649d43ff80cd24146/main.dart.wasm /Users/kevmoo/github/purrfect_code/.dart_tool/flutter_build/2576a4133cf0f67649d43ff80cd24146/main.dart
kevmoo commented 4 months ago

Here's the fun bit. With this diff, no compiler crash:

diff --git a/lib/src/sokoban_view/components/grided.dart b/lib/src/sokoban_view/components/grided.dart
index 198775a..e28a176 100644
--- a/lib/src/sokoban_view/components/grided.dart
+++ b/lib/src/sokoban_view/components/grided.dart
@@ -25,6 +25,6 @@ mixin GridElement {
   Vector2 getPosition() => Vector2(
       (gridPosition.x * gridPixelDimensions.x) + gridPixelOffset.x,
       (gridPosition.y * gridPixelDimensions.y) + gridPixelOffset.y);
-  double getGridValue() => (gridPosition.y * 13.0) + gridPosition.x;
-  int getLayeredGridValue() => getGridValue().toInt() + (layer * 200);
+  int getGridValue() => ((gridPosition.y * 13.0) + gridPosition.x).toInt();
+  int getLayeredGridValue() => getGridValue() + (layer * 200);
 }
diff --git a/lib/src/sokoban_view/components/world.dart b/lib/src/sokoban_view/components/world.dart
index c68e5a8..c2460f3 100644
--- a/lib/src/sokoban_view/components/world.dart
+++ b/lib/src/sokoban_view/components/world.dart
@@ -158,7 +158,7 @@ class SokobanWorld extends World with HasGameRef {
       var initialIdx = int.parse(split[1]);
       var nextIdx = int.parse(split[2]);
       var goalHit = bool.parse(split[3]);
-      var gridValue = box.getGridValue() as int;
+      var gridValue = box.getGridValue();

       logger.i('box $idx $gridValue');
       if (gridValue == initialIdx) {
@@ -183,7 +183,7 @@ class SokobanWorld extends World with HasGameRef {

     for (var idx = 0; idx < batteries.length; idx++) {
       var battery = batteries[idx];
-      var gridValue = battery.getGridValue() as int;
+      var gridValue = battery.getGridValue();

       if (gridValue == initialIdx) {
         battery.hide();
osa1 commented 4 months ago

Smaller repro:

class Num {
  final double x;

  Num(this.x);

  double getX() => x;
}

void main() {
  final num = Num(0.0);
  var value = num.getX() as int;
  print(value);
}

Compile with --omit-implicit-checks --omit-explicit-checks.