Skip to content

Commit 2c4f871

Browse files
committed
SILGen: Simplify mapTypeOutOfOpenedExistentialContext()
1 parent 8c14899 commit 2c4f871

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

lib/SILGen/ResultPlan.cpp

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "SILGenFunction.h"
2020
#include "swift/AST/ConformanceLookup.h"
2121
#include "swift/AST/GenericEnvironment.h"
22+
#include "swift/AST/LocalArchetypeRequirementCollector.h"
2223
#include "swift/Basic/Assertions.h"
2324
#include "swift/SIL/AbstractionPatternGenerators.h"
2425

@@ -99,54 +100,36 @@ class IndirectOpenedSelfCleanup final : public Cleanup {
99100
/// dependent type, and return a substitution map with generic parameters
100101
/// corresponding to each distinct root opened archetype.
101102
static std::pair<CanType, SubstitutionMap>
102-
mapTypeOutOfOpenedExistentialContext(CanType t) {
103+
mapTypeOutOfOpenedExistentialContext(CanType t, GenericEnvironment *genericEnv) {
103104
auto &ctx = t->getASTContext();
104105

105-
SmallVector<OpenedArchetypeType *, 4> openedTypes;
106-
t->getRootOpenedExistentials(openedTypes);
107-
108-
SmallVector<GenericTypeParamType *, 2> params;
109-
SmallVector<Requirement, 2> requirements;
110-
for (const unsigned i : indices(openedTypes)) {
111-
auto *param = GenericTypeParamType::get(
112-
/*isParameterPack*/ false, /*depth*/ 0, /*index*/ i, ctx);
113-
params.push_back(param);
114-
115-
Type constraintTy = openedTypes[i]->getExistentialType();
116-
if (auto existentialTy = constraintTy->getAs<ExistentialType>())
117-
constraintTy = existentialTy->getConstraintType();
106+
SmallVector<GenericEnvironment *, 4> capturedEnvs;
107+
t.visit([&](CanType t) {
108+
if (auto local = dyn_cast<LocalArchetypeType>(t)) {
109+
auto *genericEnv = local->getGenericEnvironment();
110+
if (std::find(capturedEnvs.begin(), capturedEnvs.end(), genericEnv)
111+
== capturedEnvs.end()) {
112+
capturedEnvs.push_back(genericEnv);
113+
}
114+
}
115+
});
118116

119-
requirements.emplace_back(RequirementKind::Conformance, param,
120-
constraintTy);
117+
GenericSignature baseGenericSig;
118+
SubstitutionMap forwardingSubs;
119+
if (genericEnv) {
120+
baseGenericSig = genericEnv->getGenericSignature();
121+
forwardingSubs = genericEnv->getForwardingSubstitutionMap();
121122
}
122123

123-
const auto mappedSubs = SubstitutionMap::get(
124-
swift::buildGenericSignature(ctx, nullptr, params, requirements,
125-
/*allowInverses=*/false),
126-
[&](SubstitutableType *t) -> Type {
127-
return openedTypes[cast<GenericTypeParamType>(t)->getIndex()];
128-
},
129-
MakeAbstractConformanceForGenericType());
130-
131-
const auto mappedTy = t.subst(
132-
[&](SubstitutableType *t) -> Type {
133-
auto *archTy = cast<ArchetypeType>(t);
134-
const auto index = std::find(openedTypes.begin(), openedTypes.end(),
135-
archTy->getRoot()) -
136-
openedTypes.begin();
137-
assert(index != openedTypes.end() - openedTypes.begin());
138-
139-
if (auto *dmt =
140-
archTy->getInterfaceType()->getAs<DependentMemberType>()) {
141-
return dmt->substRootParam(params[index],
142-
MakeAbstractConformanceForGenericType(),
143-
std::nullopt);
144-
}
124+
MapLocalArchetypesOutOfContext mapOutOfContext(baseGenericSig, capturedEnvs);
125+
auto mappedTy = t.subst(mapOutOfContext,
126+
MakeAbstractConformanceForGenericType(),
127+
SubstFlags::SubstituteLocalArchetypes);
145128

146-
return params[index];
147-
},
148-
MakeAbstractConformanceForGenericType(),
149-
SubstFlags::SubstituteLocalArchetypes);
129+
auto genericSig = buildGenericSignatureWithCapturedEnvironments(
130+
ctx, baseGenericSig, capturedEnvs);
131+
auto mappedSubs = buildSubstitutionMapWithCapturedEnvironments(
132+
forwardingSubs, genericSig, capturedEnvs);
150133

151134
return std::make_pair(mappedTy->getCanonicalType(), mappedSubs);
152135
}
@@ -189,7 +172,7 @@ class IndirectOpenedSelfResultPlan final : public ResultPlan {
189172
CanType layoutTy;
190173
SubstitutionMap layoutSubs;
191174
std::tie(layoutTy, layoutSubs) =
192-
mapTypeOutOfOpenedExistentialContext(resultTy);
175+
mapTypeOutOfOpenedExistentialContext(resultTy, SGF.F.getGenericEnvironment());
193176

194177
CanGenericSignature layoutSig =
195178
layoutSubs.getGenericSignature().getCanonicalSignature();

0 commit comments

Comments
 (0)