Skip to content

Commit fde7eb5

Browse files
committed
[AST] Record default associated conformance witnesses directly.
Referencing associated conformance witnesses via the index of the requirement in the requirement signature is inconvenient. Reference them by CanType/ProtocolDecl* instead.
1 parent 89f3da6 commit fde7eb5

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,19 +3981,15 @@ class ProtocolDecl final : public NominalTypeDecl {
39813981

39823982
/// Returns the default associated conformance witness for an associated
39833983
/// type, or \c None if there is no default.
3984-
///
3985-
/// \param reqIndex The index of the associated conformance within the
3986-
/// requirement signature.
39873984
Optional<ProtocolConformanceRef> getDefaultAssociatedConformanceWitness(
3988-
unsigned reqIndex) const;
3985+
CanType association,
3986+
ProtocolDecl *requirement) const;
39893987

39903988
/// Set the default associated conformance witness for the given
39913989
/// associated conformance.
3992-
///
3993-
/// \param reqIndex The index of the associated conformance within the
3994-
/// requirement signature.
39953990
void setDefaultAssociatedConformanceWitness(
3996-
unsigned reqIndex,
3991+
CanType association,
3992+
ProtocolDecl *requirement,
39973993
ProtocolConformanceRef conformance);
39983994

39993995
/// Retrieve the name to use for this protocol when interoperating

lib/AST/ASTContext.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
286286
DefaultTypeWitnesses;
287287

288288
/// Default associated conformance witnesses for protocols.
289-
llvm::DenseMap<std::pair<const ProtocolDecl *, unsigned>,
289+
llvm::DenseMap<std::tuple<const ProtocolDecl *, CanType, ProtocolDecl *>,
290290
ProtocolConformanceRef>
291291
DefaultAssociatedConformanceWitnesses;
292292

@@ -1715,22 +1715,27 @@ void ProtocolDecl::setDefaultTypeWitness(AssociatedTypeDecl *assocType,
17151715
}
17161716

17171717
Optional<ProtocolConformanceRef>
1718-
ProtocolDecl::getDefaultAssociatedConformanceWitness(unsigned reqIndex) const {
1718+
ProtocolDecl::getDefaultAssociatedConformanceWitness(
1719+
CanType association,
1720+
ProtocolDecl *requirement) const {
17191721
auto &ctx = getASTContext();
17201722
auto found =
1721-
ctx.getImpl().DefaultAssociatedConformanceWitnesses.find({this, reqIndex});
1723+
ctx.getImpl().DefaultAssociatedConformanceWitnesses.find(
1724+
std::make_tuple(this, association, requirement));
17221725
if (found == ctx.getImpl().DefaultAssociatedConformanceWitnesses.end())
17231726
return None;
17241727

17251728
return found->second;
17261729
}
17271730

17281731
void ProtocolDecl::setDefaultAssociatedConformanceWitness(
1729-
unsigned reqIndex,
1732+
CanType association,
1733+
ProtocolDecl *requirement,
17301734
ProtocolConformanceRef conformance) {
17311735
auto &ctx = getASTContext();
17321736
auto pair = ctx.getImpl().DefaultAssociatedConformanceWitnesses.insert(
1733-
std::make_pair(std::make_pair(this, reqIndex), conformance));
1737+
std::make_pair(std::make_tuple(this, association, requirement),
1738+
conformance));
17341739
assert(pair.second && "Already have a default associated conformance");
17351740
(void)pair;
17361741
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5304,9 +5304,7 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
53045304

53055305
// Find defaults for any associated conformances rooted on defaulted
53065306
// associated types.
5307-
auto requirementSignature = proto->getRequirementSignature();
5308-
for (unsigned reqIndex : indices(requirementSignature)) {
5309-
const auto &req = requirementSignature[reqIndex];
5307+
for (const auto &req : proto->getRequirementSignature()) {
53105308
if (req.getKind() != RequirementKind::Conformance)
53115309
continue;
53125310
if (req.getFirstType()->isEqual(proto->getProtocolSelfType()))
@@ -5371,7 +5369,8 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
53715369
}
53725370

53735371
// Record the default associated conformance.
5374-
proto->setDefaultAssociatedConformanceWitness(reqIndex, *conformance);
5372+
proto->setDefaultAssociatedConformanceWitness(
5373+
req.getFirstType()->getCanonicalType(), requirementProto, *conformance);
53755374
}
53765375
}
53775376

0 commit comments

Comments
 (0)