|
19 | 19 | #include "SILGenFunction.h"
|
20 | 20 | #include "swift/AST/ConformanceLookup.h"
|
21 | 21 | #include "swift/AST/GenericEnvironment.h"
|
| 22 | +#include "swift/AST/LocalArchetypeRequirementCollector.h" |
22 | 23 | #include "swift/Basic/Assertions.h"
|
23 | 24 | #include "swift/SIL/AbstractionPatternGenerators.h"
|
24 | 25 |
|
@@ -99,54 +100,36 @@ class IndirectOpenedSelfCleanup final : public Cleanup {
|
99 | 100 | /// dependent type, and return a substitution map with generic parameters
|
100 | 101 | /// corresponding to each distinct root opened archetype.
|
101 | 102 | static std::pair<CanType, SubstitutionMap>
|
102 |
| -mapTypeOutOfOpenedExistentialContext(CanType t) { |
| 103 | +mapTypeOutOfOpenedExistentialContext(CanType t, GenericEnvironment *genericEnv) { |
103 | 104 | auto &ctx = t->getASTContext();
|
104 | 105 |
|
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 | + }); |
118 | 116 |
|
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(); |
121 | 122 | }
|
122 | 123 |
|
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); |
145 | 128 |
|
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); |
150 | 133 |
|
151 | 134 | return std::make_pair(mappedTy->getCanonicalType(), mappedSubs);
|
152 | 135 | }
|
@@ -189,7 +172,7 @@ class IndirectOpenedSelfResultPlan final : public ResultPlan {
|
189 | 172 | CanType layoutTy;
|
190 | 173 | SubstitutionMap layoutSubs;
|
191 | 174 | std::tie(layoutTy, layoutSubs) =
|
192 |
| - mapTypeOutOfOpenedExistentialContext(resultTy); |
| 175 | + mapTypeOutOfOpenedExistentialContext(resultTy, SGF.F.getGenericEnvironment()); |
193 | 176 |
|
194 | 177 | CanGenericSignature layoutSig =
|
195 | 178 | layoutSubs.getGenericSignature().getCanonicalSignature();
|
|
0 commit comments