Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 142bf69

Browse files
johnniwintherCommit Queue
authored and
Commit Queue
committed
[cfe] Pass on guard in if-case statement
Change-Id: Ic7c201bd90b2cf67a2b7526fc4ac035b21f34cc6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265720 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 7e87efd commit 142bf69

File tree

40 files changed

+331
-8
lines changed

40 files changed

+331
-8
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,7 +2317,7 @@ class BodyBuilder extends StackListenerImpl
23172317
libraryFeatures.patterns, case_.charOffset, case_.charCount);
23182318
Matcher matcher = toMatcher(pop());
23192319
Expression expression = popForValue();
2320-
push(new Condition(expression, matcher));
2320+
push(new Condition(expression, matcher, guard));
23212321
} else {
23222322
assert(checkState(token, [
23232323
unionOfKinds([
@@ -3495,11 +3495,12 @@ class BodyBuilder extends StackListenerImpl
34953495
Statement thenPart = popStatement();
34963496
Condition condition = pop() as Condition;
34973497
Matcher? matcher = condition.matcher;
3498+
Expression? guard = condition.guard;
34983499
Expression expression = condition.expression;
34993500
Statement node;
35003501
if (matcher != null) {
35013502
node = new IfCaseStatement(
3502-
expression, matcher, thenPart, elsePart, ifToken.charOffset);
3503+
expression, matcher, guard, thenPart, elsePart, ifToken.charOffset);
35033504
} else {
35043505
node = forest.createIfStatement(
35053506
offsetForToken(ifToken), expression, thenPart, elsePart);
@@ -8809,10 +8810,14 @@ class _FindChildVisitor extends Visitor<void> with VisitorVoidMixin {
88098810
class Condition {
88108811
final Expression expression;
88118812
final Matcher? matcher;
8813+
final Expression? guard;
88128814

8813-
Condition(this.expression, [this.matcher]);
8815+
Condition(this.expression, [this.matcher, this.guard])
8816+
: assert(guard == null || matcher != null,
8817+
"Unexpected guard without matcher.");
88148818

88158819
@override
8816-
String toString() =>
8817-
'Condition($expression${matcher != null ? ',$matcher' : ''})';
8820+
String toString() => 'Condition($expression'
8821+
'${matcher != null ? ',$matcher' : ''}'
8822+
'${guard != null ? ',$guard' : ''})';
88188823
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5461,17 +5461,26 @@ class PatternVariableDeclaration extends Statement {
54615461

54625462
final Matcher dummyMatcher = new ExpressionMatcher(dummyExpression);
54635463

5464+
/// Internal statement for a if-case statements:
5465+
///
5466+
/// if (expression case matcher) then
5467+
/// if (expression case matcher) then else otherwise
5468+
/// if (expression case matcher when guard) then
5469+
/// if (expression case matcher when guard) then else otherwise
5470+
///
54645471
class IfCaseStatement extends InternalStatement {
54655472
Expression expression;
54665473
Matcher matcher;
5474+
Expression? guard;
54675475
Statement then;
54685476
Statement? otherwise;
54695477

5470-
IfCaseStatement(this.expression, this.matcher, this.then, this.otherwise,
5471-
int fileOffset) {
5478+
IfCaseStatement(this.expression, this.matcher, this.guard, this.then,
5479+
this.otherwise, int fileOffset) {
54725480
this.fileOffset = fileOffset;
54735481
expression.parent = this;
54745482
matcher.parent = this;
5483+
guard?.parent = this;
54755484
then.parent = this;
54765485
otherwise?.parent = this;
54775486
}
@@ -5487,6 +5496,10 @@ class IfCaseStatement extends InternalStatement {
54875496
printer.writeExpression(expression);
54885497
printer.write(' case ');
54895498
matcher.toTextInternal(printer);
5499+
if (guard != null) {
5500+
printer.write(' when ');
5501+
printer.writeExpression(guard!);
5502+
}
54905503
printer.write(') ');
54915504
printer.writeStatement(then);
54925505
if (otherwise != null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ class InferenceVisitorImpl extends InferenceVisitorBase
18021802
}
18031803

18041804
StatementInferenceResult visitIfCaseStatement(IfCaseStatement node) {
1805-
// TODO(cstefantsova): Handle if-case statements.
1805+
// TODO(cstefantsova): Handle if-case-when statements.
18061806
return new StatementInferenceResult.single(
18071807
new EmptyStatement()..fileOffset = node.fileOffset);
18081808
}

pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ void _testIfCaseStatement() {
11441144
new IfCaseStatement(
11451145
new IntLiteral(0),
11461146
new ExpressionMatcher(new IntLiteral(1)),
1147+
null,
11471148
new ReturnStatement(),
11481149
null,
11491150
TreeNode.noOffset),
@@ -1154,9 +1155,32 @@ if (0 case 1) return;''');
11541155
new IfCaseStatement(
11551156
new IntLiteral(0),
11561157
new ExpressionMatcher(new IntLiteral(1)),
1158+
null,
11571159
new ReturnStatement(new IntLiteral(2)),
11581160
new ReturnStatement(new IntLiteral(3)),
11591161
TreeNode.noOffset),
11601162
'''
11611163
if (0 case 1) return 2; else return 3;''');
1164+
1165+
testStatement(
1166+
new IfCaseStatement(
1167+
new IntLiteral(0),
1168+
new ExpressionMatcher(new IntLiteral(1)),
1169+
new IntLiteral(2),
1170+
new ReturnStatement(),
1171+
null,
1172+
TreeNode.noOffset),
1173+
'''
1174+
if (0 case 1 when 2) return;''');
1175+
1176+
testStatement(
1177+
new IfCaseStatement(
1178+
new IntLiteral(0),
1179+
new ExpressionMatcher(new IntLiteral(1)),
1180+
new IntLiteral(2),
1181+
new ReturnStatement(new IntLiteral(3)),
1182+
new ReturnStatement(new IntLiteral(4)),
1183+
TreeNode.noOffset),
1184+
'''
1185+
if (0 case 1 when 2) return 3; else return 4;''');
11621186
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void f(x) {
2+
if (x case 0 when true) {}
3+
}
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) → void {
5+
;
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) → void {
5+
;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void 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) → void {
5+
;
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) → void {
5+
;
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) → void
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) → void {
5+
;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void f(x) {
2+
switch (x) {
3+
case 0 when true:
4+
break;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method f(dynamic x) → void {
6+
#L1:
7+
switch(x) {
8+
#L2:
9+
case #C1:
10+
{
11+
break #L1;
12+
}
13+
}
14+
}
15+
16+
constants {
17+
#C1 = 0
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method f(dynamic x) → void {
6+
#L1:
7+
switch(x) {
8+
#L2:
9+
case #C1:
10+
{
11+
break #L1;
12+
}
13+
}
14+
}
15+
16+
constants {
17+
#C1 = 0
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method f(dynamic x) → void {
6+
#L1:
7+
switch(x) {
8+
#L2:
9+
case #C1:
10+
{
11+
break #L1;
12+
}
13+
}
14+
}
15+
16+
constants {
17+
#C1 = 0
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method f(dynamic x) → void {
6+
#L1:
7+
switch(x) {
8+
#L2:
9+
case #C1:
10+
{
11+
break #L1;
12+
}
13+
}
14+
}
15+
16+
constants {
17+
#C1 = 0
18+
}
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) → void
5+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method f(dynamic x) → void {
6+
#L1:
7+
switch(x) {
8+
#L2:
9+
case #C1:
10+
{
11+
break #L1;
12+
}
13+
}
14+
}
15+
16+
constants {
17+
#C1 = 0
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void f(x) {
2+
if (x case 0 as int when true) {}
3+
}
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) → void {
5+
;
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) → void {
5+
;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Lines changed: 6 additions & 0 deletions
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) → void {
5+
;
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) → void {
5+
;
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) → void
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) → void {
5+
;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void f(x) {
2+
switch (x) {
3+
case 0 as int when true:
4+
break;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → void {
5+
#L1:
6+
switch(x) {
7+
#L2:
8+
case #C1:
9+
{
10+
break #L1;
11+
}
12+
}
13+
}
14+
15+
constants {
16+
#C1 = null
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method f(dynamic x) → void {
5+
#L1:
6+
switch(x) {
7+
#L2:
8+
case #C1:
9+
{
10+
break #L1;
11+
}
12+
}
13+
}
14+
15+
constants {
16+
#C1 = null
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void f(x) {}

0 commit comments

Comments
 (0)