Only set isExplicitlyExhaustive on switch statements without default clauses.

This enables the front end to skip the old-style (enum-based)
exhaustiveness checking algorithm when pattern support is enabled,
because with pattern support enabled, switches on enums are required
to be exhaustive (this will be checked by the new exhaustiveness
checking algorithm).

That in turn means that in the future, when we remove support for
language versions that lack patterns support, we will be able to
remove the old-style exhaustiveness checking algorithm.

This change has a small effect on code generated by the WASM back-end
(the only back-end that uses `isExplicitlyExhaustive`): for a switch
statement that is exhaustive *and* has an unreachable `default`
clause, after testing all the cases, the WASM back-end will generate a
branch to the `default` case.  Previously it would instead generate an
`unreachable` instruction.  There should be no behavioural difference
because the instruction in question is unreachable in both cases.
Also, there should be negligible code size difference because the body
of the `default` case is being emitted either way.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Id6bd7d9a540cb1b4d9c3624db8ff494438276bea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274924
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
This commit is contained in:
Paul Berry 2022-12-14 14:38:50 +00:00 committed by Commit Queue
parent 607b370933
commit 9b294b846b
7 changed files with 16 additions and 13 deletions

View file

@ -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 = <Field?>{
...type.classNode.fields.where((Field field) => field.isEnumElement),
if (type.isPotentiallyNullable) null

View file

@ -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:

View file

@ -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:

View file

@ -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:

View file

@ -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:

View file

@ -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:

View file

@ -10394,7 +10394,9 @@ class SwitchStatement extends Statement {
Expression expression;
final List<SwitchCase> 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;