Skip to content

Commit a209ff8

Browse files
committed
AST: Remove origType parameter from ProtocolConformanceRef::subst()
1 parent 7399eeb commit a209ff8

13 files changed

+38
-76
lines changed

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,19 @@ class ProtocolConformanceRef {
185185
ProtocolDecl *getProtocol() const;
186186

187187
/// Apply a substitution to the conforming type.
188-
ProtocolConformanceRef subst(Type origType, SubstitutionMap subMap,
188+
ProtocolConformanceRef subst(SubstitutionMap subMap,
189189
SubstOptions options = std::nullopt) const;
190190

191191
/// Apply a substitution to the conforming type.
192-
ProtocolConformanceRef subst(Type origType, TypeSubstitutionFn subs,
192+
ProtocolConformanceRef subst(TypeSubstitutionFn subs,
193193
LookupConformanceFn conformances,
194194
SubstOptions options = std::nullopt) const;
195195

196196
/// Apply a substitution to the conforming type.
197197
///
198198
/// This function should generally not be used outside of the substitution
199199
/// subsystem.
200-
ProtocolConformanceRef subst(Type origType,
201-
InFlightSubstitution &IFS) const;
200+
ProtocolConformanceRef subst(InFlightSubstitution &IFS) const;
202201

203202
/// Map contextual types to interface types in the conformance.
204203
ProtocolConformanceRef mapConformanceOutOfContext() const;

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
564564
if (Functor.hasLocalArchetypes())
565565
options |= SubstFlags::SubstituteLocalArchetypes;
566566

567-
C = C.subst(Ty, Functor, Functor, options);
567+
C = C.subst(Functor, Functor, options);
568568
if (asImpl().shouldSubstOpaqueArchetypes())
569569
Ty = Ty.subst(Functor, Functor, options);
570570
}

lib/AST/GenericEnvironment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ std::pair<Type, ProtocolConformanceRef>
766766
GenericEnvironment::mapConformanceRefIntoContext(
767767
Type conformingInterfaceType,
768768
ProtocolConformanceRef conformance) const {
769-
auto contextConformance = conformance.subst(conformingInterfaceType,
769+
auto contextConformance = conformance.subst(
770770
QueryInterfaceTypeSubstitutions(this),
771771
LookUpConformanceInModule());
772772

lib/AST/PackConformance.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,12 @@ PackConformance::subst(InFlightSubstitution &IFS) const {
199199
// Just substitute the conformance. We don't directly represent
200200
// pack expansion conformances here; it's sort of implicit in the
201201
// corresponding pack element type.
202-
substConformances.push_back(
203-
origConformances[i].subst(origExpansion->getPatternType(), IFS));
202+
substConformances.push_back(origConformances[i].subst(IFS));
204203
});
205204
} else {
206205
// Substitute a scalar element of the original pack.
207206
substElementTypes.push_back(origElementType.subst(IFS));
208-
209-
substConformances.push_back(
210-
origConformances[i].subst(origElementType, IFS));
207+
substConformances.push_back(origConformances[i].subst(IFS));
211208
}
212209
}
213210

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -896,14 +896,7 @@ SpecializedProtocolConformance::getAssociatedConformance(Type assocType,
896896
ProtocolConformanceRef conformance =
897897
GenericConformance->getAssociatedConformance(assocType, protocol);
898898

899-
auto subMap = getSubstitutionMap();
900-
901-
Type origType =
902-
(conformance.isConcrete()
903-
? conformance.getConcrete()->getType()
904-
: GenericConformance->getAssociatedType(assocType));
905-
906-
return conformance.subst(origType, subMap);
899+
return conformance.subst(getSubstitutionMap());
907900
}
908901

909902
ConcreteDeclRef

lib/AST/ProtocolConformanceRef.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,22 @@ ProtocolDecl *ProtocolConformanceRef::getProtocol() const {
6767
}
6868

6969
ProtocolConformanceRef
70-
ProtocolConformanceRef::subst(Type origType,
71-
SubstitutionMap subMap,
70+
ProtocolConformanceRef::subst(SubstitutionMap subMap,
7271
SubstOptions options) const {
7372
InFlightSubstitutionViaSubMap IFS(subMap, options);
74-
return subst(origType, IFS);
73+
return subst(IFS);
7574
}
7675

7776
ProtocolConformanceRef
78-
ProtocolConformanceRef::subst(Type origType,
79-
TypeSubstitutionFn subs,
77+
ProtocolConformanceRef::subst(TypeSubstitutionFn subs,
8078
LookupConformanceFn conformances,
8179
SubstOptions options) const {
8280
InFlightSubstitution IFS(subs, conformances, options);
83-
return subst(origType, IFS);
81+
return subst(IFS);
8482
}
8583

8684
ProtocolConformanceRef
87-
ProtocolConformanceRef::subst(Type origType, InFlightSubstitution &IFS) const {
85+
ProtocolConformanceRef::subst(InFlightSubstitution &IFS) const {
8886
if (isInvalid())
8987
return *this;
9088

@@ -93,36 +91,28 @@ ProtocolConformanceRef::subst(Type origType, InFlightSubstitution &IFS) const {
9391
if (isPack())
9492
return getPack()->subst(IFS);
9593

96-
ASSERT(isAbstract());
97-
auto *proto = getProtocol();
94+
auto *abstract = getAbstract();
95+
auto origType = abstract->getType();
96+
auto *proto = abstract->getProtocol();
9897

9998
// If the type is an opaque archetype, the conformance will remain abstract,
10099
// unless we're specifically substituting opaque types.
101-
if (origType->getAs<OpaqueTypeArchetypeType>()) {
102-
if (!IFS.shouldSubstituteOpaqueArchetypes()) {
103-
return forAbstract(origType.subst(IFS), proto);
104-
}
100+
if (origType->is<OpaqueTypeArchetypeType>() &&
101+
!IFS.shouldSubstituteOpaqueArchetypes()) {
102+
return forAbstract(origType.subst(IFS), proto);
105103
}
106104

107-
// FIXME: Handle local archetypes as above!
108-
109-
// Otherwise, compute the substituted type.
110-
auto substType = origType.subst(IFS);
111-
112-
// If the type is an existential, it must be self-conforming.
113-
// FIXME: This feels like it's in the wrong place.
114-
if (substType->isExistentialType()) {
115-
auto optConformance =
116-
lookupConformance(substType, proto, /*allowMissing=*/true);
117-
if (optConformance)
118-
return optConformance;
119-
120-
return ProtocolConformanceRef::forInvalid();
105+
// If the type is a local archetype, the conformance will remain abstract,
106+
// unless we're specifically substituting local types.
107+
if (origType->is<LocalArchetypeType>() &&
108+
!IFS.shouldSubstituteLocalArchetypes()) {
109+
return forAbstract(origType.subst(IFS), proto);
121110
}
122111

123112
// Local conformance lookup into the substitution map.
124113
// FIXME: Pack element level?
125-
return IFS.lookupConformance(origType->getCanonicalType(), substType, proto,
114+
return IFS.lookupConformance(origType->getCanonicalType(),
115+
origType.subst(IFS), proto,
126116
/*level=*/0);
127117
}
128118

lib/AST/SubstitutionMap.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,10 @@ SubstitutionMap SubstitutionMap::subst(InFlightSubstitution &IFS) const {
369369

370370
auto genericSig = getGenericSignature();
371371
for (const auto &req : genericSig.getRequirements()) {
372-
if (req.getKind() != RequirementKind::Conformance) continue;
373-
374-
auto conformance = oldConformances[0];
375-
376-
// Fast path for concrete case -- we don't need to compute substType
377-
// at all.
378-
if (conformance.isConcrete() &&
379-
!IFS.shouldSubstituteOpaqueArchetypes()) {
380-
newConformances.push_back(
381-
ProtocolConformanceRef(conformance.getConcrete()->subst(IFS)));
382-
} else {
383-
auto origType = req.getFirstType();
384-
auto substType = origType.subst(*this);
372+
if (req.getKind() != RequirementKind::Conformance)
373+
continue;
385374

386-
newConformances.push_back(conformance.subst(substType, IFS));
387-
}
388-
375+
newConformances.push_back(oldConformances[0].subst(IFS));
389376
oldConformances = oldConformances.slice(1);
390377
}
391378

lib/AST/TypeSubstitution.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ static ProtocolConformanceRef substOpaqueTypesWithUnderlyingTypesRec(
10941094
llvm::DenseSet<ReplaceOpaqueTypesWithUnderlyingTypes::SeenDecl> &decls) {
10951095
ReplaceOpaqueTypesWithUnderlyingTypes replacer(inContext, contextExpansion,
10961096
isWholeModuleContext, decls);
1097-
return ref.subst(origType, replacer, replacer,
1097+
return ref.subst(replacer, replacer,
10981098
SubstFlags::SubstituteOpaqueArchetypes |
10991099
SubstFlags::PreservePackExpansionLevel);
11001100
}
@@ -1104,7 +1104,7 @@ ProtocolConformanceRef swift::substOpaqueTypesWithUnderlyingTypes(
11041104
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
11051105
context.getContext(), context.getResilienceExpansion(),
11061106
context.isWholeModuleContext());
1107-
return ref.subst(origType, replacer, replacer,
1107+
return ref.subst(replacer, replacer,
11081108
SubstFlags::SubstituteOpaqueArchetypes);
11091109
}
11101110

@@ -1159,8 +1159,7 @@ operator()(CanType maybeOpaqueType, Type replacementType,
11591159
auto partialSubstRef =
11601160
subs->lookupConformance(archetype->getInterfaceType()->getCanonicalType(),
11611161
protocol);
1162-
auto substRef =
1163-
partialSubstRef.subst(partialSubstTy, outerSubs);
1162+
auto substRef = partialSubstRef.subst(outerSubs);
11641163

11651164
// If the type still contains opaque types, recur.
11661165
if (substTy->hasOpaqueArchetype()) {

lib/IRGen/GenReflection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ IRGenModule::emitWitnessTableRefString(CanType type,
602602
type = genericEnv->mapTypeIntoContext(type)->getCanonicalType();
603603
}
604604
if (origType->hasTypeParameter()) {
605-
conformance = conformance.subst(origType,
605+
conformance = conformance.subst(
606606
genericEnv->getForwardingSubstitutionMap());
607607
}
608608
auto ret = emitWitnessTableRef(IGF, type, conformance);

lib/SIL/IR/SILTypeSubstitution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class SILTypeSubstituter :
282282
selfType = next;
283283
}
284284

285-
auto substConformance = conformance.subst(selfType, IFS);
285+
auto substConformance = conformance.subst(IFS);
286286

287287
// Substitute the underlying conformance of opaque type archetypes if we
288288
// should look through opaque archetypes.

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4320,9 +4320,8 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
43204320
auto hashable = index.Hashable;
43214321
if (genericEnv) {
43224322
formalTy = genericEnv->mapTypeIntoContext(formalTy)->getCanonicalType();
4323-
hashable = hashable.subst(index.FormalType,
4324-
[&](Type t) -> Type { return genericEnv->mapTypeIntoContext(t); },
4325-
LookUpConformanceInModule());
4323+
hashable = hashable.subst(
4324+
genericEnv->getForwardingSubstitutionMap());
43264325
}
43274326

43284327
// Set up a substitution of Self => IndexType.

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,8 +2906,7 @@ bool AssociatedTypeInference::checkCurrentTypeWitnesses(
29062906
if (auto *genericEnv = conformance->getGenericEnvironment()) {
29072907
typeInContext = genericEnv->mapTypeIntoContext(typeInContext);
29082908
conformanceInContext =
2909-
conformanceInContext.subst(conformance->getType(),
2910-
genericEnv->getForwardingSubstitutionMap());
2909+
conformanceInContext.subst(genericEnv->getForwardingSubstitutionMap());
29112910
}
29122911

29132912
auto substitutions =

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5237,8 +5237,7 @@ static void ensureRequirementsAreSatisfied(ASTContext &ctx,
52375237
if (auto *genericEnv = conformance->getGenericEnvironment()) {
52385238
typeInContext = genericEnv->mapTypeIntoContext(typeInContext);
52395239
conformanceInContext =
5240-
conformanceInContext.subst(conformance->getType(),
5241-
genericEnv->getForwardingSubstitutionMap());
5240+
conformanceInContext.subst(genericEnv->getForwardingSubstitutionMap());
52425241
}
52435242
auto substitutions = SubstitutionMap::getProtocolSubstitutions(
52445243
proto, typeInContext, conformanceInContext);

0 commit comments

Comments
 (0)