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: Internal Error: type 'InterfaceType' is not a subtype of type 'FunctionType' in type cast #56443

Open
xvrh opened this issue Aug 12, 2024 · 3 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-js-interop Issues that impact all js interop

Comments

@xvrh
Copy link
Contributor

xvrh commented Aug 12, 2024

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.<anonymous closure> (package:front_end/src/kernel/kernel_target.dart:770:7)
           <asynchronous suspension>
           #45     withCrashReporting (package:front_end/src/base/crash.dart:138:12)
           <asynchronous suspension>
           #46     KernelTarget.buildComponent (package:front_end/src/kernel/kernel_target.dart:699:12)
           <asynchronous suspension>
           #47     _buildInternal (package:front_end/src/kernel_generator_impl.dart:239:19)
           <asynchronous suspension>
           #48     withCrashReporting (package:front_end/src/base/crash.dart:138:12)
           <asynchronous suspension>
           #49     compile.<anonymous closure> (package:front_end/src/api_unstable/dart2js.dart:165:37)
           <asynchronous suspension>
           #50     CompilerContext.clear (package:front_end/src/base/compiler_context.dart:128:3)
           <asynchronous suspension>
           #51     compile (package:front_end/src/api_unstable/dart2js.dart:163:36)
           <asynchronous suspension>
           #52     _loadFromSource (package:compiler/src/phase/load_kernel.dart:318:29)
           <asynchronous suspension>
           #53     run (package:compiler/src/phase/load_kernel.dart:412:36)
           <asynchronous suspension>
           #54     Compiler.loadKernel (package:compiler/src/compiler.dart:392:9)
           <asynchronous suspension>
           #55     Compiler.produceKernel (package:compiler/src/compiler.dart:399:36)
           <asynchronous suspension>
           #56     Compiler.runSequentialPhases (package:compiler/src/compiler.dart:633:20)
           <asynchronous suspension>
           #57     Compiler.runInternal.<anonymous closure> (package:compiler/src/compiler.dart:316:7)
           <asynchronous suspension>
           #58     Compiler.runInternal (package:compiler/src/compiler.dart:315:5)
           <asynchronous suspension>
           #59     Compiler.run.<anonymous closure> (package:compiler/src/compiler.dart:236:11)
           <asynchronous suspension>
           #60     compile.<anonymous closure> (package:compiler/compiler_api.dart:256:30)
           <asynchronous suspension>
           #61     compile.compilationDone (package:compiler/src/dart2js.dart:723:3)
           <asynchronous suspension>
           #62     main (package:compiler/src/dart2js.dart:1226:3)
           <asynchronous suspension>

Reproduction:

@dart-github-bot
Copy link
Collaborator

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.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Aug 12, 2024
@biggs0125 biggs0125 added web-dart2js web-js-interop Issues that impact all js interop P1 A high priority bug; for example, a single project is unusable or has many test failures labels Aug 13, 2024
@biggs0125
Copy link

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
Copy link
Member

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

@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Aug 14, 2024
copybara-service bot pushed a commit that referenced this issue Aug 15, 2024
… children.

The JS interop transformer assumes the entire AST has been checked and ends up trying to cast to FunctionType. If there is malformed, unvisited interop code this results opaque failure (failed type cast) rather than the useful error message from the check.

Bug: #56443
Change-Id: I5b911d0da7a839e3045dce61121496d464b79af9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380381
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. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

5 participants