Skip to content

Commit e3fef32

Browse files
committed
SILGen: Don't use diagnoseUnexpectedEnumCase intrinsic for noncopyable enums.
It should be impossible to reach an unexpected case statically while using noncopyable enums, and the intrinsic has not been updated to remove its `Copyable` requirement. Emit a plain trap in cases where this code emission path might still occur, such as when a redundant but incomplete set of case patterns follow a wildcard pattern. Fixes rdar://130037881.
1 parent b7b93a1 commit e3fef32

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,13 @@ static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,
31093109
static void emitDiagnoseOfUnexpectedEnumCase(SILGenFunction &SGF,
31103110
SILLocation loc,
31113111
UnexpectedEnumCaseInfo ueci) {
3112+
if (ueci.subjectTy->isNoncopyable()) {
3113+
// TODO: The DiagnoseUnexpectedEnumCase intrinsic currently requires a
3114+
// Copyable parameter. For noncopyable enums it should be impossible to
3115+
// reach an unexpected case statically, so just emit a trap for now.
3116+
SGF.B.createUnconditionalFail(loc, "unexpected enum case");
3117+
return;
3118+
}
31123119
ASTContext &ctx = SGF.getASTContext();
31133120
auto diagnoseFailure = ctx.getDiagnoseUnexpectedEnumCase();
31143121
if (!diagnoseFailure) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-emit-silgen -verify %s
2+
3+
struct NC: ~Copyable {}
4+
5+
enum NoncopyableEnum: ~Copyable {
6+
case copyable(Int)
7+
case noncopyable(NC)
8+
}
9+
10+
func test(foo: consuming NoncopyableEnum) {
11+
switch foo {
12+
case let x: // expected-warning{{'x' was never used}}
13+
break
14+
case .copyable(let x): // expected-warning{{already handled by previous patterns}} expected-warning{{'x' was never used}}
15+
break
16+
}
17+
}

0 commit comments

Comments
 (0)