Skip to content

Commit 2e1dfef

Browse files
chloestefantsovaCommit Queue
authored and
Commit Queue
committed
[cfe] Account for non-pattern case heads when joining variables
Part of #49749 Change-Id: Ib7530e476c09402405d2763f1f7c29b2f29c3bdb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/286461 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 1c08a41 commit 2e1dfef

10 files changed

+200
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7830,6 +7830,10 @@ class BodyBuilder extends StackListenerImpl
78307830
}
78317831
jointPatternVariables = sharedVariables;
78327832
}
7833+
} else {
7834+
// It's a non-pattern head, so no variables can be joined.
7835+
jointPatternVariables = null;
7836+
break;
78337837
}
78347838
}
78357839
if (jointPatternVariables != null) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2023, 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+
test(dynamic x) {
6+
switch (x) {
7+
case final a when a > 0:
8+
case 0:
9+
case final a:
10+
return a; // Error.
11+
default:
12+
return null;
13+
}
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
6+
// return a; // Error.
7+
// ^
8+
//
9+
import self as self;
10+
import "dart:core" as core;
11+
12+
static method test(dynamic x) → dynamic {
13+
#L1:
14+
{
15+
final dynamic a;
16+
final dynamic a#1;
17+
final dynamic #0#0 = x;
18+
if((let final dynamic #t1 = a = #0#0 in true) && a{dynamic}.>(0) || #C1 =={core::num::==}{(core::Object) → core::bool} #0#0 || (let final dynamic #t2 = a#1 = #0#0 in true)) {
19+
{
20+
return invalid-expression "pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
21+
return a; // Error.
22+
^";
23+
}
24+
}
25+
else {
26+
{
27+
return null;
28+
}
29+
}
30+
}
31+
}
32+
33+
constants {
34+
#C1 = 0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
6+
// return a; // Error.
7+
// ^
8+
//
9+
import self as self;
10+
import "dart:core" as core;
11+
12+
static method test(dynamic x) → dynamic {
13+
#L1:
14+
{
15+
final dynamic a;
16+
final dynamic a#1;
17+
final dynamic #0#0 = x;
18+
if((let final dynamic #t1 = a = #0#0 in true) && a{dynamic}.>(0) || #C1 =={core::num::==}{(core::Object) → core::bool} #0#0 || (let final dynamic #t2 = a#1 = #0#0 in true)) {
19+
{
20+
return invalid-expression "pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
21+
return a; // Error.
22+
^";
23+
}
24+
}
25+
else {
26+
{
27+
return null;
28+
}
29+
}
30+
}
31+
}
32+
33+
constants {
34+
#C1 = 0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test(dynamic x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test(dynamic x) {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
6+
// return a; // Error.
7+
// ^
8+
//
9+
import self as self;
10+
import "dart:core" as core;
11+
12+
static method test(dynamic x) → dynamic {
13+
#L1:
14+
{
15+
final dynamic a;
16+
final dynamic a#1;
17+
final dynamic #0#0 = x;
18+
if((let final dynamic #t1 = a = #0#0 in true) && a{dynamic}.>(0) || #C1 =={core::num::==}{(core::Object) → core::bool} #0#0 || (let final dynamic #t2 = a#1 = #0#0 in true)) {
19+
{
20+
return invalid-expression "pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
21+
return a; // Error.
22+
^";
23+
}
24+
}
25+
else {
26+
{
27+
return null;
28+
}
29+
}
30+
}
31+
}
32+
33+
constants {
34+
#C1 = 0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
6+
// return a; // Error.
7+
// ^
8+
//
9+
import self as self;
10+
import "dart:core" as core;
11+
12+
static method test(dynamic x) → dynamic {
13+
#L1:
14+
{
15+
final dynamic a;
16+
final dynamic a#1;
17+
final dynamic #0#0 = x;
18+
if((let final dynamic #t1 = a = #0#0 in true) && a{dynamic}.>(0) || #C1 =={core::num::==}{(core::Object) → core::bool} #0#0 || (let final dynamic #t2 = a#1 = #0#0 in true)) {
19+
{
20+
return invalid-expression "pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
21+
return a; // Error.
22+
^";
23+
}
24+
}
25+
else {
26+
{
27+
return null;
28+
}
29+
}
30+
}
31+
}
32+
33+
constants {
34+
#C1 = 0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method test(dynamic x) → dynamic
5+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library /*isNonNullableByDefault*/;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
6+
// return a; // Error.
7+
// ^
8+
//
9+
import self as self;
10+
import "dart:core" as core;
11+
12+
static method test(dynamic x) → dynamic {
13+
#L1:
14+
{
15+
final dynamic a;
16+
final dynamic a#1;
17+
final dynamic #0#0 = x;
18+
if((let final dynamic #t1 = a = #0#0 in true) && a{dynamic}.>(0) || #C1 =={core::num::==}{(core::Object) → core::bool} #0#0 || (let final dynamic #t2 = a#1 = #0#0 in true)) {
19+
{
20+
return invalid-expression "pkg/front_end/testcases/patterns/non_pattern_case_among_pattern_cases_in_switch.dart:10:14: Error: Undefined name 'a'.
21+
return a; // Error.
22+
^";
23+
}
24+
}
25+
else {
26+
{
27+
return null;
28+
}
29+
}
30+
}
31+
}
32+
33+
constants {
34+
#C1 = 0
35+
}

0 commit comments

Comments
 (0)