Skip to content

Commit 104a70f

Browse files
committed
AST: Change signature of LookupConformanceFn
Instead of passing in the substituted type, we pass in the InFlightSubstitution. This allows the substituted type to be recovered if needed, but we can now skip computing it for the common case of LookUpConformanceInSubstitutionMap.
1 parent 5fed0ea commit 104a70f

16 files changed

+131
-136
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ class LookUpConformanceInSubstitutionMap {
294294
explicit LookUpConformanceInSubstitutionMap(SubstitutionMap Subs)
295295
: Subs(Subs) {}
296296

297-
ProtocolConformanceRef operator()(CanType dependentType,
298-
Type conformingReplacementType,
299-
ProtocolDecl *conformedProtocol) const;
297+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
298+
Type dependentType,
299+
ProtocolDecl *proto) const;
300300
};
301301

302302
struct OverrideSubsInfo {
@@ -326,8 +326,8 @@ struct LookUpConformanceInOverrideSubs {
326326
explicit LookUpConformanceInOverrideSubs(const OverrideSubsInfo &info)
327327
: info(info) {}
328328

329-
ProtocolConformanceRef operator()(CanType type,
330-
Type substType,
329+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
330+
Type dependentType,
331331
ProtocolDecl *proto) const;
332332
};
333333

@@ -338,9 +338,9 @@ struct OuterSubstitutions {
338338
unsigned depth;
339339

340340
Type operator()(SubstitutableType *type) const;
341-
ProtocolConformanceRef operator()(CanType dependentType,
342-
Type conformingReplacementType,
343-
ProtocolDecl *conformedProtocol) const;
341+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
342+
Type dependentType,
343+
ProtocolDecl *proto) const;
344344
};
345345

346346
} // end namespace swift

include/swift/AST/Type.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ struct QueryTypeSubstitutionMap {
8888
};
8989

9090
/// Function used to resolve conformances.
91-
using GenericFunction = auto(CanType dependentType,
92-
Type conformingReplacementType,
93-
ProtocolDecl *conformedProtocol)
91+
using GenericFunction = auto(InFlightSubstitution &IFS,
92+
Type dependentType,
93+
ProtocolDecl *proto)
9494
-> ProtocolConformanceRef;
9595
using LookupConformanceFn = llvm::function_ref<GenericFunction>;
9696

@@ -100,19 +100,19 @@ class LookUpConformanceInModule {
100100
public:
101101
explicit LookUpConformanceInModule() {}
102102

103-
ProtocolConformanceRef operator()(CanType dependentType,
104-
Type conformingReplacementType,
105-
ProtocolDecl *conformedProtocol) const;
103+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
104+
Type dependentType,
105+
ProtocolDecl *proto) const;
106106
};
107107

108108
/// Functor class suitable for use as a \c LookupConformanceFn that provides
109109
/// only abstract conformances for generic types. Asserts that the replacement
110110
/// type is an opaque generic type.
111111
class MakeAbstractConformanceForGenericType {
112112
public:
113-
ProtocolConformanceRef operator()(CanType dependentType,
114-
Type conformingReplacementType,
115-
ProtocolDecl *conformedProtocol) const;
113+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
114+
Type dependentType,
115+
ProtocolDecl *proto) const;
116116
};
117117

118118
/// Flags that can be passed when substituting into a type.

include/swift/AST/Types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7069,8 +7069,8 @@ class ReplaceOpaqueTypesWithUnderlyingTypes {
70697069
Type operator()(SubstitutableType *maybeOpaqueType) const;
70707070

70717071
/// LookupConformanceFn
7072-
ProtocolConformanceRef operator()(CanType maybeOpaqueType,
7073-
Type replacementType,
7072+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
7073+
Type maybeOpaqueType,
70747074
ProtocolDecl *protocol) const;
70757075

70767076
OpaqueSubstitutionKind
@@ -7108,8 +7108,8 @@ class ReplaceExistentialArchetypesWithConcreteTypes {
71087108
Type operator()(SubstitutableType *type) const;
71097109

71107110
/// LookupConformanceFn
7111-
ProtocolConformanceRef operator()(CanType origType,
7112-
Type substType,
7111+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
7112+
Type origType,
71137113
ProtocolDecl *protocol) const;
71147114

71157115
};

include/swift/SIL/SILCloner.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,23 @@ struct SubstitutionMapWithLocalArchetypes {
8080
return Type(type);
8181
}
8282

83-
ProtocolConformanceRef operator()(CanType origType,
84-
Type substType,
83+
ProtocolConformanceRef operator()(InFlightSubstitution &IFS,
84+
Type origType,
8585
ProtocolDecl *proto) {
86-
if (isa<LocalArchetypeType>(origType))
87-
return swift::lookupConformance(substType, proto);
86+
if (origType->is<LocalArchetypeType>())
87+
return swift::lookupConformance(origType.subst(IFS), proto);
8888

89-
if (isa<PrimaryArchetypeType>(origType) ||
90-
isa<PackArchetypeType>(origType))
91-
origType = origType->mapTypeOutOfContext()->getCanonicalType();
89+
if (origType->is<PrimaryArchetypeType>() ||
90+
origType->is<PackArchetypeType>())
91+
origType = origType->mapTypeOutOfContext();
9292

93-
if (SubsMap)
94-
return SubsMap->lookupConformance(origType, proto);
93+
if (SubsMap) {
94+
return SubsMap->lookupConformance(
95+
origType->getCanonicalType(), proto);
96+
}
9597

96-
return ProtocolConformanceRef::forAbstract(substType, proto);
98+
return ProtocolConformanceRef::forAbstract(
99+
origType.subst(IFS), proto);
97100
}
98101

99102
void dump(llvm::raw_ostream &out) const {

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,15 +1914,9 @@ void PrintAST::printSingleDepthOfGenericSignature(
19141914
if (subMap.empty())
19151915
return param;
19161916

1917-
return param.subst(
1918-
[&](SubstitutableType *type) -> Type {
1919-
if (cast<GenericTypeParamType>(type)->getDepth() < typeContextDepth)
1920-
return Type(type).subst(subMap);
1921-
return type;
1922-
},
1923-
[&](CanType depType, Type substType, ProtocolDecl *proto) {
1924-
return lookupConformance(substType, proto);
1925-
});
1917+
OuterSubstitutions replacer{subMap, typeContextDepth};
1918+
return param.subst(replacer, replacer,
1919+
SubstFlags::PreservePackExpansionLevel);
19261920
};
19271921

19281922
/// Separate the explicit generic parameters from the implicit, opaque

lib/AST/ExistentialGeneralization.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ class Generalizer : public CanTypeVisitor<Generalizer, Type> {
6868
assert(it != substTypes.end());
6969
return it->second;
7070
};
71-
auto lookupConformance = [&](CanType dependentType,
72-
Type conformingReplacementType,
71+
auto lookupConformance = [&](InFlightSubstitution &IFS,
72+
Type dependentType,
7373
ProtocolDecl *conformedProtocol) {
74-
auto it = substConformances.find({dependentType, conformedProtocol});
74+
auto it = substConformances.find(
75+
{dependentType->getCanonicalType(), conformedProtocol});
7576
assert(it != substConformances.end());
7677
return it->second;
7778
};

lib/AST/LocalArchetypeRequirementCollector.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ swift::buildSubstitutionMapWithCapturedEnvironments(
266266
return mapIntoLocalContext(param, baseDepth, capturedEnvs);
267267
return Type(type).subst(baseSubMap);
268268
},
269-
[&](CanType origType, Type substType,
270-
ProtocolDecl *proto) -> ProtocolConformanceRef {
269+
[&](InFlightSubstitution &IFS, Type origType, ProtocolDecl *proto)
270+
-> ProtocolConformanceRef {
271271
if (origType->getRootGenericParam()->getDepth() >= baseDepth)
272-
return ProtocolConformanceRef::forAbstract(substType, proto);
273-
return baseSubMap.lookupConformance(origType, proto);
272+
return ProtocolConformanceRef::forAbstract(origType.subst(IFS), proto);
273+
return baseSubMap.lookupConformance(origType->getCanonicalType(), proto);
274274
});
275275
}

lib/AST/RequirementEnvironment.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ RequirementEnvironment::RequirementEnvironment(
112112
return substGenericParam;
113113
},
114114
[substConcreteType, conformance, conformanceDC, covariantSelf, &ctx](
115-
CanType type, Type replacement, ProtocolDecl *proto)
115+
InFlightSubstitution &IFS, Type type, ProtocolDecl *proto)
116116
-> ProtocolConformanceRef {
117117
// The protocol 'Self' conforms concretely to the conforming type.
118118
if (type->isEqual(ctx.TheSelfType)) {
119+
auto replacement = type.subst(IFS);
119120
ASSERT(covariantSelf || replacement->isEqual(substConcreteType));
120121

121122
if (conformance) {
@@ -145,7 +146,7 @@ RequirementEnvironment::RequirementEnvironment(
145146

146147
// All other generic parameters come from the requirement itself
147148
// and conform abstractly.
148-
return MakeAbstractConformanceForGenericType()(type, replacement, proto);
149+
return MakeAbstractConformanceForGenericType()(IFS, type, proto);
149150
});
150151

151152
// If the requirement itself is non-generic, the witness thunk signature

lib/AST/SubstitutionMap.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,18 @@ Type QueryOverrideSubs::operator()(SubstitutableType *type) const {
473473
}
474474

475475
ProtocolConformanceRef
476-
LookUpConformanceInOverrideSubs::operator()(CanType type,
477-
Type substType,
476+
LookUpConformanceInOverrideSubs::operator()(InFlightSubstitution &IFS,
477+
Type type,
478478
ProtocolDecl *proto) const {
479479
if (type->getRootGenericParam()->getDepth() >= info.BaseDepth)
480-
return ProtocolConformanceRef::forAbstract(substType, proto);
480+
return ProtocolConformanceRef::forAbstract(type.subst(IFS), proto);
481481

482-
if (auto conformance = info.BaseSubMap.lookupConformance(type, proto))
482+
if (auto conformance = info.BaseSubMap.lookupConformance(
483+
type->getCanonicalType(), proto)) {
483484
return conformance;
485+
}
484486

485-
return lookupConformance(substType, proto);
487+
return lookupConformance(type.subst(IFS), proto);
486488
}
487489

488490
SubstitutionMap
@@ -665,8 +667,8 @@ Type OuterSubstitutions::operator()(SubstitutableType *type) const {
665667
}
666668

667669
ProtocolConformanceRef OuterSubstitutions::operator()(
668-
CanType dependentType,
669-
Type conformingReplacementType,
670+
InFlightSubstitution &IFS,
671+
Type dependentType,
670672
ProtocolDecl *conformedProtocol) const {
671673
auto sig = subs.getGenericSignature();
672674
if (!sig->isValidTypeParameter(dependentType) ||
@@ -683,10 +685,10 @@ ProtocolConformanceRef OuterSubstitutions::operator()(
683685
// Once we check for that and handle it properly, the lookupConformance()
684686
// can become a forAbstract().
685687
return swift::lookupConformance(
686-
conformingReplacementType, conformedProtocol);
688+
dependentType.subst(IFS), conformedProtocol);
687689
}
688690

689691
return LookUpConformanceInSubstitutionMap(subs)(
690-
dependentType, conformingReplacementType, conformedProtocol);
692+
IFS, dependentType, conformedProtocol);
691693
}
692694

0 commit comments

Comments
 (0)