Skip to content

Commit badcc2c

Browse files
owenvtheblixguy
authored andcommitted
[Diagnostics] Don't include @unknown default in the empty switch fix-it (#28476)
1 parent 53ed83c commit badcc2c

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,12 @@ namespace {
12071207
}
12081208
if (!Context.LangOpts.EnableNonFrozenEnumExhaustivityDiagnostics)
12091209
continue;
1210+
1211+
// This can occur if the switch is empty and the subject type is an
1212+
// enum. If decomposing the enum type yields an unknown space that
1213+
// is not required, don't suggest adding it in the fix-it.
1214+
if (flat.isAllowedButNotRequired())
1215+
continue;
12101216
}
12111217

12121218
process(flat, flats.size() == 1);

test/Sema/exhaustive_switch.swift

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution
2-
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-library-evolution -enable-nonfrozen-enum-exhaustivity-diagnostics
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution %S/Inputs/exhaustive_switch_testable_helper.swift -emit-module -o %t
3+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -I %t
4+
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-library-evolution -enable-nonfrozen-enum-exhaustivity-diagnostics -I %t
5+
6+
import exhaustive_switch_testable_helper
37

48
func foo(a: Int?, b: Int?) -> Int {
59
switch (a, b) {
@@ -1214,6 +1218,54 @@ func sr11160_extra() {
12141218
}
12151219
}
12161220

1221+
public enum SR11672Tests {
1222+
1223+
@frozen public enum FrozenSameModule {
1224+
case a, b
1225+
}
1226+
1227+
func testNotRequired(_ value: NonExhaustive, _ value2: FrozenEnum, _ value3: FrozenSameModule) {
1228+
switch value {
1229+
// expected-error@-1 {{switch must be exhaustive}}
1230+
// expected-note@-2 {{add missing case: '.a'}}
1231+
// expected-note@-3 {{add missing case: '.b'}}
1232+
// Do not suggest adding '@unknown default'
1233+
}
1234+
1235+
switch value2 {
1236+
// expected-error@-1 {{switch must be exhaustive}}
1237+
// expected-note@-2 {{add missing case: '.a'}}
1238+
// expected-note@-3 {{add missing case: '.b'}}
1239+
// expected-note@-4 {{add missing case: '.c'}}
1240+
}
1241+
1242+
switch value3 {
1243+
// expected-error@-1 {{switch must be exhaustive}}
1244+
// expected-note@-2 {{add missing case: '.a'}}
1245+
// expected-note@-3 {{add missing case: '.b'}}
1246+
}
1247+
}
1248+
1249+
@inlinable public func testNotRequired2(_ value: FrozenSameModule) {
1250+
switch value {
1251+
// expected-error@-1 {{switch must be exhaustive}}
1252+
// expected-note@-2 {{add missing case: '.a'}}
1253+
// expected-note@-3 {{add missing case: '.b'}}
1254+
}
1255+
}
1256+
1257+
// Inlinable code is considered "outside" the module and must include a default
1258+
// case.
1259+
@inlinable public func testRequired(_ value: NonExhaustive) {
1260+
switch value {
1261+
// expected-error@-1 {{switch must be exhaustive}}
1262+
// expected-note@-2 {{add missing case: '.a'}}
1263+
// expected-note@-3 {{add missing case: '.b'}}
1264+
// expected-note@-4 {{handle unknown values using "@unknown default"}}
1265+
}
1266+
}
1267+
}
1268+
12171269
// SR-11212 tests: Some of the tests here rely on compiler bugs related to
12181270
// implicit (un)tupling in patterns.
12191271
//

test/stmt/nonexhaustive_switch_stmt_editor.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public func testNonExhaustive(_ value: NonExhaustive) {
1919
case .b: break
2020
}
2121

22+
switch value { // expected-error {{switch must be exhaustive}}
23+
// expected-note@-1 {{do you want to add missing cases?}} {{3-3=case .a:\n<#code#>\ncase .b:\n<#code#>\n@unknown default:\n<#code#>\n}}
24+
}
25+
2226
switch value {
2327
case .a: break
2428
case .b: break

0 commit comments

Comments
 (0)