Skip to content

Commit 37b03ee

Browse files
jensjohaCommit Queue
authored andcommitted
[CFE] Fix crash in labeled statement that gets a parser recovry
Change-Id: If23b21a94401fb76be20b81cbeaf97b563fc497c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397903 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent 5430e68 commit 37b03ee

16 files changed

+135
-6
lines changed

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,32 @@ class BodyBuilder extends StackListenerImpl
700700
Object? element = pop();
701701
if (element is Statement) {
702702
return forest.wrapVariables(element);
703-
} else if (element is ParserRecovery) {
703+
} else {
704+
return _handleStatementNotStatement(element, token);
705+
}
706+
}
707+
708+
Statement _handleStatementNotStatement(Object? element, Token? token) {
709+
if (element is ParserRecovery) {
704710
return new Block(<Statement>[
705711
forest.createExpressionStatement(
706712
element.charOffset,
707-
new ParserErrorGenerator(this, token, cfe.messageSyntheticToken)
708-
.buildProblem())
713+
ParserErrorGenerator.buildProblemExpression(
714+
this, cfe.messageSyntheticToken, element.charOffset))
709715
])
710716
..fileOffset = element.charOffset;
711717
} else {
712-
unhandled("expected statement is ${element.runtimeType}", "popStatement",
713-
token.charOffset, uri);
718+
unhandled("expected statement is ${element.runtimeType}: $element",
719+
"popStatement", token?.charOffset ?? -1, uri);
720+
}
721+
}
722+
723+
Statement popStatementNoWrap([Token? token]) {
724+
Object? element = pop();
725+
if (element is Statement) {
726+
return element;
727+
} else {
728+
return _handleStatementNotStatement(element, token);
714729
}
715730
}
716731

@@ -7886,7 +7901,7 @@ class BodyBuilder extends StackListenerImpl
78867901
@override
78877902
void endLabeledStatement(int labelCount) {
78887903
debugEvent("LabeledStatement");
7889-
Statement statement = pop() as Statement;
7904+
Statement statement = popStatementNoWrap();
78907905
LabelTarget target = pop() as LabelTarget;
78917906
_labelScopes.pop();
78927907
// TODO(johnniwinther): Split the handling of breaks and continue.

pkg/front_end/lib/src/kernel/expression_generator.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,6 +4350,11 @@ class ParserErrorGenerator extends Generator {
43504350
void printOn(StringSink sink) {}
43514351

43524352
Expression buildProblem() {
4353+
return buildProblemExpression(_helper, message, fileOffset);
4354+
}
4355+
4356+
static Expression buildProblemExpression(
4357+
ExpressionGeneratorHelper _helper, Message message, int fileOffset) {
43534358
return _helper.buildProblem(message, fileOffset, noLength,
43544359
suppressMessage: true);
43554360
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main() {
2+
L:
3+
var x, y;
4+
x = 42;
5+
y = x;
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
4+
static method main() → dynamic {
5+
dynamic x;
6+
dynamic y;
7+
x = 42;
8+
y = x;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
4+
static method main() → dynamic {
5+
dynamic x;
6+
dynamic y;
7+
x = 42;
8+
y = x;
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library;
2+
import self as self;
3+
4+
static method main() → dynamic
5+
;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
4+
static method main() → dynamic {
5+
dynamic x;
6+
dynamic y;
7+
x = 42;
8+
y = x;
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
void foo() {
6+
o:
7+
int bar, ;
8+
}

0 commit comments

Comments
 (0)