Skip to content

[SILGen] Fall back to module lookup on invalid conformance when building subst. maps for vtable thunks #30470

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

Closed
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
24 changes: 23 additions & 1 deletion include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,30 @@ class SubstitutionMap {

/// Build an interface type substitution map for the given generic
/// signature using the mapping in the given substitutions.
///
/// \Note
/// Currently, \p fallBackToModuleConformanceLookup is only necessary
/// when we emit a reabstraction thunk for an override, because we might
/// not have enough information in the substitution map alone.
///
/// For example, combining the override substitutions with the invocation
/// generic signature for the following override requires recovering
/// the implied conformance Foo : P.
///
/// \code
/// class Base<T> {
/// func foo<U>(u: U) where T == Foo {}
/// }
/// class Derived<T>: Base<T> {
/// override func foo<U>(u: U) where T: P {}
/// }
/// \endcode
///
/// \param fallBackToModuleConformanceLookup Fall back to module lookup
/// if a conformance could not be found otherwise.
static SubstitutionMap get(GenericSignature genericSig,
SubstitutionMap substitutions);
SubstitutionMap substitutions,
bool fallBackToModuleConformanceLookup = false);

/// Build an interface type substitution map for the given generic signature
/// from a type substitution function and conformance lookup function.
Expand Down
34 changes: 32 additions & 2 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,49 @@ SubstitutionMap SubstitutionMap::getCanonical() const {


SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
SubstitutionMap substitutions) {
SubstitutionMap substitutions,
bool fallBackToModuleConformanceLookup) {
if (!genericSig) {
assert(!substitutions.hasAnySubstitutableParams() &&
"Shouldn't have substitutions here");
return SubstitutionMap();
}

LookupConformanceFn lookupConformanceFn;
if (fallBackToModuleConformanceLookup) {
/// Like \c LookUpConformanceInSubstitutionMap , but falls back to module
/// lookup if the conformance could not be found.
class LookUpConformanceInSubstitutionMapOrModule {
SubstitutionMap Subs;
public:
LookUpConformanceInSubstitutionMapOrModule(SubstitutionMap Subs)
: Subs(Subs) {}

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
auto conformance = LookUpConformanceInSubstitutionMap(Subs)
(dependentType, conformingReplacementType, conformedProtocol);

if (conformance.isInvalid())
return conformedProtocol->getParentModule()->lookupConformance(
conformingReplacementType, conformedProtocol);
return conformance;
};
};

lookupConformanceFn =
LookUpConformanceInSubstitutionMapOrModule(substitutions);
} else {
lookupConformanceFn = LookUpConformanceInSubstitutionMap(substitutions);
}

return SubstitutionMap::get(genericSig,
[&](SubstitutableType *type) -> Type {
return substitutions.lookupSubstitution(
CanSubstitutableType(type));
},
LookUpConformanceInSubstitutionMap(substitutions));
lookupConformanceFn);
}

/// Build an interface type substitution map for the given generic signature
Expand Down
21 changes: 11 additions & 10 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3025,13 +3025,13 @@ TypeConverter::getConstantOverrideInfo(TypeExpansionContext context,
// convention appropriately before calling the derived method.
bool hasGenericRequirementDifference = false;

auto derivedSig = derived.getDecl()->getAsGenericContext()
const auto derivedSig = derived.getDecl()->getAsGenericContext()
->getGenericSignature();
auto genericSig = Context.getOverrideGenericSignature(base.getDecl(),
derived.getDecl());
if (genericSig) {
const auto overrideSig = Context.getOverrideGenericSignature(
base.getDecl(), derived.getDecl());
if (overrideSig) {
hasGenericRequirementDifference =
!genericSig->requirementsNotSatisfiedBy(derivedSig).empty();
!overrideSig->requirementsNotSatisfiedBy(derivedSig).empty();
}

auto baseInfo = getConstantInfo(context, base);
Expand Down Expand Up @@ -3064,22 +3064,23 @@ TypeConverter::getConstantOverrideInfo(TypeExpansionContext context,
overrideInterfaceTy = derivedInfo.FormalType;
}

if (genericSig && !genericSig->areAllParamsConcrete()) {
if (overrideSig) {
overrideInterfaceTy =
cast<AnyFunctionType>(
GenericFunctionType::get(genericSig,
GenericFunctionType::get(overrideSig,
overrideInterfaceTy->getParams(),
overrideInterfaceTy->getResult(),
overrideInterfaceTy->getExtInfo())
->getCanonicalType());
}

// Build the lowered AST function type for the class method call.
auto bridgedTypes = getLoweredFormalTypes(derived, overrideInterfaceTy);
auto bridgedUncurriedTy =
getLoweredFormalTypes(derived, overrideInterfaceTy).Uncurried;

// Build the SILFunctionType for the class method call.
CanSILFunctionType fnTy = getNativeSILFunctionType(
*this, context, basePattern, bridgedTypes.Uncurried, base, derived,
*this, context, basePattern, bridgedUncurriedTy, base, derived,
/*reqt subs*/ None, ProtocolConformanceRef());

// Build the SILConstantInfo and cache it.
Expand All @@ -3088,7 +3089,7 @@ TypeConverter::getConstantOverrideInfo(TypeExpansionContext context,
auto result = ::new (resultBuf) SILConstantInfo{
overrideInterfaceTy,
basePattern,
bridgedTypes.Uncurried,
bridgedUncurriedTy,
fnTy};

auto inserted = ConstantOverrideTypes.insert({{derived, base}, result});
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3744,7 +3744,8 @@ SILGenFunction::emitVTableThunk(SILDeclRef base,

auto subs = getForwardingSubstitutionMap();
if (auto genericSig = derivedFTy->getInvocationGenericSignature()) {
subs = SubstitutionMap::get(genericSig, subs);
subs = SubstitutionMap::get(genericSig, subs,
/*fallBackToModuleConformanceLookup*/true);

derivedFTy =
derivedFTy->substGenericArgs(SGM.M, subs, getTypeExpansionContext());
Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/vtable_thunks_reabstraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,14 @@ class ConcreteOptional: Opaque<S?> {
class GenericBase<T> {
func doStuff<U>(t: T, u: U) {}
init<U>(t: T, u: U) {}

func doStuff2<U>(u: U) where T == Int {}
}

// CHECK-LABEL: sil_vtable GenericBase {
// CHECK-NEXT: #GenericBase.doStuff: <T><U> (GenericBase<T>) -> (T, U) -> () : @$s27vtable_thunks_reabstraction11GenericBaseC7doStuff1t1uyx_qd__tlF // GenericBase.doStuff<A>(t:u:)
// CHECK-NEXT: #GenericBase.init!allocator: <T><U> (GenericBase<T>.Type) -> (T, U) -> GenericBase<T> : @$s27vtable_thunks_reabstraction11GenericBaseC1t1uACyxGx_qd__tclufC
// CHECK-NEXT: #GenericBase.doStuff2: <T where T == Int><U> (GenericBase<T>) -> (U) -> () : @$s27vtable_thunks_reabstraction11GenericBaseC8doStuff21uyqd___tSiRszlF
// CHECK-NEXT: #GenericBase.deinit!deallocator: @$s27vtable_thunks_reabstraction11GenericBaseCfD // GenericBase.__deallocating_deinit
// CHECK-NEXT: }

Expand All @@ -504,6 +507,7 @@ class ConcreteSub : GenericBase<Int> {
// CHECK-LABEL: sil_vtable ConcreteSub {
// CHECK-NEXT: #GenericBase.doStuff: <T><U> (GenericBase<T>) -> (T, U) -> () : @$s27vtable_thunks_reabstraction11ConcreteSubC7doStuff1t1uySi_xtlFAA11GenericBaseCAdeFyx_qd__tlFTV [override] // vtable thunk for GenericBase.doStuff<A>(t:u:) dispatching to ConcreteSub.doStuff<A>(t:u:)
// CHECK-NEXT: #GenericBase.init!allocator: <T><U> (GenericBase<T>.Type) -> (T, U) -> GenericBase<T> : @$s27vtable_thunks_reabstraction11ConcreteSubC1t1uACSi_xtclufCAA11GenericBaseCAdeGyxGx_qd__tclufCTV [override]
// CHECK-NEXT: #GenericBase.doStuff2: <T where T == Int><U> {{.*}}F [inherited]
// CHECK-NEXT: #ConcreteSub.deinit!deallocator: @$s27vtable_thunks_reabstraction11ConcreteSubCfD // ConcreteSub.__deallocating_deinit
// CHECK-NEXT: }

Expand Down Expand Up @@ -543,6 +547,7 @@ class MoreGenericSub1<T, TT> : GenericBase<T> {
// CHECK-LABEL: sil_vtable MoreGenericSub1 {
// CHECK-NEXT: #GenericBase.doStuff: <T><U> (GenericBase<T>) -> (T, U) -> () : @$s27vtable_thunks_reabstraction15MoreGenericSub1C7doStuff1t1uyx_qd__tlF [override] // MoreGenericSub1.doStuff<A>(t:u:)
// CHECK-NEXT: #GenericBase.init!allocator: <T><U> (GenericBase<T>.Type) -> (T, U) -> GenericBase<T> : @$s27vtable_thunks_reabstraction15MoreGenericSub1C1t1uACyxq_Gx_qd__tclufC [override]
// CHECK-NEXT: #GenericBase.doStuff2: <T where T == Int><U> {{.*}}F [inherited]
// CHECK-NEXT: #MoreGenericSub1.deinit!deallocator: @$s27vtable_thunks_reabstraction15MoreGenericSub1CfD // MoreGenericSub1.__deallocating_deinit
// CHECK-NEXT: }

Expand All @@ -555,5 +560,18 @@ class MoreGenericSub2<TT, T> : GenericBase<T> {
// CHECK-LABEL: sil_vtable MoreGenericSub2 {
// CHECK-NEXT: #GenericBase.doStuff: <T><U> (GenericBase<T>) -> (T, U) -> () : @$s27vtable_thunks_reabstraction15MoreGenericSub2C7doStuff1t1uyq__qd__tlF [override] // MoreGenericSub2.doStuff<A>(t:u:)
// CHECK-NEXT: #GenericBase.init!allocator: <T><U> (GenericBase<T>.Type) -> (T, U) -> GenericBase<T> : @$s27vtable_thunks_reabstraction15MoreGenericSub2C1t1uACyxq_Gq__qd__tclufC [override]
// CHECK-NEXT: #GenericBase.doStuff2: <T where T == Int><U> {{.*}}F [inherited]
// CHECK-NEXT: #MoreGenericSub2.deinit!deallocator: @$s27vtable_thunks_reabstraction15MoreGenericSub2CfD // MoreGenericSub2.__deallocating_deinit
// CHECK-NEXT: }

class GenericSub2<T>: GenericBase<T> {
override func doStuff2<U>(u: U) where T: FixedWidthInteger {}
}

// CHECK-LABEL: sil_vtable GenericSub2 {
// CHECK-NEXT: #GenericBase.doStuff!1: <T><U> {{.*}}F [inherited]
// CHECK-NEXT: #GenericBase.init
// CHECK-NEXT: #GenericBase.doStuff2!1: <T where T == Int><U> (GenericBase<T>) -> (U) -> () : @$s27vtable_thunks_reabstraction11GenericSub2C8doStuff21uyqd___ts17FixedWidthIntegerRzlFAA0D4BaseCAdEyqd___tSiRszlFTV [override]
// CHECK-NEXT: #GenericSub2.doStuff2!1: <T where T : FixedWidthInteger><U> (GenericSub2<T>) -> (U) -> () : @$s27vtable_thunks_reabstraction11GenericSub2C8doStuff21uyqd___ts17FixedWidthIntegerRzlF
// CHECK-NEXT: #GenericSub2.deinit
// CHECK-NEXT: }