Skip to content

Commit 75aa283

Browse files
author
David Ungar
authored
Merge pull request #16374 from davidungar/rdar-39947138-better-diag-for-unverifyable-switch
Rdar 39947138 Different diagnostics for proveably vs unproveably non-exhaustive switch.
2 parents 09b66c8 + c7a2ae9 commit 75aa283

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,6 +3954,10 @@ ERROR(empty_switch_stmt,none,
39543954
"'switch' statement body must have at least one 'case' or 'default' "
39553955
"block; do you want to add a default case?",())
39563956
ERROR(non_exhaustive_switch,none, "switch must be exhaustive", ())
3957+
ERROR(possibly_non_exhaustive_switch,none,
3958+
"the compiler is unable to check that this switch is exhaustive in reasonable time",
3959+
())
3960+
39573961
NOTE(missing_several_cases,none,
39583962
"do you want to add "
39593963
"%select{missing cases|a default clause}0"

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,8 @@ namespace {
13511351
SpaceTooLarge,
13521352
};
13531353

1354-
void diagnoseMissingCases(RequiresDefault defaultReason, Space uncovered,
1354+
void diagnoseMissingCases(const RequiresDefault defaultReason,
1355+
Space uncovered,
13551356
const CaseStmt *unknownCase = nullptr,
13561357
bool sawDowngradablePattern = false) {
13571358
SourceLoc startLoc = Switch->getStartLoc();
@@ -1377,14 +1378,18 @@ namespace {
13771378
mainDiagType = diag::non_exhaustive_switch_warn;
13781379
break;
13791380
case RequiresDefault::UncoveredSwitch:
1380-
case RequiresDefault::SpaceTooLarge:
1381-
TC.diagnose(startLoc, diag::non_exhaustive_switch);
1381+
case RequiresDefault::SpaceTooLarge: {
1382+
auto diagnostic = defaultReason == RequiresDefault::UncoveredSwitch
1383+
? diag::non_exhaustive_switch
1384+
: diag::possibly_non_exhaustive_switch;
1385+
TC.diagnose(startLoc, diagnostic);
13821386
TC.diagnose(unknownCase->getLoc(),
13831387
diag::non_exhaustive_switch_drop_unknown)
13841388
.fixItRemoveChars(unknownCase->getStartLoc(),
13851389
unknownCase->getLoc());
13861390
return;
13871391
}
1392+
}
13881393
}
13891394

13901395
switch (uncovered.checkDowngradeToWarning()) {
@@ -1427,7 +1432,7 @@ namespace {
14271432
return;
14281433
case RequiresDefault::SpaceTooLarge: {
14291434
OS << tok::kw_default << ":\n" << "<#fatalError()#>" << "\n";
1430-
TC.diagnose(startLoc, diag::non_exhaustive_switch);
1435+
TC.diagnose(startLoc, diag::possibly_non_exhaustive_switch);
14311436
TC.diagnose(startLoc, diag::missing_several_cases, /*default*/true)
14321437
.fixItInsert(insertLoc, buffer.str());
14331438
}

test/Compatibility/exhaustive_switch.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ enum ContainsOverlyLargeEnum {
442442
}
443443

444444
func quiteBigEnough() -> Bool {
445-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
445+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
446446
// expected-note@-1 {{do you want to add a default clause?}}
447447
case (.case0, .case0): return true
448448
case (.case1, .case1): return true
@@ -458,7 +458,7 @@ func quiteBigEnough() -> Bool {
458458
case (.case11, .case11): return true
459459
}
460460

461-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
461+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
462462
// expected-note@-1 {{do you want to add a default clause?}}
463463
case (.case0, _): return true
464464
case (.case1, _): return true
@@ -473,7 +473,7 @@ func quiteBigEnough() -> Bool {
473473
case (.case10, _): return true
474474
}
475475

476-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
476+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
477477
case (.case0, _): return true
478478
case (.case1, _): return true
479479
case (.case2, _): return true

test/Sema/exhaustive_switch.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ enum ContainsOverlyLargeEnum {
465465
}
466466

467467
func quiteBigEnough() -> Bool {
468-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
468+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
469469
// expected-note@-1 {{do you want to add a default clause?}}
470470
case (.case0, .case0): return true
471471
case (.case1, .case1): return true
@@ -481,7 +481,7 @@ func quiteBigEnough() -> Bool {
481481
case (.case11, .case11): return true
482482
}
483483

484-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
484+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
485485
// expected-note@-1 {{do you want to add a default clause?}}
486486
case (.case0, _): return true
487487
case (.case1, _): return true
@@ -496,7 +496,7 @@ func quiteBigEnough() -> Bool {
496496
case (.case10, _): return true
497497
}
498498

499-
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{switch must be exhaustive}}
499+
switch (OverlyLargeSpaceEnum.case1, OverlyLargeSpaceEnum.case2) { // expected-error {{the compiler is unable to check that this switch is exhaustive in reasonable time}}
500500
case (.case0, _): return true
501501
case (.case1, _): return true
502502
case (.case2, _): return true

0 commit comments

Comments
 (0)