Skip to content

Commit 4036b35

Browse files
johnniwintherCommit Queue
authored and
Commit Queue
committed
[cfe] Only use inferred constant context for <3.0 switch cases
When patterns are enabled, the case expression is not an inferred constant context. Closes #51898 Change-Id: I6b5a68b8de587c53a329756b6ce7caa6b9235fef Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292081 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 7196bb2 commit 4036b35

15 files changed

+137
-61
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,9 @@ class BodyBuilder extends StackListenerImpl
25812581

25822582
createAndEnterLocalScope(debugName: "case-head", kind: ScopeKind.caseHead);
25832583
super.push(constantContext);
2584-
constantContext = ConstantContext.inferred;
2584+
if (!libraryFeatures.patterns.isEnabled) {
2585+
constantContext = ConstantContext.inferred;
2586+
}
25852587
assert(checkState(
25862588
caseKeyword, [ValueKinds.ConstantContext, ValueKinds.Scope]));
25872589
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
void main() {
6+
int d = 1;
7+
var x = false;
8+
switch (d) {
9+
case 1 when x = true:
10+
print('OK!');
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method main() → void {
6+
core::int d = 1;
7+
core::bool x = false;
8+
#L1:
9+
{
10+
final synthesized core::int #0#0 = d;
11+
{
12+
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0 && (x = true)) {
13+
{
14+
core::print("OK!");
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
constants {
22+
#C1 = 1
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method main() → void {
6+
core::int d = 1;
7+
core::bool x = false;
8+
#L1:
9+
{
10+
final synthesized core::int #0#0 = d;
11+
{
12+
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0 && (x = true)) {
13+
{
14+
core::print("OK!");
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
constants {
22+
#C1 = 1
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method main() → void {
6+
core::int d = 1;
7+
core::bool x = false;
8+
#L1:
9+
{
10+
final synthesized core::int #0#0 = d;
11+
{
12+
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0 && (x = true)) {
13+
{
14+
core::print("OK!");
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
constants {
22+
#C1 = 1
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method main() → void {
6+
core::int d = 1;
7+
core::bool x = false;
8+
#L1:
9+
{
10+
final synthesized core::int #0#0 = d;
11+
{
12+
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0 && (x = true)) {
13+
{
14+
core::print("OK!");
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
constants {
22+
#C1 = 1
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
4+
static method main() → void
5+
;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method main() → void {
6+
core::int d = 1;
7+
core::bool x = false;
8+
#L1:
9+
{
10+
final synthesized core::int #0#0 = d;
11+
{
12+
if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0 && (x = true)) {
13+
{
14+
core::print("OK!");
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
constants {
22+
#C1 = 1
23+
}

pkg/front_end/testcases/patterns/non_type_object_pattern.dart.strong.expect

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ library /*isNonNullableByDefault*/;
4848
// if (o case NonType(:var field)) {}
4949
// ^^^^^^^
5050
//
51-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: Not a constant expression.
52-
// case NonType(:var field):
53-
// ^^^^^^^
54-
//
5551
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: 'NonType' isn't a type.
5652
// case NonType(:var field):
5753
// ^^^^^^^
@@ -138,10 +134,6 @@ library /*isNonNullableByDefault*/;
138134
// if (o case prefix.NonType(:var field)) {}
139135
// ^^^^^^^
140136
//
141-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: Not a constant expression.
142-
// case prefix.NonType(:var field):
143-
// ^^^^^^^
144-
//
145137
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: 'NonType' isn't a type.
146138
// case prefix.NonType(:var field):
147139
// ^^^^^^^
@@ -228,10 +220,6 @@ library /*isNonNullableByDefault*/;
228220
// if (o case NonType.hashCode(:var field)) {}
229221
// ^^^^^^^^^^^^^^^^
230222
//
231-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: Not a constant expression.
232-
// case NonType.hashCode(:var field):
233-
// ^^^^^^^
234-
//
235223
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: 'NonType.hashCode' can't be used as a type because 'NonType' doesn't refer to an import prefix.
236224
// case NonType.hashCode(:var field):
237225
// ^^^^^^^^^^^^^^^^

pkg/front_end/testcases/patterns/non_type_object_pattern.dart.strong.transformed.expect

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ library /*isNonNullableByDefault*/;
4848
// if (o case NonType(:var field)) {}
4949
// ^^^^^^^
5050
//
51-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: Not a constant expression.
52-
// case NonType(:var field):
53-
// ^^^^^^^
54-
//
5551
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: 'NonType' isn't a type.
5652
// case NonType(:var field):
5753
// ^^^^^^^
@@ -138,10 +134,6 @@ library /*isNonNullableByDefault*/;
138134
// if (o case prefix.NonType(:var field)) {}
139135
// ^^^^^^^
140136
//
141-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: Not a constant expression.
142-
// case prefix.NonType(:var field):
143-
// ^^^^^^^
144-
//
145137
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: 'NonType' isn't a type.
146138
// case prefix.NonType(:var field):
147139
// ^^^^^^^
@@ -228,10 +220,6 @@ library /*isNonNullableByDefault*/;
228220
// if (o case NonType.hashCode(:var field)) {}
229221
// ^^^^^^^^^^^^^^^^
230222
//
231-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: Not a constant expression.
232-
// case NonType.hashCode(:var field):
233-
// ^^^^^^^
234-
//
235223
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: 'NonType.hashCode' can't be used as a type because 'NonType' doesn't refer to an import prefix.
236224
// case NonType.hashCode(:var field):
237225
// ^^^^^^^^^^^^^^^^

pkg/front_end/testcases/patterns/non_type_object_pattern.dart.weak.expect

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ library /*isNonNullableByDefault*/;
4848
// if (o case NonType(:var field)) {}
4949
// ^^^^^^^
5050
//
51-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: Not a constant expression.
52-
// case NonType(:var field):
53-
// ^^^^^^^
54-
//
5551
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: 'NonType' isn't a type.
5652
// case NonType(:var field):
5753
// ^^^^^^^
@@ -138,10 +134,6 @@ library /*isNonNullableByDefault*/;
138134
// if (o case prefix.NonType(:var field)) {}
139135
// ^^^^^^^
140136
//
141-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: Not a constant expression.
142-
// case prefix.NonType(:var field):
143-
// ^^^^^^^
144-
//
145137
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: 'NonType' isn't a type.
146138
// case prefix.NonType(:var field):
147139
// ^^^^^^^
@@ -228,10 +220,6 @@ library /*isNonNullableByDefault*/;
228220
// if (o case NonType.hashCode(:var field)) {}
229221
// ^^^^^^^^^^^^^^^^
230222
//
231-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: Not a constant expression.
232-
// case NonType.hashCode(:var field):
233-
// ^^^^^^^
234-
//
235223
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: 'NonType.hashCode' can't be used as a type because 'NonType' doesn't refer to an import prefix.
236224
// case NonType.hashCode(:var field):
237225
// ^^^^^^^^^^^^^^^^

pkg/front_end/testcases/patterns/non_type_object_pattern.dart.weak.modular.expect

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ library /*isNonNullableByDefault*/;
4848
// if (o case NonType(:var field)) {}
4949
// ^^^^^^^
5050
//
51-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: Not a constant expression.
52-
// case NonType(:var field):
53-
// ^^^^^^^
54-
//
5551
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: 'NonType' isn't a type.
5652
// case NonType(:var field):
5753
// ^^^^^^^
@@ -138,10 +134,6 @@ library /*isNonNullableByDefault*/;
138134
// if (o case prefix.NonType(:var field)) {}
139135
// ^^^^^^^
140136
//
141-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: Not a constant expression.
142-
// case prefix.NonType(:var field):
143-
// ^^^^^^^
144-
//
145137
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: 'NonType' isn't a type.
146138
// case prefix.NonType(:var field):
147139
// ^^^^^^^
@@ -228,10 +220,6 @@ library /*isNonNullableByDefault*/;
228220
// if (o case NonType.hashCode(:var field)) {}
229221
// ^^^^^^^^^^^^^^^^
230222
//
231-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: Not a constant expression.
232-
// case NonType.hashCode(:var field):
233-
// ^^^^^^^
234-
//
235223
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: 'NonType.hashCode' can't be used as a type because 'NonType' doesn't refer to an import prefix.
236224
// case NonType.hashCode(:var field):
237225
// ^^^^^^^^^^^^^^^^

pkg/front_end/testcases/patterns/non_type_object_pattern.dart.weak.transformed.expect

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ library /*isNonNullableByDefault*/;
4848
// if (o case NonType(:var field)) {}
4949
// ^^^^^^^
5050
//
51-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: Not a constant expression.
52-
// case NonType(:var field):
53-
// ^^^^^^^
54-
//
5551
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:34:10: Error: 'NonType' isn't a type.
5652
// case NonType(:var field):
5753
// ^^^^^^^
@@ -138,10 +134,6 @@ library /*isNonNullableByDefault*/;
138134
// if (o case prefix.NonType(:var field)) {}
139135
// ^^^^^^^
140136
//
141-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: Not a constant expression.
142-
// case prefix.NonType(:var field):
143-
// ^^^^^^^
144-
//
145137
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:82:17: Error: 'NonType' isn't a type.
146138
// case prefix.NonType(:var field):
147139
// ^^^^^^^
@@ -228,10 +220,6 @@ library /*isNonNullableByDefault*/;
228220
// if (o case NonType.hashCode(:var field)) {}
229221
// ^^^^^^^^^^^^^^^^
230222
//
231-
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: Not a constant expression.
232-
// case NonType.hashCode(:var field):
233-
// ^^^^^^^
234-
//
235223
// pkg/front_end/testcases/patterns/non_type_object_pattern.dart:130:10: Error: 'NonType.hashCode' can't be used as a type because 'NonType' doesn't refer to an import prefix.
236224
// case NonType.hashCode(:var field):
237225
// ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)