Skip to content

Crash with non-identifier expression in for (... in ...) #60358

Open
@lrhn

Description

@lrhn

Example:

void main() {
  var b = <int>[0];
  var ints = <dynamic>[1, 2, 3, 4];
  for (b[0] in ints) {
    print(b[0]);
  }
}

The b[0] is an assignable expression, but not an identifier.

The language grammar only allows a single identifier (or declaration) in that position, not a general expression, assignable or not.
(Could potentially allow any assignment pattern, possibly even any assignable expression, as long as it doesn't raise concerns about repeated evaluation.)

The analyzer reports "Unexpected character '['", which is correct. (It has a follow-up error if the ints array was typed as List<int>, saying that you can't assign int to List<int>, so it probably error-recovers to an assignment to b.)

The front-end crashes spectacularly in type inferece (DartPad, main branch):

compileNewDDC
Crash when compiling file:///tmp/dartpadSSKDCZ/lib/main.dart at character offset 10:
main.dart: Internal problem: Unhandled IndexSet in handleForInStatementWithoutVariable.

#0      internalProblem (package:front_end/src/base/problems.dart:50:5)
#1      unhandled (package:front_end/src/base/problems.dart:71:10)
#2      InferenceVisitorImpl.computeForInVariable (package:front_end/src/type_inference/inference_visitor.dart:1714:23)
#3      InferenceVisitorImpl._handleForInWithoutVariable (package:front_end/src/type_inference/inference_visitor.dart:1731:9)
#4      InferenceVisitorImpl.handleForInWithoutVariable (package:front_end/src/type_inference/inference_visitor.dart:1843:14)
#5      InferenceVisitorImpl.visitForInStatementWithSynthesizedVariable (package:front_end/src/type_inference/inference_visitor.dart:1880:26)
#6      ForInStatementWithSynthesizedVariable.acceptInference (package:front_end/src/kernel/internal_ast.dart:234:20)
...

Should have had a parsing error before that when the part before in was not a single identifier.

Metadata

Metadata

Labels

P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-crashesCrashes in the analyzer or CFEtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions