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.24k stars 1.57k forks source link

type 'InvalidType' is not a subtype of type 'InterfaceType' in type cast #56757

Open ferrantejake opened 1 month ago

ferrantejake commented 1 month ago
.dart_tool/flutter_build/458e4d88ecd75baf48682554536eaf3a/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.5.0), and

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

The compiler crashed: type 'InvalidType' is not a subtype of type 'InterfaceType' in type cast
#0      StaticInteropClassEraser.visitStaticInvocation (package:_js_interop_checks/src/transformations/static_interop_class_eraser.dart:221:49)
#1      StaticInvocation.accept (package:kernel/ast.dart:6582:44)
#2      Transformer.transform (package:kernel/visitor.dart:1771:17)
#3      ReturnStatement.transformChildren (package:kernel/ast.dart:10154:22)
#4      Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#5      TreeVisitorDefault.defaultStatement (package:kernel/visitor.dart:622:41)
#6      StatementVisitorDefaultMixin.visitReturnStatement (package:kernel/visitor.dart:377:51)
#7      ReturnStatement.accept (package:kernel/ast.dart:10140:43)
#8      Transformer.transform (package:kernel/visitor.dart:1771:17)
#9      FunctionNode.transformChildren (package:kernel/ast.dart:3905:16)
#10     Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#11     TreeVisitorDefaultMixin.visitFunctionNode (package:kernel/visitor.dart:577:45)
#12     FunctionNode.accept (package:kernel/ast.dart:3872:38)
#13     Transformer.transform (package:kernel/visitor.dart:1771:17)
#14     Procedure.transformChildren (package:kernel/ast.dart:3247:18)
#15     Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#16     TreeVisitorDefault.defaultMember (package:kernel/visitor.dart:626:35)
#17     MemberVisitorDefaultMixin.visitProcedure (package:kernel/visitor.dart:411:39)
#18     StaticInteropClassEraser.visitProcedure (package:_js_interop_checks/src/transformations/static_interop_class_eraser.dart:179:18)
#19     Procedure.accept (package:kernel/ast.dart:3228:40)
#20     Transformer.transform (package:kernel/visitor.dart:1771:17)
#21     Transformer.transformList (package:kernel/visitor.dart:1788:18)
#22     Class.transformChildren (package:kernel/ast.dart:1525:7)
#23     Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#24     TreeVisitorDefaultMixin.visitClass (package:kernel/visitor.dart:556:31)
#25     Class.accept (package:kernel/ast.dart:1471:38)
#26     Transformer.transform (package:kernel/visitor.dart:1771:17)
#27     Transformer.transformList (package:kernel/visitor.dart:1788:18)
#28     Library.transformChildren (package:kernel/ast.dart:609:7)
#29     Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#30     TreeVisitorDefaultMixin.visitLibrary (package:kernel/visitor.dart:565:35)
#31     StaticInteropClassEraser.visitLibrary (package:_js_interop_checks/src/transformations/static_interop_class_eraser.dart:119:18)
#32     Library.accept (package:kernel/ast.dart:585:38)
#33     Transformer.transform (package:kernel/visitor.dart:1771:17)
#34     Transformer.transformList (package:kernel/visitor.dart:1788:18)
#35     Component.transformChildren (package:kernel/ast.dart:14644:7)
#36     Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10)
#37     TreeVisitorDefaultMixin.visitComponent (package:kernel/visitor.dart:600:39)
#38     _doTransformsOnKernelLoad (package:compiler/src/phase/load_kernel.dart:172:41)
#39     _loadFromSource (package:compiler/src/phase/load_kernel.dart:328:5)
<asynchronous suspension>
#40     run (package:compiler/src/phase/load_kernel.dart:412:36)
<asynchronous suspension>
#41     Compiler.loadKernel (package:compiler/src/compiler.dart:392:9)
<asynchronous suspension>
#42     Compiler.produceKernel (package:compiler/src/compiler.dart:399:36)
<asynchronous suspension>
#43     Compiler.runSequentialPhases (package:compiler/src/compiler.dart:633:20)
<asynchronous suspension>
#44     Compiler.runInternal.<anonymous closure> (package:compiler/src/compiler.dart:316:7)
<asynchronous suspension>
#45     Compiler.runInternal (package:compiler/src/compiler.dart:315:5)
<asynchronous suspension>
#46     Compiler.run.<anonymous closure> (package:compiler/src/compiler.dart:236:11)
<asynchronous suspension>
#47     compile.<anonymous closure> (package:compiler/compiler_api.dart:256:30)
<asynchronous suspension>
#48     compile.compilationDone (package:compiler/src/dart2js.dart:723:3)
<asynchronous suspension>
#49     main (package:compiler/src/dart2js.dart:1226:3)
<asynchronous suspension>
  Command: C:\Users\jakef\flutter\flutter_windows_3.19.6-stable\flutter\bin\cache\dart-sdk\bin\dart.exe --disable-dart-dev C:\Users\jakef\flutter\flutter_windows_3.19.6-stable\flutter\bin\cache\dart-sdk\bin\snapshots\dart2js.dart.snapshot
  --platform-binaries=C:\Users\jakef\flutter\flutter_windows_3.19.6-stable\flutter\bin\cache\flutter_web_sdk\kernel --invoker=flutter_tool -Ddart.vm.product=true -DFLUTTER_WEB_AUTO_DETECT=false -DFLUTTER_WEB_USE_SKIA=true
  -DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/b8800d88be4866db1b15f8b954ab2573bba9960f/ --native-null-assertions --no-source-maps -o C:\Users\jakef\Documents\code\project\project\.dart_tool\flutter_build\458e4d88ecd75baf48682554536eaf3a\app.dill --packages=.dart_tool/package_config.json
  --cfe-only C:\Users\jakef\Documents\code\project\project\.dart_tool\flutter_build\458e4d88ecd75baf48682554536eaf3a\main.dart
#0      RunResult.throwException (package:flutter_tools/src/base/process.dart:122:5)
#1      _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart:370:19)
<asynchronous suspension>
#2      Dart2JSTarget.build (package:flutter_tools/src/build_system/targets/web.dart:203:5)
<asynchronous suspension>
#3      _BuildInstance._invokeInternal (package:flutter_tools/src/build_system/build_system.dart:875:9)
<asynchronous suspension>
#4      Future.wait.<anonymous closure> (dart:async/future.dart:534:21)
<asynchronous suspension>
#5      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:813:32)
<asynchronous suspension>
#6      Future.wait.<anonymous closure> (dart:async/future.dart:534:21)
<asynchronous suspension>
#7      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:813:32)
<asynchronous suspension>
#8      FlutterBuildSystem.build (package:flutter_tools/src/build_system/build_system.dart:635:16)
<asynchronous suspension>
#9      WebBuilder.buildWeb (package:flutter_tools/src/web/compile.dart:92:34)
<asynchronous suspension>
#10     BuildWebCommand.runCommand (package:flutter_tools/src/commands/build_web.dart:227:5)
<asynchronous suspension>
#11     FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1408:27)
<asynchronous suspension>
#12     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153: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:420:9)
<asynchronous suspension>
#15     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#16     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:364:5)
<asynchronous suspension>
#17     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:130:9)
<asynchronous suspension>
#18     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#19     main (package:flutter_tools/executable.dart:93:3)
<asynchronous suspension>
dart-github-bot commented 1 month ago

Summary: The Dart compiler crashed during compilation due to a type mismatch. The compiler encountered an "InvalidType" which is not a subtype of "InterfaceType" during a type cast operation.

srujzs commented 1 month ago

Can you share a minimal repro here? It looks like a @staticInterop class' factory function's return type is being treated as an InvalidType.

sigmundch commented 1 month ago

InvalidType usually come from other static errors in the input program, but where the compiler continues running past that point.

I was able to repro with an example like this:

import 'dart:js_interop';

class A {}

@staticInterop
class A { 
  external factory A();
}

main() => A();

This produced a lot of static errors first, then the crash:

dart compile js q.dart
q.dart:6:7:
Error: 'A' is already declared in this scope.
class A { 
      ^
q.dart:3:7:
Info: Previous declaration of 'A'.
class A {}
      ^
q.dart:7:20:
Error: 'A' isn't a type.
  external factory A();
                   ^
q.dart:7:20:
Info: This isn't a type.
  external factory A();
                   ^
q.dart:10:11:
Error: Can't use 'A' because it is declared more than once.
main() => A();
          ^
q.dart:6:7:
Error: `@staticInterop` classes should also have the `@JS` annotation.
class A { 
      ^
q.dart:7:20:
Error: Only JS interop members may be 'external'.
  external factory A();
                   ^
q.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.6.0-255.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: type 'InvalidType' is not a subtype of type 'InterfaceType' in type cast
#0      StaticInteropClassEraser.visitStaticInvocation (package:_js_interop_checks/src/transformations/static_interop_class_eraser.dart:221:49)
#1      StaticInvocation.accept (package:kernel/ast.dart:6604:44)

I haven't tried this in the flutter tool, but I wonder if the errors are less visible in that case?

I wonder if we can bail early on some of our transformations if we know compile-time errors have already been emitted?

srujzs commented 1 month ago

I wonder if we can bail early on some of our transformations if we know compile-time errors have already been emitted?

We do something somewhat similar here, but we can expand this to all errors and for more transforms. The transform in question is done in the backend, but we can still query the DiagnosticReporter. That being said, I'm sure we'll still hit a crash at some point in the compilation pipeline because we assume InvalidType doesn't exist.

ferrantejake commented 1 month ago

I'll see what I can do to replicate the issue with a minimal repo - this has taken a backseat on priority but I wanted to share in case that trace was useful.