Skip to content

Commit 23c0ff5

Browse files
chloestefantsovaCommit Queue
authored and
Commit Queue
committed
[cfe] Implement desugaring of switch expressions
Part of #49749 Closes #50814 Change-Id: Ie2524c7e5a1fc1a25f392eecbf2e444c89229923 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277685 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Aske Simon Christensen <[email protected]>
1 parent 7cc63e4 commit 23c0ff5

File tree

73 files changed

+2143
-659
lines changed

Some content is hidden

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

73 files changed

+2143
-659
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11317,6 +11317,14 @@ const MessageCode messageSwitchCaseFallThrough = const MessageCode(
1131711317
analyzerCodes: <String>["CASE_BLOCK_NOT_TERMINATED"],
1131811318
problemMessage: r"""Switch case may fall through to the next case.""");
1131911319

11320+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
11321+
const Code<Null> codeSwitchExpressionEmpty = messageSwitchExpressionEmpty;
11322+
11323+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
11324+
const MessageCode messageSwitchExpressionEmpty = const MessageCode(
11325+
"SwitchExpressionEmpty",
11326+
problemMessage: r"""Switch expressions must contain at least one case.""");
11327+
1132011328
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
1132111329
const Code<Null> codeSwitchExpressionNotAssignableCause =
1132211330
messageSwitchExpressionNotAssignableCause;

pkg/dart2wasm/lib/types.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,9 @@ class Types {
510510
// necessary to test the type arguments.
511511
Class cls = translator.classForType(operandType);
512512
InterfaceType? base = translator.hierarchy
513-
.getTypeAsInstanceOf(type, cls, codeGen.member.enclosingLibrary)
513+
.getTypeAsInstanceOf(type, cls,
514+
isNonNullableByDefault:
515+
codeGen.member.enclosingLibrary.isNonNullableByDefault)
514516
?.withDeclaredNullability(operandType.declaredNullability);
515517
if (base != operandType) {
516518
makeType(codeGen, type);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4145,7 +4145,7 @@ class BodyBuilder extends StackListenerImpl
41454145
patterns[i] = toPattern(pop());
41464146
}
41474147
List<TypeBuilder>? typeArguments = pop() as List<TypeBuilder>?;
4148-
DartType typeArgument;
4148+
DartType? typeArgument;
41494149
if (typeArguments != null) {
41504150
if (typeArguments.length > 1) {
41514151
addProblem(
@@ -4160,8 +4160,6 @@ class BodyBuilder extends StackListenerImpl
41604160
typeArgument = instantiateToBounds(
41614161
typeArgument, coreTypes.objectClass, libraryBuilder.library);
41624162
}
4163-
} else {
4164-
typeArgument = implicitTypeArgument;
41654163
}
41664164

41674165
push(new ListPattern(typeArgument, patterns, leftBracket.charOffset));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,9 @@ abstract class CombinedMemberSignatureBase<T> {
718718
return type;
719719
}
720720
InterfaceType? instance = hierarchy.getTypeAsInstanceOf(
721-
thisType, member.enclosingClass!, classBuilder.libraryBuilder.library);
721+
thisType, member.enclosingClass!,
722+
isNonNullableByDefault:
723+
classBuilder.libraryBuilder.isNonNullableByDefault);
722724
assert(
723725
instance != null,
724726
"No instance of $thisType as ${member.enclosingClass} found for "

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@ class Forest {
399399
// ignore: unnecessary_null_comparison
400400
assert(fileOffset != null);
401401
// TODO(johnniwinther): Use [label]?
402-
return new BreakStatementImpl(isContinue: false)..fileOffset = fileOffset;
402+
return new BreakStatementImpl(isContinue: false)
403+
..fileOffset = fileOffset
404+
..target = label is LabeledStatement ? label : dummyLabeledStatement;
403405
}
404406

405407
/// Return a representation of a catch clause.

pkg/front_end/lib/src/fasta/kernel/hierarchy/hierarchy_builder.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class ClassHierarchyBuilder implements ClassHierarchyBase {
100100
}
101101

102102
@override
103-
InterfaceType getTypeAsInstanceOf(
104-
InterfaceType type, Class superclass, Library clientLibrary) {
103+
InterfaceType getTypeAsInstanceOf(InterfaceType type, Class superclass,
104+
{required bool isNonNullableByDefault}) {
105105
if (type.classNode == superclass) return type;
106106
return asSupertypeOf(type, superclass)!
107107
.asInterfaceType
@@ -122,14 +122,15 @@ class ClassHierarchyBuilder implements ClassHierarchyBase {
122122

123123
@override
124124
InterfaceType getLegacyLeastUpperBound(
125-
InterfaceType type1, InterfaceType type2, Library clientLibrary) {
125+
InterfaceType type1, InterfaceType type2,
126+
{required bool isNonNullableByDefault}) {
126127
if (type1 == type2) return type1;
127128

128129
// LLUB(Null, List<dynamic>*) works differently for opt-in and opt-out
129130
// libraries. In opt-out libraries the legacy behavior is preserved, so
130131
// LLUB(Null, List<dynamic>*) = List<dynamic>*. In opt-out libraries the
131132
// rules imply that LLUB(Null, List<dynamic>*) = List<dynamic>?.
132-
if (!clientLibrary.isNonNullableByDefault) {
133+
if (!isNonNullableByDefault) {
133134
if (type1 is NullType) {
134135
return type2;
135136
}
@@ -153,10 +154,10 @@ class ClassHierarchyBuilder implements ClassHierarchyBase {
153154
continue;
154155
}
155156
if (nodes1.contains(node)) {
156-
DartType candidate1 =
157-
getTypeAsInstanceOf(type1, node.classBuilder.cls, clientLibrary);
158-
DartType candidate2 =
159-
getTypeAsInstanceOf(type2, node.classBuilder.cls, clientLibrary);
157+
DartType candidate1 = getTypeAsInstanceOf(type1, node.classBuilder.cls,
158+
isNonNullableByDefault: isNonNullableByDefault);
159+
DartType candidate2 = getTypeAsInstanceOf(type2, node.classBuilder.cls,
160+
isNonNullableByDefault: isNonNullableByDefault);
160161
if (candidate1 == candidate2) {
161162
common.add(node);
162163
}
@@ -172,7 +173,8 @@ class ClassHierarchyBuilder implements ClassHierarchyBase {
172173
for (int i = 0; i < common.length - 1; i++) {
173174
ClassHierarchyNode node = common[i];
174175
if (node.maxInheritancePath != common[i + 1].maxInheritancePath) {
175-
return getTypeAsInstanceOf(type1, node.classBuilder.cls, clientLibrary)
176+
return getTypeAsInstanceOf(type1, node.classBuilder.cls,
177+
isNonNullableByDefault: isNonNullableByDefault)
176178
.withDeclaredNullability(
177179
uniteNullabilities(type1.nullability, type2.nullability));
178180
} else {

pkg/front_end/lib/src/fasta/kernel/hierarchy/mixin_inferrer.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ class TypeBuilderConstraintGatherer extends TypeConstraintGatherer
5454
CoreTypes get coreTypes => hierarchy.coreTypes;
5555

5656
@override
57-
void addLowerBound(
58-
TypeConstraint constraint, DartType lower, Library clientLibrary) {
59-
constraint.lower =
60-
getStandardUpperBound(constraint.lower, lower, clientLibrary);
57+
void addLowerBound(TypeConstraint constraint, DartType lower,
58+
{required bool isNonNullableByDefault}) {
59+
constraint.lower = getStandardUpperBound(constraint.lower, lower,
60+
isNonNullableByDefault: isNonNullableByDefault);
6161
}
6262

6363
@override
64-
void addUpperBound(
65-
TypeConstraint constraint, DartType upper, Library clientLibrary) {
66-
constraint.upper =
67-
getStandardLowerBound(constraint.upper, upper, clientLibrary);
64+
void addUpperBound(TypeConstraint constraint, DartType upper,
65+
{required bool isNonNullableByDefault}) {
66+
constraint.upper = getStandardLowerBound(constraint.upper, upper,
67+
isNonNullableByDefault: isNonNullableByDefault);
6868
}
6969

7070
@override
@@ -75,7 +75,8 @@ class TypeBuilderConstraintGatherer extends TypeConstraintGatherer
7575
@override
7676
InterfaceType getTypeAsInstanceOf(InterfaceType type, Class superclass,
7777
Library clientLibrary, CoreTypes coreTypes) {
78-
return hierarchy.getTypeAsInstanceOf(type, superclass, clientLibrary);
78+
return hierarchy.getTypeAsInstanceOf(type, superclass,
79+
isNonNullableByDefault: clientLibrary.isNonNullableByDefault);
7980
}
8081

8182
@override

0 commit comments

Comments
 (0)