Skip to content

Commit e4af4e1

Browse files
committed
Handle archetypes inside Substitution::subst
With inlining of generics we run into cases, where a replacement can be an archetype with a class requirement or an archetype with a superclass.
1 parent a755f5e commit e4af4e1

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/AST/Substitution.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,26 @@ Substitution Substitution::subst(ModuleDecl *module,
9595
// conformances from thin air. FIXME: gross.
9696
if (!conformance &&
9797
proto->isSpecificProtocol(KnownProtocolKind::AnyObject)) {
98-
auto classDecl
99-
= substReplacement->getClassOrBoundGenericClass();
100-
assert(classDecl);
101-
SmallVector<ProtocolConformance *, 1> lookupResults;
102-
classDecl->lookupConformance(classDecl->getParentModule(),
103-
proto, lookupResults);
104-
conformance = ProtocolConformanceRef(lookupResults.front());
98+
auto archetype =
99+
dyn_cast<ArchetypeType>(substReplacement->getCanonicalType());
100+
// If classDecl is not nullptr, it is a concrete class.
101+
auto classDecl = substReplacement->getClassOrBoundGenericClass();
102+
if (!classDecl && archetype->getSuperclass()) {
103+
// Replacement type is an archetype with a superclass constraint.
104+
classDecl = archetype->getSuperclass()->getClassOrBoundGenericClass();
105+
assert(classDecl);
106+
}
107+
if (classDecl) {
108+
// Create a concrete conformance based on the conforming class.
109+
SmallVector<ProtocolConformance *, 1> lookupResults;
110+
classDecl->lookupConformance(classDecl->getParentModule(), proto,
111+
lookupResults);
112+
conformance = ProtocolConformanceRef(lookupResults.front());
113+
} else if (archetype && archetype->requiresClass()) {
114+
// Replacement type is an archetype with a class constraint.
115+
// Create an abstract conformance.
116+
conformance = ProtocolConformanceRef(proto);
117+
}
105118
}
106119

107120
assert(conformance);

0 commit comments

Comments
 (0)