Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compiler crash when destructing and spread operator are used in a for loop #53268

Closed
ebraminio opened this issue Aug 18, 2023 · 4 comments
Closed
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@ebraminio
Copy link

ebraminio commented Aug 18, 2023

This is a minified version of something I spotted in a real program, enter the following in https://dartpad.dev

void main() {
  for (final (i, item) in [...[]].indexed) {}
}

You'll get a compiler crash,

Internal Error: The compiler crashed when compiling this element.

The compiler is broken.

Crash log Error compiling to JavaScript: lib/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.1.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 file:///tmp/dartpadNIFUFT/lib/main.dart at character offset 10:
type 'VariableDeclaration' is not a subtype of type 'VariableDeclarationImpl' of 'node'

#0 InferenceVisitorImpl.visitVariableDeclaration (package:front_end/src/fasta/type_inference/inference_visitor.dart:8631:41)
#1 VariableDeclaration.accept (package:kernel/ast.dart:10709:43)
#2 InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:242:26)
#3 InferenceVisitorImpl._visitStatements (package:front_end/src/fasta/type_inference/inference_visitor.dart:847:50)
#4 InferenceVisitorImpl.visitBlock (package:front_end/src/fasta/type_inference/inference_visitor.dart:868:31)
#5 Block.accept (package:kernel/ast.dart:9173:43)
#6 InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:242:26)
#7 InferenceVisitorImpl.visitBlockExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:384:43)
#8 BlockExpression.accept1 (package:kernel/ast.dart:8778:9)
#9 InferenceVisitorImpl._inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:259:27)
#10 InferenceVisitorImpl.inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:291:40)
#11 InferenceVisitorBase._inferInvocation.inferArgument (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1872:22)
#12 InferenceVisitorBase._inferInvocation (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1936:44)
#13 InferenceVisitorBase.inferInvocation (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1627:12)
#14 InferenceVisitorImpl.visitStaticInvocation (package:front_end/src/fasta/type_inference/inference_visitor.dart:8065:40)
#15 StaticInvocation.accept1 (package:kernel/ast.dart:6582:9)
#16 InferenceVisitorImpl._inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:259:27)
#17 InferenceVisitorImpl.inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:291:40)
#18 InferenceVisitorImpl.inferForInIterable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1506:9)
#19 InferenceVisitorImpl._handlePatternForIn (package:front_end/src/fasta/type_inference/inference_visitor.dart:1630:9)
#20 InferenceVisitorImpl.handleForInWithoutVariable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1654:14)
#21 InferenceVisitorImpl.visitForInStatementWithSynthesizedVariable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1694:26)
#22 ForInStatementWithSynthesizedVariable.acceptInference (package:front_end/src/fasta/kernel/internal_ast.dart:266:20)
#23 InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:240:26)
#24 InferenceVisitorImpl._visitStatements (package:front_end/src/fasta/type_inference/inference_visitor.dart:847:50)
#25 InferenceVisitorImpl.visitBlock (package:front_end/src/fasta/type_inference/inference_visitor.dart:868:31)
#26 Block.accept (package:kernel/ast.dart:9173:43)
#27 InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:242:26)
#28 TypeInferrerImpl.inferFunctionBody (package:front_end/src/fasta/type_inference/type_inferrer.dart:198:17)
#29 BodyBuilder.finishFunction (package:front_end/src/fasta/kernel/body_builder.dart:1254:43)
#30 DietListener.buildFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:1133:19)
#31 DietListener.endTopLevelMethod (package:front_end/src/fasta/source/diet_listener.dart:387:5)
#32 Parser.parseTopLevelMethod (package:_fe_analyzer_shared/src/parser/parser_impl.dart:3747:14)
#33 Parser.parseTopLevelMemberImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:3504:14)
#34 Parser.parseTopLevelDeclarationImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:624:14)
#35 Parser.parseUnit (package:_fe_analyzer_shared/src/parser/parser_impl.dart:414:15)
#36 SourceLoader.buildBody (package:front_end/src/fasta/source/source_loader.dart:1247:12)

#37 SourceLoader.buildBodies (package:front_end/src/fasta/source/source_loader.dart:669:7)

#38 KernelTarget.buildComponent. (package:front_end/src/fasta/kernel/kernel_target.dart:607:7)

#39 withCrashReporting (package:front_end/src/fasta/crash.dart:133:12)

#40 KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:588:12)

#41 _buildInternal (package:front_end/src/kernel_generator_impl.dart:210:19)

#42 withCrashReporting (package:front_end/src/fasta/crash.dart:133:12)

#43 compile. (package:front_end/src/api_unstable/dart2js.dart:198:37)

#44 compile (package:front_end/src/api_unstable/dart2js.dart:196:36)

#45 _loadFromSource (package:compiler/src/phase/load_kernel.dart:304:29)

#46 run (package:compiler/src/phase/load_kernel.dart:417:36)

#47 Compiler.loadKernel (package:compiler/src/compiler.dart:388:9)

#48 Compiler.produceKernel (package:compiler/src/compiler.dart:395:36)

#49 Compiler.runSequentialPhases (package:compiler/src/compiler.dart:711:20)

#50 Compiler.runInternal. (package:compiler/src/compiler.dart:311:7)

#51 Compiler.runInternal (package:compiler/src/compiler.dart:310:5)

#52 Compiler.run. (package:compiler/src/compiler.dart:231:11)

#53 main (package:compiler/src/dart2js.dart:1241:3)

@ebraminio ebraminio changed the title Compiler crash when destructing and spread operator involved Compiler crash when destructing and spread operator are used in a for loop Aug 18, 2023
@lrhn
Copy link
Member

lrhn commented Aug 18, 2023

Marking as dart2js for now, but may be a general front-end issue.

Seems to require a destructuring pattern declaration, a collection-elements operation, and an extension member called on the collection, to trigger.

Changing :

void main() {
  for (var [int i] in <int>[1, if (true) 1, 1].asLists()) {
    print("$i");
  }
}
extension on List<int> {
  Iterable<List<int>> asLists() => this.map((i) => [i]);
}

still triggers the same error. Changing the spread or if-element to a for-element also keeps the error.

Removing the spread or other collection-elements operation removes the error.
Changing the declaration to non-destructuring, like the original final (i, item) to final (int, int) p removes the error.
Changing .indexed to a non-extension invocation, like .map<(int, int)>((x) => (x, x)), removes the error.

Crash-log includes:

type 'VariableDeclaration' is not a subtype of type 'VariableDeclarationImpl' of 'node'

#0      InferenceVisitorImpl.visitVariableDeclaration (package:front_end/src/fasta/type_inference/inference_visitor.dart:8629:41)
#1      VariableDeclaration.accept (package:kernel/ast.dart:10532:43)
#2      InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:252:26)
#3      InferenceVisitorImpl._visitStatements (package:front_end/src/fasta/type_inference/inference_visitor.dart:856:50)
#4      InferenceVisitorImpl.visitBlock (package:front_end/src/fasta/type_inference/inference_visitor.dart:877:31)
#5      Block.accept (package:kernel/ast.dart:8996:43)
#6      InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:252:26)
#7      InferenceVisitorImpl.visitBlockExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:394:43)
#8      BlockExpression.accept1 (package:kernel/ast.dart:8601:9)
#9      InferenceVisitorImpl._inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:269:27)
#10     InferenceVisitorImpl.inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:301:40)
#11     InferenceVisitorBase._inferInvocation.inferArgument (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1787:22)
#12     InferenceVisitorBase._inferInvocation (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1851:44)
#13     InferenceVisitorBase.inferInvocation (package:front_end/src/fasta/type_inference/inference_visitor_base.dart:1542:12)
#14     InferenceVisitorImpl.visitStaticInvocation (package:front_end/src/fasta/type_inference/inference_visitor.dart:8060:40)
#15     StaticInvocation.accept1 (package:kernel/ast.dart:6403:9)
#16     InferenceVisitorImpl._inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:269:27)
#17     InferenceVisitorImpl.inferExpression (package:front_end/src/fasta/type_inference/inference_visitor.dart:301:40)
#18     InferenceVisitorImpl.inferForInIterable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1515:9)
#19     InferenceVisitorImpl._handlePatternForIn (package:front_end/src/fasta/type_inference/inference_visitor.dart:1641:9)
#20     InferenceVisitorImpl.handleForInWithoutVariable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1665:14)
#21     InferenceVisitorImpl.visitForInStatementWithSynthesizedVariable (package:front_end/src/fasta/type_inference/inference_visitor.dart:1705:26)
#22     ForInStatementWithSynthesizedVariable.acceptInference (package:front_end/src/fasta/kernel/internal_ast.dart:266:20)
#23     InferenceVisitorImpl.inferStatement (package:front_end/src/fasta/type_inference/inference_visitor.dart:250:26)

@lrhn lrhn added web-dart2js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels Aug 18, 2023
@ebraminio
Copy link
Author

ebraminio commented Aug 18, 2023

Just to note I saw the bug originally in flutter but gave dartpad link just for easier reproduction.

@sigmundch sigmundch added area-front-end Use area-front-end for front end / CFE / kernel format related issues. and removed web-dart2js area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels Aug 18, 2023
@sigmundch
Copy link
Member

Thanks so much for the very nice reproduction! Indeed, this is a bug in the CFE's type inference so I've triaged it that way. The error also occurs on the standalone Dart VM.

@chloestefantsova
Copy link
Contributor

I believe this was fixed in 47420c0

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants
@chloestefantsova @ebraminio @sigmundch @lrhn and others