Skip to content

Commit cd2daa9

Browse files
committed
Fix assertion from empty switch over uninhabited enum
Fixes SR-8933. Swift's uninhabited checking is conservative. An enum might not be found to be uninhabited, but all of its cases might. In that case, the switch can be empty even though the type isn't known to be uninhabited. Fix an assertion that assumed there would always be at least one case for types that aren't known to be uninhabited.
1 parent 2b091f8 commit cd2daa9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ chooseNecessaryColumn(const ClauseMatrix &matrix, unsigned firstRow) {
983983
/// Recursively emit a decision tree from the given pattern matrix.
984984
void PatternMatchEmission::emitDispatch(ClauseMatrix &clauses, ArgArray args,
985985
const FailureHandler &outerFailure) {
986+
if (clauses.rows() == 0) {
987+
SGF.B.createUnreachable(SILLocation(PatternMatchStmt));
988+
return;
989+
}
990+
986991
unsigned firstRow = 0;
987992
while (true) {
988993
// If there are no rows remaining, then we fail.

test/Sema/exhaustive_switch.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ enum MyNever {}
360360
func ~= (_ : MyNever, _ : MyNever) -> Bool { return true }
361361
func myFatalError() -> MyNever { fatalError() }
362362

363+
@_frozen public enum UninhabitedT4<A> {
364+
case x(A)
365+
}
366+
363367
func checkUninhabited() {
364368
// Scrutinees of uninhabited type may match any number and kind of patterns
365369
// that Sema is willing to accept at will. After all, it's quite a feat to
@@ -379,6 +383,10 @@ func checkUninhabited() {
379383
case myFatalError(): break
380384
}
381385
}
386+
387+
func test4(x: UninhabitedT4<Never>) {
388+
switch x {} // No diagnostic.
389+
}
382390
}
383391

384392
enum Runcible {

0 commit comments

Comments
 (0)