Skip to content

Commit e6d5934

Browse files
johnniwintherCommit Queue
authored and
Commit Queue
committed
[cfe] Build PatternVariableDeclaration in BodyBuilder
Change-Id: Ic55dbc03bf2cf4865275b186266c0f420263d406 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273744 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent e44cece commit e6d5934

File tree

157 files changed

+1331
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+1331
-18
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8490,6 +8490,27 @@ class BodyBuilder extends StackListenerImpl
84908490
push(pattern);
84918491
}
84928492
}
8493+
8494+
@override
8495+
void handlePatternVariableDeclarationStatement(
8496+
Token keyword, Token equals, Token semicolon) {
8497+
debugEvent('PatternVariableDeclarationStatement');
8498+
assert(checkState(keyword, [
8499+
unionOfKinds([
8500+
ValueKinds.Expression,
8501+
ValueKinds.Generator,
8502+
ValueKinds.ProblemBuilder,
8503+
]),
8504+
ValueKinds.Pattern,
8505+
ValueKinds.AnnotationListOrNull,
8506+
]));
8507+
Expression initializer = popForValue();
8508+
Pattern pattern = toPattern(pop());
8509+
// TODO(johnniwinther,cstefantsova): Handle metadata.
8510+
pop(NullValue.Metadata) as List<Expression>?;
8511+
push(new PatternVariableDeclaration(pattern, initializer,
8512+
offset: keyword.charOffset, isFinal: keyword.lexeme == 'final'));
8513+
}
84938514
}
84948515

84958516
abstract class EnsureLoaded {

pkg/front_end/lib/src/fasta/kernel/internal_ast.dart

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4742,38 +4742,29 @@ class WildcardPattern extends Pattern {
47424742
class PatternVariableDeclaration extends InternalStatement {
47434743
final Pattern pattern;
47444744
final Expression initializer;
4745+
final bool isFinal;
47454746

47464747
PatternVariableDeclaration(this.pattern, this.initializer,
4747-
{required int offset}) {
4748+
{required int offset, required this.isFinal}) {
47484749
fileOffset = offset;
47494750
}
47504751

47514752
@override
47524753
StatementInferenceResult acceptInference(InferenceVisitorImpl visitor) {
4753-
throw new UnimplementedError("PatternVariableDeclaration.acceptInference");
4754-
}
4755-
4756-
@override
4757-
R accept<R>(StatementVisitor<R> visitor) {
4758-
if (visitor is Printer || visitor is Precedence || visitor is Transformer) {
4759-
// Allow visitors needed for toString and replaceWith.
4760-
return visitor.defaultStatement(this);
4761-
}
4762-
return unsupported(
4763-
"${runtimeType}.accept on ${visitor.runtimeType}", -1, null);
4764-
}
4765-
4766-
@override
4767-
R accept1<R, A>(StatementVisitor1<R, A> visitor, A arg) {
4768-
return unsupported(
4769-
"${runtimeType}.accept1 on ${visitor.runtimeType}", -1, null);
4754+
return visitor.visitPatternVariableDeclaration(this);
47704755
}
47714756

47724757
@override
47734758
void toTextInternal(AstPrinter printer) {
4759+
if (isFinal) {
4760+
printer.write('final ');
4761+
} else {
4762+
printer.write('var ');
4763+
}
47744764
pattern.toTextInternal(printer);
47754765
printer.write(" = ");
47764766
printer.writeExpression(initializer);
4767+
printer.write(';');
47774768
}
47784769

47794770
@override

pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8103,6 +8103,12 @@ class InferenceVisitorImpl extends InferenceVisitorBase
81038103
return const StatementInferenceResult();
81048104
}
81058105

8106+
StatementInferenceResult visitPatternVariableDeclaration(
8107+
PatternVariableDeclaration node) {
8108+
return new StatementInferenceResult.single(
8109+
new ExpressionStatement(new InvalidExpression('$node')));
8110+
}
8111+
81068112
@override
81078113
ExpressionInferenceResult visitVariableGet(
81088114
covariant VariableGetImpl node, DartType typeContext) {

pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ void main() {
129129
_testIfCaseStatement();
130130
_testPatternSwitchStatement();
131131
_testSwitchExpression();
132+
_testPatternVariableDeclaration();
132133
});
133134
}
134135

@@ -1312,3 +1313,19 @@ if (0 case 1 when 2) return;''');
13121313
'''
13131314
if (0 case 1 when 2) return 3; else return 4;''');
13141315
}
1316+
1317+
void _testPatternVariableDeclaration() {
1318+
testStatement(
1319+
new PatternVariableDeclaration(
1320+
new ExpressionPattern(new IntLiteral(0)), new IntLiteral(1),
1321+
isFinal: false, offset: TreeNode.noOffset),
1322+
'''
1323+
var 0 = 1;''');
1324+
1325+
testStatement(
1326+
new PatternVariableDeclaration(
1327+
new ExpressionPattern(new IntLiteral(0)), new IntLiteral(1),
1328+
isFinal: true, offset: TreeNode.noOffset),
1329+
'''
1330+
final 0 = 1;''');
1331+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2022, 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+
f(x) {
6+
late var (_) = x;
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected an identifier, but got '('.
6+
// Try inserting an identifier before '('.
7+
// late var (_) = x;
8+
// ^
9+
//
10+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected ';' after this.
11+
// late var (_) = x;
12+
// ^
13+
//
14+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Expected ';' after this.
15+
// late var (_) = x;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Undefined name '_'.
19+
// late var (_) = x;
20+
// ^
21+
//
22+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Expected an identifier, but got ')'.
23+
// Try inserting an identifier before ')'.
24+
// late var (_) = x;
25+
// ^
26+
//
27+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Unexpected token ';'.
28+
// late var (_) = x;
29+
// ^
30+
//
31+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:16: Error: Expected an identifier, but got '='.
32+
// Try inserting an identifier before '='.
33+
// late var (_) = x;
34+
// ^
35+
//
36+
import self as self;
37+
38+
static method f(dynamic x) → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected an identifier, but got '('.
6+
// Try inserting an identifier before '('.
7+
// late var (_) = x;
8+
// ^
9+
//
10+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected ';' after this.
11+
// late var (_) = x;
12+
// ^
13+
//
14+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Expected ';' after this.
15+
// late var (_) = x;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Undefined name '_'.
19+
// late var (_) = x;
20+
// ^
21+
//
22+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Expected an identifier, but got ')'.
23+
// Try inserting an identifier before ')'.
24+
// late var (_) = x;
25+
// ^
26+
//
27+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Unexpected token ';'.
28+
// late var (_) = x;
29+
// ^
30+
//
31+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:16: Error: Expected an identifier, but got '='.
32+
// Try inserting an identifier before '='.
33+
// late var (_) = x;
34+
// ^
35+
//
36+
import self as self;
37+
38+
static method f(dynamic x) → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(x) {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected an identifier, but got '('.
6+
// Try inserting an identifier before '('.
7+
// late var (_) = x;
8+
// ^
9+
//
10+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected ';' after this.
11+
// late var (_) = x;
12+
// ^
13+
//
14+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Expected ';' after this.
15+
// late var (_) = x;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Undefined name '_'.
19+
// late var (_) = x;
20+
// ^
21+
//
22+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Expected an identifier, but got ')'.
23+
// Try inserting an identifier before ')'.
24+
// late var (_) = x;
25+
// ^
26+
//
27+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Unexpected token ';'.
28+
// late var (_) = x;
29+
// ^
30+
//
31+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:16: Error: Expected an identifier, but got '='.
32+
// Try inserting an identifier before '='.
33+
// late var (_) = x;
34+
// ^
35+
//
36+
import self as self;
37+
38+
static method f(dynamic x) → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected an identifier, but got '('.
6+
// Try inserting an identifier before '('.
7+
// late var (_) = x;
8+
// ^
9+
//
10+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected ';' after this.
11+
// late var (_) = x;
12+
// ^
13+
//
14+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Expected ';' after this.
15+
// late var (_) = x;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Undefined name '_'.
19+
// late var (_) = x;
20+
// ^
21+
//
22+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Expected an identifier, but got ')'.
23+
// Try inserting an identifier before ')'.
24+
// late var (_) = x;
25+
// ^
26+
//
27+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Unexpected token ';'.
28+
// late var (_) = x;
29+
// ^
30+
//
31+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:16: Error: Expected an identifier, but got '='.
32+
// Try inserting an identifier before '='.
33+
// late var (_) = x;
34+
// ^
35+
//
36+
import self as self;
37+
38+
static method f(dynamic x) → dynamic {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic
5+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected an identifier, but got '('.
6+
// Try inserting an identifier before '('.
7+
// late var (_) = x;
8+
// ^
9+
//
10+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:12: Error: Expected ';' after this.
11+
// late var (_) = x;
12+
// ^
13+
//
14+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Expected ';' after this.
15+
// late var (_) = x;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:13: Error: Undefined name '_'.
19+
// late var (_) = x;
20+
// ^
21+
//
22+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Expected an identifier, but got ')'.
23+
// Try inserting an identifier before ')'.
24+
// late var (_) = x;
25+
// ^
26+
//
27+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:14: Error: Unexpected token ';'.
28+
// late var (_) = x;
29+
// ^
30+
//
31+
// pkg/front_end/testcases/patterns/patternVariableDeclarationStatement_disallowsLate.dart:6:16: Error: Expected an identifier, but got '='.
32+
// Try inserting an identifier before '='.
33+
// late var (_) = x;
34+
// ^
35+
//
36+
import self as self;
37+
38+
static method f(dynamic x) → dynamic {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2022, 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+
f(x) {
6+
final C(f: a) = x;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(final <dummy-pattern> = x;)";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(final <dummy-pattern> = x;)";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(final <dummy-pattern> = x;)";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(final <dummy-pattern> = x;)";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic
5+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(final <dummy-pattern> = x;)";
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2022, 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+
f(x) {
6+
var C(f: a) = x;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → dynamic {
5+
invalid-expression "PatternVariableDeclaration(var <dummy-pattern> = x;)";
6+
}

0 commit comments

Comments
 (0)