Skip to content

Commit 7e4e79c

Browse files
committed
AST: Simplify SubstitutionMap::get()
1 parent 851a829 commit 7e4e79c

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ class SubstitutionMap {
104104
ArrayRef<Type> replacementTypes,
105105
LookupConformanceFn lookupConformance);
106106

107+
/// Build a substitution map from the substitutions represented by
108+
/// the given in-flight substitution.
109+
///
110+
/// This function should generally only be used by the substitution
111+
/// subsystem.
112+
static SubstitutionMap get(GenericSignature genericSig,
113+
ArrayRef<Type> replacementTypes,
114+
InFlightSubstitution &IFS);
115+
107116
/// Build a substitution map from the substitutions represented by
108117
/// the given in-flight substitution.
109118
///

include/swift/AST/TypeTransform.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,7 @@ case TypeKind::Id:
998998
return subs;
999999

10001000
auto sig = subs.getGenericSignature();
1001-
return SubstitutionMap::get(sig,
1002-
QueryReplacementTypeArray{sig, newSubs},
1003-
LookUpConformanceInModule());
1001+
return SubstitutionMap::get(sig, newSubs, LookUpConformanceInModule());
10041002
}
10051003

10061004
CanType transformSILField(CanType fieldTy, TypePosition pos) {

lib/AST/SubstitutionMap.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,28 @@ SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
168168
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
169169
ArrayRef<Type> types,
170170
LookupConformanceFn lookupConformance) {
171-
return get(genericSig,
172-
QueryReplacementTypeArray{genericSig, types},
173-
lookupConformance);
171+
QueryReplacementTypeArray subs{genericSig, types};
172+
InFlightSubstitution IFS(subs, lookupConformance, std::nullopt);
173+
return get(genericSig, types, IFS);
174+
}
175+
176+
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
177+
ArrayRef<Type> types,
178+
InFlightSubstitution &IFS) {
179+
// Form the stored conformances.
180+
SmallVector<ProtocolConformanceRef, 4> conformances;
181+
for (const auto &req : genericSig.getRequirements()) {
182+
if (req.getKind() != RequirementKind::Conformance) continue;
183+
184+
CanType depTy = req.getFirstType()->getCanonicalType();
185+
auto replacement = depTy.subst(IFS);
186+
auto *proto = req.getProtocolDecl();
187+
auto conformance = IFS.lookupConformance(depTy, replacement, proto,
188+
/*level=*/0);
189+
conformances.push_back(conformance);
190+
}
191+
192+
return SubstitutionMap(genericSig, types, conformances);
174193
}
175194

176195
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
@@ -185,29 +204,17 @@ SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
185204

186205
for (auto *gp : genericSig.getGenericParams()) {
187206
// Record the replacement.
188-
Type replacement = Type(gp).subst(IFS);
189-
190-
assert((!replacement || replacement->hasError() ||
207+
Type replacement = IFS.substType(gp, /*level=*/0);
208+
if (!replacement)
209+
replacement = ErrorType::get(gp->getASTContext());
210+
assert((replacement->hasError() ||
191211
gp->isParameterPack() == replacement->is<PackType>()) &&
192212
"replacement for pack parameter must be a pack type");
193213

194214
replacementTypes.push_back(replacement);
195215
}
196216

197-
// Form the stored conformances.
198-
SmallVector<ProtocolConformanceRef, 4> conformances;
199-
for (const auto &req : genericSig.getRequirements()) {
200-
if (req.getKind() != RequirementKind::Conformance) continue;
201-
202-
CanType depTy = req.getFirstType()->getCanonicalType();
203-
auto replacement = depTy.subst(IFS);
204-
auto *proto = req.getProtocolDecl();
205-
auto conformance = IFS.lookupConformance(depTy, replacement, proto,
206-
/*level=*/0);
207-
conformances.push_back(conformance);
208-
}
209-
210-
return SubstitutionMap(genericSig, replacementTypes, conformances);
217+
return SubstitutionMap::get(genericSig, replacementTypes, IFS);
211218
}
212219

213220
Type SubstitutionMap::lookupSubstitution(GenericTypeParamType *genericParam) const {

lib/AST/TypeSubstitution.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ SubstitutionMap TypeBase::getContextSubstitutionMap() {
808808
std::reverse(replacementTypes.begin(), replacementTypes.end());
809809

810810
auto subMap = SubstitutionMap::get(
811-
genericSig,
812-
QueryReplacementTypeArray{genericSig, replacementTypes},
811+
genericSig, replacementTypes,
813812
LookUpConformanceInModule());
814813

815814
nominalTy->ContextSubMap = subMap;

0 commit comments

Comments
 (0)