Skip to content

Commit 9148ae3

Browse files
authored
Merge pull request #80482 from slavapestov/abstract-conformance-cleanup
AST: Simplify ProtocolConformanceRef operations a little bit
2 parents 7836aff + e475b08 commit 9148ae3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+228
-437
lines changed

include/swift/AST/GenericEnvironment.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,6 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
325325
/// abstraction level of their associated type requirements.
326326
SILType mapTypeIntoContext(SILModule &M, SILType type) const;
327327

328-
/// Map an interface type's protocol conformance into the corresponding
329-
/// conformance for the contextual type.
330-
static std::pair<Type, ProtocolConformanceRef>
331-
mapConformanceRefIntoContext(GenericEnvironment *genericEnv,
332-
Type conformingType,
333-
ProtocolConformanceRef conformance);
334-
335-
/// Map an interface type's protocol conformance into the corresponding
336-
/// conformance for the contextual type.
337-
std::pair<Type, ProtocolConformanceRef>
338-
mapConformanceRefIntoContext(Type conformingType,
339-
ProtocolConformanceRef conformance) const;
340-
341328
/// Returns a substitution map that sends every generic parameter to its
342329
/// corresponding archetype in this generic environment.
343330
SubstitutionMap getForwardingSubstitutionMap() const;

include/swift/AST/ProtocolAssociations.h

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,6 @@
2323

2424
namespace swift {
2525

26-
/// A type associated with a protocol.
27-
///
28-
/// This struct exists largely so that we can maybe eventually
29-
/// generalize it to an arbitrary path.
30-
class AssociatedType {
31-
AssociatedTypeDecl *Association;
32-
using AssociationInfo = llvm::DenseMapInfo<AssociatedTypeDecl*>;
33-
34-
struct SpecialValue {};
35-
explicit AssociatedType(SpecialValue _, AssociatedTypeDecl *specialValue)
36-
: Association(specialValue) {}
37-
38-
public:
39-
explicit AssociatedType(AssociatedTypeDecl *association)
40-
: Association(association) {
41-
assert(association);
42-
}
43-
44-
ProtocolDecl *getSourceProtocol() const {
45-
return Association->getProtocol();
46-
}
47-
48-
AssociatedTypeDecl *getAssociation() const {
49-
return Association;
50-
}
51-
52-
friend bool operator==(AssociatedType lhs, AssociatedType rhs) {
53-
return lhs.Association == rhs.Association;
54-
}
55-
friend bool operator!=(AssociatedType lhs, AssociatedType rhs) {
56-
return !(lhs == rhs);
57-
}
58-
59-
unsigned getHashValue() const {
60-
return llvm::hash_value(Association);
61-
}
62-
63-
static AssociatedType getEmptyKey() {
64-
return AssociatedType(SpecialValue(), AssociationInfo::getEmptyKey());
65-
}
66-
static AssociatedType getTombstoneKey() {
67-
return AssociatedType(SpecialValue(), AssociationInfo::getTombstoneKey());
68-
}
69-
};
70-
7126
/// A base conformance of a protocol.
7227
class BaseConformance {
7328
ProtocolDecl *Source;
@@ -147,24 +102,6 @@ class AssociatedConformance {
147102
} // end namespace swift
148103

149104
namespace llvm {
150-
template <> struct DenseMapInfo<swift::AssociatedType> {
151-
static inline swift::AssociatedType getEmptyKey() {
152-
return swift::AssociatedType::getEmptyKey();
153-
}
154-
155-
static inline swift::AssociatedType getTombstoneKey() {
156-
return swift::AssociatedType::getTombstoneKey();
157-
}
158-
159-
static unsigned getHashValue(swift::AssociatedType val) {
160-
return val.getHashValue();
161-
}
162-
163-
static bool isEqual(swift::AssociatedType lhs, swift::AssociatedType rhs) {
164-
return lhs == rhs;
165-
}
166-
};
167-
168105
template <> struct DenseMapInfo<swift::AssociatedConformance> {
169106
static inline swift::AssociatedConformance getEmptyKey() {
170107
return swift::AssociatedConformance::getEmptyKey();

include/swift/AST/ProtocolConformance.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,6 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance
363363
/// Retrieve the protocol conformance for the inherited protocol.
364364
ProtocolConformance *getInheritedConformance(ProtocolDecl *protocol) const;
365365

366-
/// Given a dependent type expressed in terms of the self parameter,
367-
/// map it into the context of this conformance.
368-
Type getAssociatedType(Type assocType) const;
369-
370366
/// Given that the requirement signature of the protocol directly states
371367
/// that the given dependent type must conform to the given protocol,
372368
/// return its associated conformance.

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,38 +185,36 @@ 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;
205204

206205
/// Look up the type witness for an associated type declaration in this
207206
/// conformance.
208-
Type getTypeWitness(Type origType, AssociatedTypeDecl *assocType,
207+
Type getTypeWitness(AssociatedTypeDecl *assocType,
209208
SubstOptions options = std::nullopt) const;
210209

211210
/// Given a dependent type (expressed in terms of this conformance's
212211
/// protocol), follow it from the conforming type.
213-
Type getAssociatedType(Type origType, Type dependentType) const;
212+
Type getAssociatedType(Type dependentType) const;
214213

215214
/// Given a dependent type (expressed in terms of this conformance's
216215
/// protocol) and conformance, follow it from the conforming type.
217216
ProtocolConformanceRef
218-
getAssociatedConformance(Type origType, Type dependentType,
219-
ProtocolDecl *requirement) const;
217+
getAssociatedConformance(Type dependentType, ProtocolDecl *requirement) const;
220218

221219
SWIFT_DEBUG_DUMP;
222220
void dump(llvm::raw_ostream &out, unsigned indent = 0,
@@ -235,15 +233,15 @@ class ProtocolConformanceRef {
235233
return llvm::hash_value(conformance.Union.getOpaqueValue());
236234
}
237235

238-
Type getTypeWitnessByName(Type type, Identifier name) const;
236+
Type getTypeWitnessByName(Identifier name) const;
239237

240238
/// Find a particular named function witness for a type that conforms to
241239
/// the given protocol.
242240
///
243241
/// \param type The conforming type.
244242
///
245243
/// \param name The name of the requirement.
246-
ConcreteDeclRef getWitnessByName(Type type, DeclName name) const;
244+
ConcreteDeclRef getWitnessByName(DeclName name) const;
247245

248246
/// Determine whether this conformance is canonical.
249247
bool isCanonical() const;

include/swift/AST/SubstitutionMap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class SubstitutionMap {
187187
SubstitutionMap mapIntoTypeExpansionContext(
188188
TypeExpansionContext context) const;
189189

190+
/// Create a substitution map for a protocol conformance.
191+
static SubstitutionMap
192+
getProtocolSubstitutions(ProtocolConformanceRef conformance);
193+
190194
/// Create a substitution map for a protocol conformance.
191195
static SubstitutionMap
192196
getProtocolSubstitutions(ProtocolDecl *protocol,

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5028,7 +5028,7 @@ Type substOpaqueTypesWithUnderlyingTypes(Type type,
50285028
CanType substOpaqueTypesWithUnderlyingTypes(CanType type,
50295029
TypeExpansionContext context);
50305030
ProtocolConformanceRef
5031-
substOpaqueTypesWithUnderlyingTypes(ProtocolConformanceRef ref, Type origType,
5031+
substOpaqueTypesWithUnderlyingTypes(ProtocolConformanceRef ref,
50325032
TypeExpansionContext context);
50335033
namespace Lowering {
50345034
class TypeConverter;

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 2 deletions
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
}
@@ -576,7 +576,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
576576
!context.shouldLookThroughOpaqueTypeArchetypes())
577577
return C;
578578

579-
return substOpaqueTypesWithUnderlyingTypes(C, Ty, context);
579+
return substOpaqueTypesWithUnderlyingTypes(C, context);
580580
}
581581

582582
return C;

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
100100
// If this is a new associated type (which does not override an
101101
// existing associated type), add it.
102102
if (associatedType->getOverriddenDecls().empty())
103-
asDerived().addAssociatedType(AssociatedType(associatedType));
103+
asDerived().addAssociatedType(associatedType);
104104
}
105105

106106
if (asDerived().shouldVisitRequirementSignatureOnly())

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
16781678
}
16791679

16801680
auto *ctx = const_cast<ASTContext *>(this);
1681-
witness = builtinConformance.getWitnessByName(type, initName(*ctx));
1681+
witness = builtinConformance.getWitnessByName(initName(*ctx));
16821682
if (!witness) {
16831683
assert(false && "Missing required witness");
16841684
witness = ConcreteDeclRef();
@@ -6486,7 +6486,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
64866486
*bridgedValueType = type;
64876487

64886488
// Find the Objective-C class type we bridge to.
6489-
Type witnessTy = conformance.getTypeWitnessByName(type, Id_ObjectiveCType);
6489+
Type witnessTy = conformance.getTypeWitnessByName(Id_ObjectiveCType);
64906490
// If Objective-C import is broken, witness type would be a dependent member
64916491
// with `<<error type>>` base.
64926492
return (witnessTy && !witnessTy->hasError()) ? witnessTy : Type();

lib/AST/ActorIsolation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ActorIsolation::forActorInstanceParameter(Expr *actor,
6161
if (auto globalActor = ctx.getProtocol(KnownProtocolKind::GlobalActor)) {
6262
auto conformance = checkConformance(baseType, globalActor);
6363
if (conformance &&
64-
conformance.getWitnessByName(baseType, ctx.Id_shared) == declRef) {
64+
conformance.getWitnessByName(ctx.Id_shared) == declRef) {
6565
return ActorIsolation::forGlobalActor(baseType);
6666
}
6767
}

lib/AST/DistributedDecl.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Type swift::getDistributedActorSystemType(NominalTypeDecl *actor) {
203203
// Dig out the actor system type.
204204
Type selfType = actor->getSelfInterfaceType();
205205
auto conformance = lookupConformance(selfType, DA);
206-
return conformance.getTypeWitnessByName(selfType, C.Id_ActorSystem);
206+
return conformance.getTypeWitnessByName(C.Id_ActorSystem);
207207
}
208208

209209
Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {
@@ -220,7 +220,7 @@ static Type getTypeWitnessByName(NominalTypeDecl *type, ProtocolDecl *protocol,
220220
auto conformance = lookupConformance(selfType, protocol);
221221
if (!conformance || conformance.isInvalid())
222222
return Type();
223-
return conformance.getTypeWitnessByName(selfType, member);
223+
return conformance.getTypeWitnessByName(member);
224224
}
225225

226226
Type swift::getDistributedActorSerializationType(
@@ -301,8 +301,7 @@ Type swift::getDistributedSerializationRequirementType(
301301
if (conformance.isInvalid())
302302
return Type();
303303

304-
return conformance.getTypeWitnessByName(selfType,
305-
ctx.Id_SerializationRequirement);
304+
return conformance.getTypeWitnessByName(ctx.Id_SerializationRequirement);
306305
}
307306

308307
AbstractFunctionDecl *
@@ -365,11 +364,10 @@ Type swift::getAssociatedTypeOfDistributedSystemOfActor(
365364
auto actorConformance =
366365
lookupConformance(
367366
actorType->getDeclaredInterfaceType(), actorProtocol);
368-
if (!actorConformance || actorConformance.isInvalid())
367+
if (actorConformance.isInvalid())
369368
return Type();
370369

371-
auto subs = SubstitutionMap::getProtocolSubstitutions(
372-
actorProtocol, actorType->getDeclaredInterfaceType(), actorConformance);
370+
auto subs = SubstitutionMap::getProtocolSubstitutions(actorConformance);
373371

374372
memberTy = memberTy.subst(subs);
375373

lib/AST/GenericEnvironment.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -752,28 +752,6 @@ SubstitutionMap GenericEnvironment::getForwardingSubstitutionMap() const {
752752
MakeAbstractConformanceForGenericType());
753753
}
754754

755-
std::pair<Type, ProtocolConformanceRef>
756-
GenericEnvironment::mapConformanceRefIntoContext(GenericEnvironment *genericEnv,
757-
Type conformingType,
758-
ProtocolConformanceRef conformance) {
759-
if (!genericEnv)
760-
return {conformingType, conformance};
761-
762-
return genericEnv->mapConformanceRefIntoContext(conformingType, conformance);
763-
}
764-
765-
std::pair<Type, ProtocolConformanceRef>
766-
GenericEnvironment::mapConformanceRefIntoContext(
767-
Type conformingInterfaceType,
768-
ProtocolConformanceRef conformance) const {
769-
auto contextConformance = conformance.subst(conformingInterfaceType,
770-
QueryInterfaceTypeSubstitutions(this),
771-
LookUpConformanceInModule());
772-
773-
auto contextType = mapTypeIntoContext(conformingInterfaceType);
774-
return {contextType, contextConformance};
775-
}
776-
777755
OpenedElementContext
778756
OpenedElementContext::createForContextualExpansion(ASTContext &ctx,
779757
CanPackExpansionType expansionType) {

lib/AST/PackConformance.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,15 @@ PackType *PackConformance::getTypeWitness(
112112
// conformance.
113113
if (auto *packExpansion = packElement->getAs<PackExpansionType>()) {
114114
auto assocTypePattern =
115-
conformances[i].getTypeWitness(packExpansion->getPatternType(),
116-
assocType, options);
115+
conformances[i].getTypeWitness(assocType, options);
117116

118117
packElements.push_back(PackExpansionType::get(
119118
assocTypePattern, packExpansion->getCountType()));
120119

121120
// If the pack element is a scalar type, replace the scalar type with
122121
// the associated type witness from the pattern conformance.
123122
} else {
124-
auto assocTypeScalar =
125-
conformances[i].getTypeWitness(packElement, assocType, options);
123+
auto assocTypeScalar = conformances[i].getTypeWitness(assocType, options);
126124
packElements.push_back(assocTypeScalar);
127125
}
128126
}
@@ -144,24 +142,20 @@ PackConformance *PackConformance::getAssociatedConformance(
144142
auto packElement = ConformingType->getElementType(i);
145143

146144
if (auto *packExpansion = packElement->getAs<PackExpansionType>()) {
147-
auto assocTypePattern =
148-
conformances[i].getAssociatedType(packExpansion->getPatternType(),
149-
assocType);
150-
packElements.push_back(PackExpansionType::get(
151-
assocTypePattern, packExpansion->getCountType()));
152-
153145
auto assocConformancePattern =
154-
conformances[i].getAssociatedConformance(packExpansion->getPatternType(),
155-
assocType, protocol);
146+
conformances[i].getAssociatedConformance(assocType, protocol);
156147
packConformances.push_back(assocConformancePattern);
157-
} else {
158-
auto assocTypeScalar =
159-
conformances[i].getAssociatedType(packElement, assocType);
160-
packElements.push_back(assocTypeScalar);
161148

149+
auto assocTypePattern = assocConformancePattern.getType();
150+
packElements.push_back(PackExpansionType::get(
151+
assocTypePattern, packExpansion->getCountType()));
152+
} else {
162153
auto assocConformanceScalar =
163-
conformances[i].getAssociatedConformance(packElement, assocType, protocol);
154+
conformances[i].getAssociatedConformance(assocType, protocol);
164155
packConformances.push_back(assocConformanceScalar);
156+
157+
auto assocTypeScalar = assocConformanceScalar.getType();
158+
packElements.push_back(assocTypeScalar);
165159
}
166160
}
167161

@@ -202,15 +196,12 @@ PackConformance::subst(InFlightSubstitution &IFS) const {
202196
// Just substitute the conformance. We don't directly represent
203197
// pack expansion conformances here; it's sort of implicit in the
204198
// corresponding pack element type.
205-
substConformances.push_back(
206-
origConformances[i].subst(origExpansion->getPatternType(), IFS));
199+
substConformances.push_back(origConformances[i].subst(IFS));
207200
});
208201
} else {
209202
// Substitute a scalar element of the original pack.
210203
substElementTypes.push_back(origElementType.subst(IFS));
211-
212-
substConformances.push_back(
213-
origConformances[i].subst(origElementType, IFS));
204+
substConformances.push_back(origConformances[i].subst(IFS));
214205
}
215206
}
216207

0 commit comments

Comments
 (0)