Skip to content

Commit af83f88

Browse files
committed
Sema: Fix crash when enum case satisfies requirement in @rethrows protocol
1 parent 5246fef commit af83f88

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,18 @@ static bool classifyWitness(ModuleDecl *module,
135135
ProtocolConformance *conformance,
136136
AbstractFunctionDecl *req) {
137137
auto declRef = conformance->getWitnessDeclRef(req);
138-
auto witnessDecl = cast<AbstractFunctionDecl>(declRef.getDecl());
138+
if (!declRef) {
139+
// Invalid conformance.
140+
return true;
141+
}
142+
143+
auto witnessDecl = dyn_cast<AbstractFunctionDecl>(declRef.getDecl());
144+
if (!witnessDecl) {
145+
// Enum element constructors never throw.
146+
assert(isa<EnumElementDecl>(declRef.getDecl()));
147+
return false;
148+
}
149+
139150
switch (witnessDecl->getRethrowingKind()) {
140151
case FunctionRethrowingKind::None:
141152
// Witness doesn't throw at all, so it contributes nothing.

test/attr/attr_rethrows_protocol.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,18 @@ struct ThirdWitness : Third {
191191

192192
func takesFirst<T : First>(_: T) rethrows {}
193193

194-
takesFirst(FirstWitness())
194+
takesFirst(FirstWitness())
195+
196+
// Crash with enum case
197+
@rethrows protocol WitnessedByEnumCase {
198+
static func foo(_: Int) throws -> Self
199+
}
200+
201+
enum MyEnum : WitnessedByEnumCase {
202+
case foo(Int)
203+
case bar
204+
}
205+
206+
func takesWitnessedByEnumCase<T : WitnessedByEnumCase>(_: T) rethrows {}
207+
208+
takesWitnessedByEnumCase(MyEnum.bar)

0 commit comments

Comments
 (0)