Skip to content

Commit 155273a

Browse files
authored
Merge pull request swiftlang#9199 from nkcsgexi/separate-note-switch
Sema: Move the fixit for adding missing enum cases to a separate note instead of attaching to the exhaustive error itself.
2 parents cd04e26 + 9909478 commit 155273a

16 files changed

+69
-60
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,12 +3615,14 @@ WARNING(debug_long_closure_body, none,
36153615
ERROR(empty_switch_stmt,none,
36163616
"'switch' statement body must have at least one 'case' or 'default' "
36173617
"block; do you want to add a default case?",())
3618-
ERROR(non_exhaustive_switch,none,
3619-
"%select{switch must be exhaustive, consider adding |do you want to add }0"
3620-
"%select{missing cases|a default clause}1"
3621-
"%select{:|?}0", (bool, bool))
3618+
ERROR(non_exhaustive_switch,none, "switch must be exhaustive", ())
3619+
NOTE(missing_several_cases,none,
3620+
"do you want to add "
3621+
"%select{missing cases|a default clause}0"
3622+
"?", (bool))
3623+
36223624
NOTE(missing_particular_case,none,
3623-
"missing case: '%0'", (StringRef))
3625+
"add missing case: '%0'", (StringRef))
36243626
WARNING(redundant_particular_case,none,
36253627
"case is already handled by previous patterns; consider removing it",())
36263628

include/swift/Migrator/FixitFilter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ struct FixitFilter {
118118
Info.ID == diag::objc_inference_swift3_addobjc.ID ||
119119
Info.ID == diag::objc_inference_swift3_dynamic.ID ||
120120
Info.ID == diag::override_swift3_objc_inference.ID ||
121-
Info.ID == diag::objc_inference_swift3_objc_derived.ID)
121+
Info.ID == diag::objc_inference_swift3_objc_derived.ID ||
122+
Info.ID == diag::missing_several_cases.ID ||
123+
Info.ID == diag::missing_particular_case.ID)
122124
return true;
123125

124126
return false;

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,10 @@ namespace {
910910
TC.Context.Diags.diagnose(StartLoc, diag::empty_switch_stmt)
911911
.fixItInsert(EndLoc, Buffer.str());
912912
} else {
913-
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch,
914-
InEditor, uncovered.isEmpty())
915-
.fixItInsert(EndLoc, Buffer.str());
913+
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch);
914+
TC.Context.Diags.diagnose(StartLoc, diag::missing_several_cases,
915+
uncovered.isEmpty()).fixItInsert(EndLoc,
916+
Buffer.str());
916917
}
917918
return;
918919
}
@@ -944,11 +945,11 @@ namespace {
944945
}
945946
}
946947

947-
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch, InEditor,
948-
false).fixItInsert(EndLoc, Buffer.str());
948+
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch);
949+
TC.Context.Diags.diagnose(StartLoc, diag::missing_several_cases, false)
950+
.fixItInsert(EndLoc, Buffer.str());
949951
} else {
950-
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch,
951-
InEditor, false);
952+
TC.Context.Diags.diagnose(StartLoc, diag::non_exhaustive_switch);
952953

953954
for (auto &uncoveredSpace : uncovered.getSpaces()) {
954955
SmallVector<Space, 4> flats;

test/ClangImporter/enum-dataflow.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import user_objc
77

88
let aliasOriginal = NSAliasesEnum.byName
99

10-
switch aliasOriginal { // expected-error {{switch must be exhaustive, consider adding missing cases:}}
11-
// expected-note@-1 {{missing case: '.differentValue'}}
10+
switch aliasOriginal { // expected-error {{switch must be exhaustive}}
11+
// expected-note@-1 {{add missing case: '.differentValue'}}
1212
case .original:
1313
break
1414
}
1515

16-
switch aliasOriginal { // expected-error {{switch must be exhaustive, consider adding missing cases:}}
17-
// expected-note@-1 {{missing case: '.original'}}
18-
// expected-note@-2 {{missing case: '.differentValue'}}
16+
switch aliasOriginal { // expected-error {{switch must be exhaustive}}
17+
// expected-note@-1 {{add missing case: '.original'}}
18+
// expected-note@-2 {{add missing case: '.differentValue'}}
1919
case .bySameValue:
2020
break
2121
}

test/ClangImporter/enum-error.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func testError() {
8383
switch (terr) { case .TENone, .TEOne, .TETwo: break } // ok
8484

8585
switch (terr) { case .TENone, .TEOne: break }
86-
// EXHAUSTIVE: [[@LINE-1]]:{{.+}}: error: switch must be exhaustive, consider adding missing cases
87-
// EXHAUSTIVE: [[@LINE-2]]:{{.+}}: note: missing case: '.TETwo'
86+
// EXHAUSTIVE: [[@LINE-1]]:{{.+}}: error: switch must be exhaustive
87+
// EXHAUSTIVE: [[@LINE-2]]:{{.+}}: note: add missing case: '.TETwo'
8888
let _ = TestError.Code(rawValue: 2)!
8989

9090
do {

test/ClangImporter/enum-new.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ func test() {
1313
case .Yellow, .Magenta, .Black, .Cyan: break
1414
} // no-error
1515

16-
switch getColorOptions() { // expected-error {{switch must be exhaustive, consider adding a default clause}}
16+
switch getColorOptions() { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
1717
case ColorOptions.Pastel: break
1818
case ColorOptions.Swift: break
1919
}
2020

21-
switch 5 as Int16 { // expected-error {{switch must be exhaustive, consider adding a default clause}}
22-
case Zero: break // no-error
21+
switch 5 as Int16 { // expected-error {{'switch' statement body must have at least one 'case' or 'default' block; do you want to add a default case?}}
2322
}
2423
}

test/ClangImporter/swift2_warnings.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ class X : NSDocument {
9999
func makeProgress<T: NSProgressReporting>(thing: T) {} // expected-error {{'NSProgressReporting' has been renamed to 'ProgressReporting'}} {{22-41=ProgressReporting}}
100100

101101
func useLowercasedEnumCase(x: NSRuncingMode) {
102-
switch x { // expected-error {{switch must be exhaustive, consider adding missing cases:}}
103-
// expected-note@-1 {{missing case: '.mince'}}
104-
// expected-note@-2 {{missing case: '.quince'}}
102+
switch x { // expected-error {{switch must be exhaustive}}
103+
// expected-note@-1 {{add missing case: '.mince'}}
104+
// expected-note@-2 {{add missing case: '.quince'}}
105105
case .Mince: return // expected-error {{'Mince' has been renamed to 'mince'}} {{11-16=mince}}
106106
case .Quince: return // expected-error {{'Quince' has been renamed to 'quince'}} {{11-17=quince}}
107107
}

test/Parse/matching_patterns.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ case _: // expected-warning {{case is already handled by previous patterns; cons
5858

5959
var e : Any = 0
6060

61-
switch e { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
61+
switch e { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
6262
// 'is' pattern.
6363
case is Int,
6464
is A<Int>,
@@ -152,7 +152,7 @@ struct ContainsEnum {
152152
}
153153

154154
func member(_ n: Possible<Int>) {
155-
switch n { // expected-error {{switch must be exhaustive, consider adding missing cases:}}
155+
switch n { // expected-error {{switch must be exhaustive}}
156156
// expected-note@-1 {{missing case: '.Mere(_)'}}
157157
// expected-note@-2 {{missing case: '.Twain(_, _)'}}
158158
case ContainsEnum.Possible<Int>.Naught,
@@ -166,7 +166,7 @@ struct ContainsEnum {
166166
}
167167

168168
func nonmemberAccessesMemberType(_ n: ContainsEnum.Possible<Int>) {
169-
switch n { // expected-error {{switch must be exhaustive, consider adding missing cases:}}
169+
switch n { // expected-error {{switch must be exhaustive}}
170170
// expected-note@-1 {{missing case: '.Mere(_)'}}
171171
// expected-note@-2 {{missing case: '.Twain(_, _)'}}
172172
case ContainsEnum.Possible<Int>.Naught,

test/Parse/switch.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ default:
7171
}
7272

7373
// Multiple cases per case block
74-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
74+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
7575
case 0: // expected-error {{'case' label in a 'switch' should have at least one executable statement}} {{8-8= break}}
7676
case 1:
7777
x = 0
@@ -83,7 +83,7 @@ default:
8383
x = 0
8484
}
8585

86-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
86+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
8787
case 0:
8888
x = 0
8989
case 1: // expected-error {{'case' label in a 'switch' should have at least one executable statement}} {{8-8= break}}
@@ -95,7 +95,7 @@ case 0:
9595
default: // expected-error {{'default' label in a 'switch' should have at least one executable statement}} {{9-9= break}}
9696
}
9797

98-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
98+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
9999
case 0:
100100
; // expected-error {{';' statements are not allowed}} {{3-5=}}
101101
case 1:
@@ -142,22 +142,22 @@ default: // expected-error{{additional 'case' blocks cannot appear after the 'de
142142
x = 0
143143
}
144144

145-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
145+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
146146
default where x == 0: // expected-error{{'default' cannot be used with a 'where' guard expression}}
147147
x = 0
148148
}
149149

150-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
150+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
151151
case 0: // expected-error {{'case' label in a 'switch' should have at least one executable statement}} {{8-8= break}}
152152
}
153153

154-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
154+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
155155
case 0: // expected-error{{'case' label in a 'switch' should have at least one executable statement}} {{8-8= break}}
156156
case 1:
157157
x = 0
158158
}
159159

160-
switch x { // expected-error {{switch must be exhaustive, consider adding a default clause:}}
160+
switch x { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
161161
case 0:
162162
x = 0
163163
case 1: // expected-error{{'case' label in a 'switch' should have at least one executable statement}} {{8-8= break}}
@@ -260,7 +260,7 @@ case (var a, var b): // expected-warning {{variable 'a' was never mutated; consi
260260
}
261261

262262
func test_label(x : Int) {
263-
Gronk: // expected-error {{switch must be exhaustive, consider adding a default clause:}}
263+
Gronk: // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
264264
switch x {
265265
case 42: return
266266
}

test/Parse/switch_incomplete.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
// <rdar://problem/15971438> Incomplete switch was parsing to an AST that
44
// triggered an assertion failure.
5-
// expected-error@+1 {{switch must be exhaustive, consider adding a default clause:}}
5+
// expected-error@+1 {{switch must be exhaustive}} expected-note@+1{{do you want to add a default clause?}}
66
switch 1 { // expected-note{{to match this opening '{'}}
77
case 1: // expected-error@+1{{expected '}' at end of 'switch' statement}}

0 commit comments

Comments
 (0)