Skip to content

Produce specialized conformances to AnyObject for bound generic types. #9181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ ProtocolConformanceRef::subst(Type origType,
SmallVector<ProtocolConformance *, 1> lookupResults;
classDecl->lookupConformance(classDecl->getParentModule(), proto,
lookupResults);
return ProtocolConformanceRef(lookupResults.front());
auto *conf = lookupResults.front();
auto subMap = substType->getContextSubstitutionMap(
conf->getDeclContext()->getParentModule(), conf->getDeclContext());
if (!subMap.empty())
conf = substType->getASTContext().getSpecializedConformance(substType,
conf, subMap);
return ProtocolConformanceRef(conf);
}

llvm_unreachable("Invalid conformance substitution");
Expand Down
5 changes: 3 additions & 2 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
auto genericParam = cast<GenericTypeParamType>(type);
auto mutableThis = const_cast<SubstitutionMap *>(this);
auto replacementTypes = mutableThis->getReplacementTypes();
auto genericParams = getGenericSignature()->getGenericParams();
auto genericSig = getGenericSignature();
assert(genericSig);
auto genericParams = genericSig->getGenericParams();
auto replacementIndex =
GenericParamKey(genericParam).findIndexIn(genericParams);

Expand All @@ -133,7 +135,6 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
// The generic parameter may have been made concrete by the generic signature,
// substitute into the concrete type.
ModuleDecl &anyModule = *genericParam->getASTContext().getStdlibModule();
auto genericSig = getGenericSignature();
if (auto concreteType = genericSig->getConcreteType(genericParam, anyModule)){
// Set the replacement type to an error, to block infinite recursion.
replacementType = ErrorType::get(concreteType);
Expand Down
23 changes: 23 additions & 0 deletions test/SILOptimizer/specialized_anyobject_conformance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend -O -sil-inline-threshold 0 -emit-sil -primary-file %s | %FileCheck %s

// rdar://problem/31910351

// Check that swift compiler does not crash on this input.

public protocol P {
func use<T:AnyObject>(_ t: T)
}

public class C<T> {
}

public func callee(_ t: C<Int32>?, _ p: P) {
// This call results in a creation of a specialized conformance of C<Int32> to AnyObject.
p.use(t!)
}

// CHECK-LABEL: sil @_T033specialized_anyobject_conformance7caller1yAA1P_p1p_tF : $@convention(thin) (@in P) -> ()
public func caller1(p: P) {
callee(C<Int32>(), p)
}