Skip to content

AST: Fix existential erasure of long member types #81181

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
Apr 30, 2025
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
1 change: 0 additions & 1 deletion include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ struct OuterSubstitutions {
SubstitutionMap subs;
unsigned depth;

bool isUnsubstitutedTypeParameter(Type type) const;
Type operator()(SubstitutableType *type) const;
ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
Expand Down
10 changes: 7 additions & 3 deletions lib/AST/GenericEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,13 @@ GenericEnvironment::maybeApplyOuterContextSubstitutions(Type type) const {
case Kind::OpenedExistential:
case Kind::OpenedElement:
case Kind::Opaque: {
OuterSubstitutions replacer{
getOuterSubstitutions(), getGenericSignature()->getMaxDepth()};
return type.subst(replacer, replacer);
if (auto subs = getOuterSubstitutions()) {
OuterSubstitutions replacer{subs,
getGenericSignature()->getMaxDepth()};
return type.subst(replacer, replacer);
}

return type;
}
}
}
Expand Down
33 changes: 17 additions & 16 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,21 +663,8 @@ SubstitutionMap SubstitutionMap::mapIntoTypeExpansionContext(
SubstFlags::PreservePackExpansionLevel);
}

bool OuterSubstitutions::isUnsubstitutedTypeParameter(Type type) const {
if (!type->isTypeParameter())
return false;

if (auto depMemTy = type->getAs<DependentMemberType>())
return isUnsubstitutedTypeParameter(depMemTy->getBase());

if (auto genericParam = type->getAs<GenericTypeParamType>())
return genericParam->getDepth() >= depth;

return false;
}

Type OuterSubstitutions::operator()(SubstitutableType *type) const {
if (isUnsubstitutedTypeParameter(type))
if (cast<GenericTypeParamType>(type)->getDepth() >= depth)
return Type(type);

return QuerySubstitutionMap{subs}(type);
Expand All @@ -687,9 +674,23 @@ ProtocolConformanceRef OuterSubstitutions::operator()(
CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
if (isUnsubstitutedTypeParameter(dependentType))
return ProtocolConformanceRef::forAbstract(
auto sig = subs.getGenericSignature();
if (!sig->isValidTypeParameter(dependentType) ||
!sig->requiresProtocol(dependentType, conformedProtocol)) {
// FIXME: We need the isValidTypeParameter() check instead of just looking
// at the root generic parameter because in the case of an existential
// environment, the reduced type of a member type of Self might be an outer
// type parameter that is not formed from the outer generic signature's
// conformance requirements. Ideally, we'd either add these supplementary
// conformance requirements to the generalization signature, or we would
// store the supplementary conformances directly in the generic environment
// somehow.
//
// Once we check for that and handle it properly, the lookupConformance()
// can become a forAbstract().
return swift::lookupConformance(
conformingReplacementType, conformedProtocol);
}

return LookUpConformanceInSubstitutionMap(subs)(
dependentType, conformingReplacementType, conformedProtocol);
Expand Down
9 changes: 5 additions & 4 deletions lib/Sema/OpenedExistentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,11 @@ Type swift::typeEraseOpenedExistentialReference(

auto applyOuterSubstitutions = [&](Type t) -> Type {
if (t->hasTypeParameter()) {
auto outerSubs = existentialSig.Generalization;
unsigned depth = existentialSig.OpenedSig->getMaxDepth();
OuterSubstitutions replacer{outerSubs, depth};
return t.subst(replacer, replacer);
if (auto outerSubs = existentialSig.Generalization) {
unsigned depth = existentialSig.OpenedSig->getMaxDepth();
OuterSubstitutions replacer{outerSubs, depth};
return t.subst(replacer, replacer);
}
}

return t;
Expand Down
25 changes: 25 additions & 0 deletions test/SILGen/existential_erasure_length_2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-swift-emit-silgen %s

protocol N {
associatedtype A: N
}

protocol P<A> {
associatedtype A: N
associatedtype B

func f0(_: A) -> B
func f1(_: A.A) -> B
func f2(_: A.A.A) -> B
}

struct G<T>: N {
typealias A = G<G<T>>
}

func call(x: any P<G<Int>>) -> (Any, Any, Any) {
let y0 = x.f0(G<Int>())
let y1 = x.f1(G<G<Int>>())
let y2 = x.f2(G<G<G<Int>>>())
return (y0, y1, y2)
}