Skip to content

Commit 4da11aa

Browse files
committed
[AST] Teach SpecializedProtocolConformance to profile its substitutions.
Otherwise, we could theoretically get collisions, although they are quite unlikely to matter in practice.
1 parent ebdd56a commit 4da11aa

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,16 @@ class SpecializedProtocolConformance : public ProtocolConformance,
589589
}
590590

591591
void Profile(llvm::FoldingSetNodeID &ID) {
592-
Profile(ID, getType(), getGenericConformance());
592+
Profile(ID, getType(), getGenericConformance(),
593+
getGenericSubstitutions());
593594
}
594595

595596
static void Profile(llvm::FoldingSetNodeID &ID, Type type,
596-
ProtocolConformance *genericConformance) {
597-
// FIXME: Consider profiling substitutions here. They could differ in
598-
// some crazy cases that also require major diagnostic work, where the
599-
// substitutions involve conformances of the same type to the same
600-
// protocol drawn from different imported modules.
597+
ProtocolConformance *genericConformance,
598+
SubstitutionList subs) {
601599
ID.AddPointer(type.getPointer());
602600
ID.AddPointer(genericConformance);
601+
profileSubstitutionList(ID, subs);
603602
}
604603

605604
static bool classof(const ProtocolConformance *conformance) {

include/swift/AST/SubstitutionList.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
#include "swift/AST/Substitution.h"
2323
#include "llvm/ADT/ArrayRef.h"
24+
#include "llvm/ADT/FoldingSet.h"
25+
26+
namespace llvm {
27+
class FoldingSetNodeID;
28+
} // end namespace llvm
2429

2530
namespace swift {
2631

@@ -41,6 +46,10 @@ void dump(SubstitutionList subs);
4146
SubstitutionList
4247
getCanonicalSubstitutionList(SubstitutionList subs,
4348
SmallVectorImpl<Substitution> &canSubs);
49+
50+
/// Profile the substitution list for use in a folding set.
51+
void profileSubstitutionList(llvm::FoldingSetNodeID &id, SubstitutionList subs);
52+
4453
} // end namespace swift
4554

4655
#endif

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ ASTContext::getSpecializedConformance(Type type,
15281528
ProtocolConformance *generic,
15291529
SubstitutionList substitutions) {
15301530
llvm::FoldingSetNodeID id;
1531-
SpecializedProtocolConformance::Profile(id, type, generic);
1531+
SpecializedProtocolConformance::Profile(id, type, generic, substitutions);
15321532

15331533
// Figure out which arena this conformance should go into.
15341534
AllocationArena arena = getArena(type->getRecursiveProperties());

lib/AST/SubstitutionList.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,17 @@
1717
//===----------------------------------------------------------------------===//
1818

1919
#include "swift/AST/SubstitutionList.h"
20+
#include "swift/AST/ProtocolConformanceRef.h"
21+
#include "llvm/ADT/FoldingSet.h"
2022

2123
using namespace swift;
24+
25+
void swift::profileSubstitutionList(llvm::FoldingSetNodeID &id,
26+
SubstitutionList subs) {
27+
id.AddInteger(subs.size());
28+
for (auto &sub : subs) {
29+
id.AddPointer(sub.getReplacement().getPointer());
30+
for (auto conformance : sub.getConformances())
31+
id.AddPointer(conformance.getOpaqueValue());
32+
}
33+
}

lib/AST/Type.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,11 +4060,7 @@ bool TypeBase::usesNativeReferenceCounting(ResilienceExpansion resilience) {
40604060
void SILBoxType::Profile(llvm::FoldingSetNodeID &id, SILLayout *Layout,
40614061
SubstitutionList Args) {
40624062
id.AddPointer(Layout);
4063-
for (auto &arg : Args) {
4064-
id.AddPointer(arg.getReplacement().getPointer());
4065-
for (auto conformance : arg.getConformances())
4066-
id.AddPointer(conformance.getOpaqueValue());
4067-
}
4063+
profileSubstitutionList(id, Args);
40684064
}
40694065

40704066
SILBoxType::SILBoxType(ASTContext &C,

0 commit comments

Comments
 (0)