Skip to content

Commit 107e307

Browse files
authored
Space Engine: uninhabited types map to empty spaces (#17690)
Without this, the compiler ended up complaining about missing cases that can't actually occur, like `Optional<Never>.some(_)`. This was a regression from Swift 4.1. https://bugs.swift.org/browse/SR-8125 (cherry picked from commit e783027)
1 parent 13f8098 commit 107e307

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ namespace {
214214
Spaces({}) {}
215215

216216
static Space forType(Type T, Identifier NameForPrinting) {
217+
if (T->isStructurallyUninhabited())
218+
return Space();
217219
return Space(T, NameForPrinting);
218220
}
219221
static Space forUnknown(bool allowedButNotRequired) {

test/Compatibility/exhaustive_switch.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,3 +1184,23 @@ func testUnavailableCases(_ x: UnavailableCase, _ y: UnavailableCaseOSSpecific,
11841184
case .notYetIntroduced: break
11851185
} // no-error
11861186
}
1187+
1188+
// The following test used to behave differently when the uninhabited enum was
1189+
// defined in the same module as the function (as opposed to using Swift.Never).
1190+
enum NoError {}
1191+
extension Result where T == NoError {
1192+
func testUninhabited() {
1193+
switch self {
1194+
case .Error(_):
1195+
break
1196+
// No .Ok case possible because of the 'NoError'.
1197+
}
1198+
1199+
switch self {
1200+
case .Error(_):
1201+
break
1202+
case .Ok(_):
1203+
break // But it's okay to write one.
1204+
}
1205+
}
1206+
}

test/Sema/exhaustive_switch.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,23 @@ func testUnavailableCases(_ x: UnavailableCase, _ y: UnavailableCaseOSSpecific,
12231223
case .notYetIntroduced: break
12241224
} // no-error
12251225
}
1226+
1227+
// The following test used to behave differently when the uninhabited enum was
1228+
// defined in the same module as the function (as opposed to using Swift.Never).
1229+
enum NoError {}
1230+
extension Result where T == NoError {
1231+
func testUninhabited() {
1232+
switch self {
1233+
case .Error(_):
1234+
break
1235+
// No .Ok case possible because of the 'NoError'.
1236+
}
1237+
1238+
switch self {
1239+
case .Error(_):
1240+
break
1241+
case .Ok(_):
1242+
break // But it's okay to write one.
1243+
}
1244+
}
1245+
}

0 commit comments

Comments
 (0)