Skip to content

Commit ef0164d

Browse files
committed
AST: Small cleanup for InFlightSubstitution
1 parent 7b0ad4a commit ef0164d

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

include/swift/AST/InFlightSubstitution.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class InFlightSubstitution {
3939
};
4040
SmallVector<ActivePackExpansion, 4> ActivePackExpansions;
4141

42+
Type projectLaneFromPackType(
43+
Type substType, unsigned level);
44+
ProtocolConformanceRef projectLaneFromPackConformance(
45+
PackConformance *substPackConf, unsigned level);
46+
4247
public:
4348
InFlightSubstitution(TypeSubstitutionFn substType,
4449
LookupConformanceFn lookupConformance,

lib/AST/TypeSubstitution.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,14 @@ void InFlightSubstitution::expandPackExpansionShape(Type origShape,
185185
ActivePackExpansions.pop_back();
186186
}
187187

188-
Type InFlightSubstitution::substType(SubstitutableType *origType,
189-
unsigned level) {
190-
auto substType = BaselineSubstType(origType);
191-
if (!substType)
192-
return Type();
188+
Type InFlightSubstitution::projectLaneFromPackType(Type substType,
189+
unsigned level) {
190+
auto outerExpansions = ArrayRef(ActivePackExpansions).drop_back(level);
191+
auto innerExpansions = ArrayRef(ActivePackExpansions).take_back(level);
193192

194193
// FIXME: All the logic around 'level' is probably slightly wrong, and in
195194
// the unlikely event that it is correct, at the very least warrants a
196195
// detailed explanation.
197-
198-
if (ActivePackExpansions.empty())
199-
return substType->increasePackElementLevel(level);
200-
201-
auto outerExpansions = ArrayRef(ActivePackExpansions).drop_back(level);
202-
auto innerExpansions = ArrayRef(ActivePackExpansions).take_back(level);
203-
204196
unsigned outerLevel = 0;
205197
if (!getOptions().contains(SubstFlags::PreservePackExpansionLevel)) {
206198
for (const auto &activeExpansion : outerExpansions) {
@@ -241,17 +233,24 @@ Type InFlightSubstitution::substType(SubstitutableType *origType,
241233
}
242234
}
243235

244-
ProtocolConformanceRef
245-
InFlightSubstitution::lookupConformance(Type dependentType,
246-
ProtocolDecl *proto,
247-
unsigned level) {
248-
auto substConfRef = BaselineLookupConformance(*this, dependentType, proto);
249-
if (!substConfRef ||
250-
ActivePackExpansions.empty() ||
251-
!substConfRef.isPack())
252-
return substConfRef;
236+
Type InFlightSubstitution::substType(SubstitutableType *origType,
237+
unsigned level) {
238+
auto substType = BaselineSubstType(origType);
239+
if (!substType)
240+
return Type();
241+
242+
if (!ActivePackExpansions.empty())
243+
substType = projectLaneFromPackType(substType, level);
244+
else
245+
substType = substType->increasePackElementLevel(level);
253246

254-
auto substPackConf = substConfRef.getPack();
247+
return substType;
248+
}
249+
250+
ProtocolConformanceRef
251+
InFlightSubstitution::projectLaneFromPackConformance(
252+
PackConformance *substPackConf,
253+
unsigned level) {
255254
auto substPackPatterns = substPackConf->getPatternConformances();
256255
assert(level < ActivePackExpansions.size() && "too deep");
257256
auto index = ActivePackExpansions[ActivePackExpansions.size() - level - 1]
@@ -262,6 +261,19 @@ InFlightSubstitution::lookupConformance(Type dependentType,
262261
return substPackPatterns[index];
263262
}
264263

264+
ProtocolConformanceRef
265+
InFlightSubstitution::lookupConformance(Type dependentType,
266+
ProtocolDecl *proto,
267+
unsigned level) {
268+
auto substConfRef = BaselineLookupConformance(*this, dependentType, proto);
269+
if (!ActivePackExpansions.empty() && substConfRef.isPack()) {
270+
substConfRef = projectLaneFromPackConformance(
271+
substConfRef.getPack(), level);
272+
}
273+
274+
return substConfRef;
275+
}
276+
265277
namespace {
266278

267279
class TypeSubstituter : public TypeTransform<TypeSubstituter> {

0 commit comments

Comments
 (0)