Skip to content

Commit 7c4ee78

Browse files
committed
Produce specialized conformances to AnyObject for bound generic types.
Fixes rdar://31910351 (SR-4750)
1 parent 37de124 commit 7c4ee78

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/AST/ProtocolConformance.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ ProtocolConformanceRef::subst(Type origType,
159159
SmallVector<ProtocolConformance *, 1> lookupResults;
160160
classDecl->lookupConformance(classDecl->getParentModule(), proto,
161161
lookupResults);
162-
return ProtocolConformanceRef(lookupResults.front());
162+
auto *conf = lookupResults.front();
163+
auto subMap = substType->getContextSubstitutionMap(
164+
conf->getDeclContext()->getParentModule(), conf->getDeclContext());
165+
if (!subMap.empty())
166+
conf = substType->getASTContext().getSpecializedConformance(substType,
167+
conf, subMap);
168+
return ProtocolConformanceRef(conf);
163169
}
164170

165171
llvm_unreachable("Invalid conformance substitution");

lib/AST/SubstitutionMap.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
116116
auto genericParam = cast<GenericTypeParamType>(type);
117117
auto mutableThis = const_cast<SubstitutionMap *>(this);
118118
auto replacementTypes = mutableThis->getReplacementTypes();
119-
auto genericParams = getGenericSignature()->getGenericParams();
119+
auto genericSig = getGenericSignature();
120+
assert(genericSig);
121+
auto genericParams = genericSig->getGenericParams();
120122
auto replacementIndex =
121123
GenericParamKey(genericParam).findIndexIn(genericParams);
122124

@@ -133,7 +135,6 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
133135
// The generic parameter may have been made concrete by the generic signature,
134136
// substitute into the concrete type.
135137
ModuleDecl &anyModule = *genericParam->getASTContext().getStdlibModule();
136-
auto genericSig = getGenericSignature();
137138
if (auto concreteType = genericSig->getConcreteType(genericParam, anyModule)){
138139
// Set the replacement type to an error, to block infinite recursion.
139140
replacementType = ErrorType::get(concreteType);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend -O -sil-inline-threshold 0 -emit-sil -primary-file %s | %FileCheck %s
2+
3+
// rdar://problem/31910351
4+
5+
// Check that swift compiler does not crash on this input.
6+
7+
public protocol P {
8+
func use<T:AnyObject>(_ t: T)
9+
}
10+
11+
public class C<T> {
12+
}
13+
14+
public func callee(_ t: C<Int32>?, _ p: P) {
15+
// This call results in a creation of a specialized conformance of C<Int32> to AnyObject.
16+
p.use(t!)
17+
}
18+
19+
// CHECK-LABEL: sil @_T033specialized_anyobject_conformance7caller1yAA1P_p1p_tF : $@convention(thin) (@in P) -> ()
20+
public func caller1(p: P) {
21+
callee(C<Int32>(), p)
22+
}
23+

0 commit comments

Comments
 (0)