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.07k stars 1.56k forks source link

dart2js: Internal Error: type 'InterfaceType' is not a subtype of type 'FunctionType' in type cast #56443

Open xvrh opened 1 month ago

xvrh commented 1 month ago

Since Dart 3.5, we hit a compiler crash in the chrome_extension package (which use dart:js_interop internally).

Full error message:

Target dart2js failed: ProcessException: Process exited abnormally with exit code 253: ``` Target dart2js failed: ProcessException: Process exited abnormally with exit code 253: .dart_tool/flutter_build/b7629c0c1f24a1cdb3a354a01092bc2f/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: Crash when compiling: type 'InterfaceType' is not a subtype of type 'FunctionType' in type cast #0 JsUtilOptimizer._lowerFunctionToJS (package:_js_interop_checks/src/transformations/js_util_optimizer.dart:698:52) #1 JsUtilOptimizer.visitStaticInvocation (package:_js_interop_checks/src/transformations/js_util_optimizer.dart:502:20) #2 StaticInvocation.accept (package:kernel/ast.dart:6582:44) #3 Transformer.transform (package:kernel/visitor.dart:1771:17) #4 InstanceSet.transformChildren (package:kernel/ast.dart:4853:15) #5 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10) #6 TreeVisitorDefault.defaultExpression (package:kernel/visitor.dart:618:43) #7 ExpressionVisitorDefaultMixin.visitInstanceSet (package:kernel/visitor.dart:109:43) #8 InstanceSet.accept (package:kernel/ast.dart:4835:44) #9 Transformer.transform (package:kernel/visitor.dart:1771:17) #10 ExpressionStatement.transformChildren (package:kernel/ast.dart:9141:20) #11 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10) #12 TreeVisitorDefault.defaultStatement (package:kernel/visitor.dart:622:41) #13 StatementVisitorDefaultMixin.visitExpressionStatement (package:kernel/visitor.dart:343:7) #14 ExpressionStatement.accept (package:kernel/ast.dart:9128:43) #15 Transformer.transform (package:kernel/visitor.dart:1771:17) #16 Transformer.transformList (package:kernel/visitor.dart:1788:18) #17 Block.transformChildren (package:kernel/ast.dart:9193:7) #18 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10) #19 TreeVisitorDefault.defaultStatement (package:kernel/visitor.dart:622:41) #20 StatementVisitorDefaultMixin.visitBlock (package:kernel/visitor.dart:345:31) #21 Block.accept (package:kernel/ast.dart:9181:43) #22 Transformer.transform (package:kernel/visitor.dart:1771:17) #23 FunctionNode.transformChildren (package:kernel/ast.dart:3905:16) #24 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10) #25 TreeVisitorDefaultMixin.visitFunctionNode (package:kernel/visitor.dart:577:45) #26 FunctionNode.accept (package:kernel/ast.dart:3872:38) #27 Transformer.transform (package:kernel/visitor.dart:1771:17) #28 Constructor.transformChildren (package:kernel/ast.dart:2693:18) #29 JsUtilOptimizer.defaultMember (package:_js_interop_checks/src/transformations/js_util_optimizer.dart:143:10) #30 MemberVisitorDefaultMixin.visitConstructor (package:kernel/visitor.dart:409:43) #31 Constructor.accept (package:kernel/ast.dart:2671:40) #32 Transformer.transform (package:kernel/visitor.dart:1771:17) #33 Transformer.transformList (package:kernel/visitor.dart:1788:18) #34 Class.transformChildren (package:kernel/ast.dart:1524:7) #35 Transformer.defaultTreeNode (package:kernel/visitor.dart:1805:10) #36 TreeVisitorDefaultMixin.visitClass (package:kernel/visitor.dart:556:31) #37 Class.accept (package:kernel/ast.dart:1471:38) #38 Transformer.transform (package:kernel/visitor.dart:1771:17) #39 Transformer.transformList (package:kernel/visitor.dart:1788:18) #40 Library.transformChildren (package:kernel/ast.dart:609:7) #41 JsUtilOptimizer.visitLibrary (package:_js_interop_checks/src/transformations/js_util_optimizer.dart:135:10) #42 Dart2jsTarget.performModularTransformationsOnLibraries (package:compiler/src/kernel/dart2js_target.dart:169:25) #43 KernelTarget.runBuildTransformations (package:front_end/src/kernel/kernel_target.dart:1724:19) #44 KernelTarget.buildComponent. (package:front_end/src/kernel/kernel_target.dart:770:7) #45 withCrashReporting (package:front_end/src/base/crash.dart:138:12) #46 KernelTarget.buildComponent (package:front_end/src/kernel/kernel_target.dart:699:12) #47 _buildInternal (package:front_end/src/kernel_generator_impl.dart:239:19) #48 withCrashReporting (package:front_end/src/base/crash.dart:138:12) #49 compile. (package:front_end/src/api_unstable/dart2js.dart:165:37) #50 CompilerContext.clear (package:front_end/src/base/compiler_context.dart:128:3) #51 compile (package:front_end/src/api_unstable/dart2js.dart:163:36) #52 _loadFromSource (package:compiler/src/phase/load_kernel.dart:318:29) #53 run (package:compiler/src/phase/load_kernel.dart:412:36) #54 Compiler.loadKernel (package:compiler/src/compiler.dart:392:9) #55 Compiler.produceKernel (package:compiler/src/compiler.dart:399:36) #56 Compiler.runSequentialPhases (package:compiler/src/compiler.dart:633:20) #57 Compiler.runInternal. (package:compiler/src/compiler.dart:316:7) #58 Compiler.runInternal (package:compiler/src/compiler.dart:315:5) #59 Compiler.run. (package:compiler/src/compiler.dart:236:11) #60 compile. (package:compiler/compiler_api.dart:256:30) #61 compile.compilationDone (package:compiler/src/dart2js.dart:723:3) #62 main (package:compiler/src/dart2js.dart:1226:3) ```

Reproduction:

dart-github-bot commented 1 month ago

Summary: The dart2js compiler crashes during compilation of the chrome_extension package due to a type mismatch error. The compiler attempts to cast an InterfaceType to a FunctionType, which is invalid, resulting in a crash.

biggs0125 commented 4 weeks ago

It looks like there's a bug in JsInteropChecks.visitConstructor where we're not recursing into the constructor's children to see if there are any errors.

Minimal repro:

import 'dart:js_interop';

class A {
  A() {
    Function f = (int x) => x;
    x.toJS; // <-- This should get caught by a check before the transform.
  }
}

Since the check is missing the error, the compiler continues to the transform which assumes everything is well-formed and eventually throws on the cast shown in the exception.

I'll put together a fix but in the meantime if you can find which .toJS call is failing, then you will likely see that the static type of the function is Function and not a more specific type like void Function(). .toJS only works when the static type is more specific (this is normally caught by the checker).

sigmundch commented 4 weeks ago

Nice repro isolation! Given the low risk and crash visibility, this may be a good candidate for filing a cherry-pick request.