diff --git a/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart b/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart index a8705b11ac5..53435d95804 100644 --- a/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart +++ b/pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart @@ -7670,12 +7670,11 @@ class InferenceVisitorImpl extends InferenceVisitorBase // Note that a switch statement with a `default` clause is always considered // exhaustive, but the kernel format also keeps track of whether the switch // statement is "explicitly exhaustive", meaning that it has a `case` clause - // for every possible enum value. So if there's a `default` clause we need - // to call `isSwitchExhaustive` to figure out whether the switch is - // *explicitly* exhaustive. - node.isExplicitlyExhaustive = analysisResult.hasDefault - ? isLegacySwitchExhaustive(node, analysisResult.scrutineeType) - : analysisResult.isExhaustive; + // for every possible enum value. It is only necessary to set this flag if + // the switch doesn't have a `default` clause. + if (!analysisResult.hasDefault) { + node.isExplicitlyExhaustive = analysisResult.isExhaustive; + } _enumFields = previousEnumFields; // Stack: (Expression) Node? rewrite = popRewrite(); @@ -8764,7 +8763,9 @@ class InferenceVisitorImpl extends InferenceVisitorBase @override void handleSwitchScrutinee(DartType type) { - if (type is InterfaceType && type.classNode.isEnum) { + if (!options.patternsEnabled && + type is InterfaceType && + type.classNode.isEnum) { _enumFields = { ...type.classNode.fields.where((Field field) => field.isEnumElement), if (type.isPotentiallyNullable) null diff --git a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.expect b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.expect index 0629e562fa2..fa58db588b8 100644 --- a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.expect +++ b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.expect @@ -63,7 +63,7 @@ static method method3(self::Enum? e) → core::int { } } static method method4(self::Enum? e) → core::int { - switch(e) /*isExplicitlyExhaustive*/ { + switch(e) { #L6: case #C3: case #C6: diff --git a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.transformed.expect b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.transformed.expect index 0629e562fa2..fa58db588b8 100644 --- a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.transformed.expect +++ b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.strong.transformed.expect @@ -63,7 +63,7 @@ static method method3(self::Enum? e) → core::int { } } static method method4(self::Enum? e) → core::int { - switch(e) /*isExplicitlyExhaustive*/ { + switch(e) { #L6: case #C3: case #C6: diff --git a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.expect b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.expect index 8e6c5c22993..7966201fc8e 100644 --- a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.expect +++ b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.expect @@ -67,7 +67,7 @@ static method method3(self::Enum? e) → core::int { } } static method method4(self::Enum? e) → core::int { - switch(e) /*isExplicitlyExhaustive*/ { + switch(e) { #L7: case #C3: case #C6: diff --git a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.modular.expect b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.modular.expect index 8e6c5c22993..7966201fc8e 100644 --- a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.modular.expect +++ b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.modular.expect @@ -67,7 +67,7 @@ static method method3(self::Enum? e) → core::int { } } static method method4(self::Enum? e) → core::int { - switch(e) /*isExplicitlyExhaustive*/ { + switch(e) { #L7: case #C3: case #C6: diff --git a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.transformed.expect b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.transformed.expect index 8e6c5c22993..7966201fc8e 100644 --- a/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.transformed.expect +++ b/pkg/front_end/testcases/nnbd/switch_nullable_enum.dart.weak.transformed.expect @@ -67,7 +67,7 @@ static method method3(self::Enum? e) → core::int { } } static method method4(self::Enum? e) → core::int { - switch(e) /*isExplicitlyExhaustive*/ { + switch(e) { #L7: case #C3: case #C6: diff --git a/pkg/kernel/lib/ast.dart b/pkg/kernel/lib/ast.dart index e2b2ea79617..ea6aaf510ef 100644 --- a/pkg/kernel/lib/ast.dart +++ b/pkg/kernel/lib/ast.dart @@ -10394,7 +10394,9 @@ class SwitchStatement extends Statement { Expression expression; final List cases; - /// For enum switches, whether all enum values are covered by a switch case. + /// For switches without a default clause, whether all possible values are + /// covered by a switch case. For switches with a default clause, always + /// `false`. /// Initialized during type inference. bool isExplicitlyExhaustive;