Skip to content

Commit 619254f

Browse files
committed
AST: Fix latent bug in SubstitutionMap::lookupConformance()
We should check if the type parameter actually conforms to our protocol before we do the global lookup, otherwise we might return an abstract conformance instead of an invalid conformance. I don't know if there's any way to exercise this today though.
1 parent c86c772 commit 619254f

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/AST/SubstitutionMap.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,6 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
355355
if (!type->isTypeParameter())
356356
return ProtocolConformanceRef::forInvalid();
357357

358-
// If the protocol is invertible, just do a global lookup. This avoids an
359-
// infinite substitution issue by recognizing that these protocols are
360-
// very simple (see rdar://119950540 for the general issue).
361-
if (proto->getInvertibleProtocolKind()) {
362-
auto substType = type.subst(*this);
363-
if (!substType->isTypeParameter())
364-
return proto->getModuleContext()->lookupConformance(substType, proto);
365-
return ProtocolConformanceRef(proto);
366-
}
367-
368358
auto genericSig = getGenericSignature();
369359

370360
auto getSignatureConformance =
@@ -396,6 +386,15 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
396386
return ProtocolConformanceRef::forMissingOrInvalid(substType, proto);
397387
}
398388

389+
// If the protocol is invertible, fall back to a global lookup instead of
390+
// evaluating a conformance path, to avoid an infinite substitution issue.
391+
if (proto->getInvertibleProtocolKind()) {
392+
auto substType = type.subst(*this);
393+
if (!substType->isTypeParameter())
394+
return proto->getModuleContext()->lookupConformance(substType, proto);
395+
return ProtocolConformanceRef(proto);
396+
}
397+
399398
auto path = genericSig->getConformancePath(type, proto);
400399

401400
ProtocolConformanceRef conformance;

0 commit comments

Comments
 (0)