Skip to content

Commit 0b9f5eb

Browse files
committed
SIL: add some asserts to catch broken enum ASTs
1 parent 0c5fd6a commit 0b9f5eb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,9 @@ class SILBuilder {
17281728
EnumElementDecl *Element, SILType Ty,
17291729
ValueOwnershipKind forwardingOwnershipKind) {
17301730
ASSERT(isLoadableOrOpaque(Ty));
1731+
// Assert that this works and does not crash.
1732+
(void)getModule().getCaseIndex(Element);
1733+
17311734
return insert(new (getModule())
17321735
EnumInst(getSILDebugLocation(Loc), Operand, Element, Ty,
17331736
forwardingOwnershipKind));
@@ -1751,6 +1754,9 @@ class SILBuilder {
17511754
SILValue Operand,
17521755
EnumElementDecl *Element,
17531756
SILType Ty) {
1757+
// Assert that this works and does not crash.
1758+
(void)getModule().getCaseIndex(Element);
1759+
17541760
return insert(new (getModule()) InitEnumDataAddrInst(
17551761
getSILDebugLocation(Loc), Operand, Element, Ty));
17561762
}
@@ -1776,6 +1782,9 @@ class SILBuilder {
17761782
EnumElementDecl *Element, SILType Ty,
17771783
ValueOwnershipKind forwardingOwnershipKind) {
17781784
ASSERT(isLoadableOrOpaque(Ty));
1785+
// Assert that this works and does not crash.
1786+
(void)getModule().getCaseIndex(Element);
1787+
17791788
return insert(new (getModule()) UncheckedEnumDataInst(
17801789
getSILDebugLocation(Loc), Operand, Element, Ty,
17811790
forwardingOwnershipKind));
@@ -1791,6 +1800,9 @@ class SILBuilder {
17911800
UncheckedTakeEnumDataAddrInst *
17921801
createUncheckedTakeEnumDataAddr(SILLocation Loc, SILValue Operand,
17931802
EnumElementDecl *Element, SILType Ty) {
1803+
// Assert that this works and does not crash.
1804+
(void)getModule().getCaseIndex(Element);
1805+
17941806
return insert(new (getModule()) UncheckedTakeEnumDataAddrInst(
17951807
getSILDebugLocation(Loc), Operand, Element, Ty));
17961808
}
@@ -1805,6 +1817,9 @@ class SILBuilder {
18051817

18061818
InjectEnumAddrInst *createInjectEnumAddr(SILLocation Loc, SILValue Operand,
18071819
EnumElementDecl *Element) {
1820+
// Assert that this works and does not crash.
1821+
(void)getModule().getCaseIndex(Element);
1822+
18081823
return insert(new (getModule()) InjectEnumAddrInst(
18091824
getSILDebugLocation(Loc), Operand, Element));
18101825
}

lib/SIL/IR/SILModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ unsigned SILModule::getCaseIndex(EnumElementDecl *enumElement) {
804804
}
805805
++idx;
806806
}
807-
llvm_unreachable("enum element not found in enum decl");
807+
ASSERT(false && "enum element not found in enum decl, broken AST?");
808+
return 0;
808809
}
809810

810811
void SILModule::notifyAddedInstruction(SILInstruction *inst) {

0 commit comments

Comments
 (0)