Skip to content

IRGen: Workaround for type substitution limitation #76486

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 2 commits into from
Sep 16, 2024
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
32 changes: 18 additions & 14 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,24 @@ class PolymorphicConvention {
FulfillmentMap Fulfillments;

GenericSignature::RequiredProtocols getRequiredProtocols(Type t) {
// FIXME: We need to rework this to use archetypes instead of interface
// types, or fix the bad interaction between interface type substitution
// and concretized conformance requirements. Then we can remove the hack
// from getReducedType() to handle this case, and also stop calling
// getReducedType() here.
t = Generics.getReducedType(t);
if (!t->isTypeParameter())
return {};

return Generics->getRequiredProtocols(t);
}

CanType getSuperclassBound(Type t) {
// See above.
t = Generics.getReducedType(t);
if (!t->isTypeParameter())
return CanType();

if (auto superclassTy = Generics->getSuperclassBound(t))
return superclassTy->getCanonicalType();
return CanType();
Expand Down Expand Up @@ -143,8 +157,6 @@ class PolymorphicConvention {
}

private:
void initGenerics();

template <typename ...Args>
void considerNewTypeSource(IsExact_t isExact, MetadataSource::Kind kind,
CanType type, Args... args);
Expand Down Expand Up @@ -199,9 +211,8 @@ class PolymorphicConvention {
PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,
CanSILFunctionType fnType,
bool considerParameterSources = true)
: IGM(IGM), M(*IGM.getSwiftModule()), FnType(fnType){
initGenerics();

: IGM(IGM), M(*IGM.getSwiftModule()), FnType(fnType),
Generics(fnType->getInvocationGenericSignature()) {
auto rep = fnType->getRepresentation();

if (fnType->isPseudogeneric()) {
Expand Down Expand Up @@ -249,11 +260,8 @@ PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,

void PolymorphicConvention::addPseudogenericFulfillments() {
enumerateRequirements([&](GenericRequirement reqt) {
auto archetype = Generics.getGenericEnvironment()
->mapTypeIntoContext(reqt.getTypeParameter())
->getAs<ArchetypeType>();
assert(archetype && "did not get an archetype by mapping param?");
auto erasedTypeParam = archetype->getExistentialType()->getCanonicalType();
auto erasedTypeParam = Generics->getExistentialType(reqt.getTypeParameter())
->getCanonicalType();
Sources.emplace_back(MetadataSource::Kind::ErasedTypeMetadata,
reqt.getTypeParameter(), erasedTypeParam);

Expand Down Expand Up @@ -319,10 +327,6 @@ enumerateUnfulfilledRequirements(const RequirementCallback &callback) {
});
}

void PolymorphicConvention::initGenerics() {
Generics = FnType->getInvocationGenericSignature();
}

template <typename ...Args>
void PolymorphicConvention::considerNewTypeSource(IsExact_t isExact,
MetadataSource::Kind kind,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-swift-frontend -emit-ir %s

// https://github.com/swiftlang/swift/issues/76479

public struct G<T> where T: P1, T.A: P1, T.A.A: P1 {}

public protocol P1 {
associatedtype A
}

extension Int: P1 {
public typealias A = Int
}

public protocol P2 {
func f()
}

extension G: P2 where T.A == Int {
public func f() {}
}