Skip to content

Commit 4e658b8

Browse files
committed
AST: Refactor getOverrideGenericSignature() to use a more correct uniquing key
1 parent f8fa2c0 commit 4e658b8

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

include/swift/AST/ASTContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,12 @@ class ASTContext final {
13441344
GenericSignature getOverrideGenericSignature(const ValueDecl *base,
13451345
const ValueDecl *derived);
13461346

1347+
GenericSignature
1348+
getOverrideGenericSignature(const NominalTypeDecl *baseNominal,
1349+
const NominalTypeDecl *derivedNominal,
1350+
GenericSignature baseGenericSig,
1351+
const GenericParamList *derivedParams);
1352+
13471353
enum class OverrideGenericSignatureReqCheck {
13481354
/// Base method's generic requirements are satisfied by derived method
13491355
BaseReqSatisfiedByDerived,

lib/AST/ASTContext.cpp

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ using AssociativityCacheType =
103103

104104
struct OverrideSignatureKey {
105105
GenericSignature baseMethodSig;
106-
GenericSignature derivedMethodSig;
107-
NominalTypeDecl *derivedNominal;
108-
109-
OverrideSignatureKey(GenericSignature baseMethodSignature,
110-
GenericSignature derivedMethodSignature,
111-
NominalTypeDecl *derivedNominal)
112-
: baseMethodSig(baseMethodSignature),
113-
derivedMethodSig(derivedMethodSignature),
114-
derivedNominal(derivedNominal) {}
106+
const NominalTypeDecl *baseNominal;
107+
const NominalTypeDecl *derivedNominal;
108+
const GenericParamList *derivedParams;
109+
110+
OverrideSignatureKey(GenericSignature baseMethodSig,
111+
const NominalTypeDecl *baseNominal,
112+
const NominalTypeDecl *derivedNominal,
113+
const GenericParamList *derivedParams)
114+
: baseMethodSig(baseMethodSig),
115+
baseNominal(baseNominal),
116+
derivedNominal(derivedNominal),
117+
derivedParams(derivedParams) {}
115118
};
116119

117120
namespace llvm {
@@ -122,28 +125,32 @@ template <> struct DenseMapInfo<OverrideSignatureKey> {
122125
static bool isEqual(const OverrideSignatureKey lhs,
123126
const OverrideSignatureKey rhs) {
124127
return lhs.baseMethodSig.getPointer() == rhs.baseMethodSig.getPointer() &&
125-
lhs.derivedMethodSig.getPointer() == rhs.derivedMethodSig.getPointer() &&
126-
lhs.derivedNominal == rhs.derivedNominal;
128+
lhs.baseNominal == rhs.baseNominal &&
129+
lhs.derivedNominal == rhs.derivedNominal &&
130+
lhs.derivedParams == rhs.derivedParams;
127131
}
128132

129133
static inline OverrideSignatureKey getEmptyKey() {
130134
return OverrideSignatureKey(DenseMapInfo<GenericSignature>::getEmptyKey(),
131-
DenseMapInfo<GenericSignature>::getEmptyKey(),
132-
DenseMapInfo<NominalTypeDecl *>::getEmptyKey());
135+
DenseMapInfo<NominalTypeDecl *>::getEmptyKey(),
136+
DenseMapInfo<NominalTypeDecl *>::getEmptyKey(),
137+
DenseMapInfo<GenericParamList *>::getEmptyKey());
133138
}
134139

135140
static inline OverrideSignatureKey getTombstoneKey() {
136141
return OverrideSignatureKey(
137142
DenseMapInfo<GenericSignature>::getTombstoneKey(),
138-
DenseMapInfo<GenericSignature>::getTombstoneKey(),
139-
DenseMapInfo<NominalTypeDecl *>::getTombstoneKey());
143+
DenseMapInfo<NominalTypeDecl *>::getTombstoneKey(),
144+
DenseMapInfo<NominalTypeDecl *>::getTombstoneKey(),
145+
DenseMapInfo<GenericParamList *>::getTombstoneKey());
140146
}
141147

142148
static unsigned getHashValue(const OverrideSignatureKey &Val) {
143149
return hash_combine(
144150
DenseMapInfo<GenericSignature>::getHashValue(Val.baseMethodSig),
145-
DenseMapInfo<GenericSignature>::getHashValue(Val.derivedMethodSig),
146-
DenseMapInfo<NominalTypeDecl *>::getHashValue(Val.derivedNominal));
151+
DenseMapInfo<NominalTypeDecl *>::getHashValue(Val.baseNominal),
152+
DenseMapInfo<NominalTypeDecl *>::getHashValue(Val.derivedNominal),
153+
DenseMapInfo<GenericParamList *>::getHashValue(Val.derivedParams));
147154
}
148155
};
149156
} // namespace llvm
@@ -5271,32 +5278,42 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
52715278

52725279
const auto baseGenericSig =
52735280
base->getAsGenericContext()->getGenericSignature();
5274-
const auto derivedGenericSig =
5275-
derived->getAsGenericContext()->getGenericSignature();
5281+
const auto *derivedParams =
5282+
derived->getAsGenericContext()->getGenericParams();
5283+
5284+
return getOverrideGenericSignature(baseNominal, derivedNominal,
5285+
baseGenericSig, derivedParams);
5286+
}
5287+
5288+
GenericSignature
5289+
ASTContext::getOverrideGenericSignature(const NominalTypeDecl *baseNominal,
5290+
const NominalTypeDecl *derivedNominal,
5291+
GenericSignature baseGenericSig,
5292+
const GenericParamList *derivedParams) {
5293+
if (baseNominal == derivedNominal)
5294+
return baseGenericSig;
52765295

5277-
if (base == derived)
5278-
return derivedGenericSig;
5296+
const auto derivedNominalSig = derivedNominal->getGenericSignature();
52795297

5280-
if (derivedGenericSig.isNull())
5298+
if (derivedNominalSig.isNull() && derivedParams == nullptr)
52815299
return nullptr;
52825300

52835301
if (baseGenericSig.isNull())
5284-
return derivedGenericSig;
5302+
return derivedNominalSig;
52855303

52865304
auto key = OverrideSignatureKey(baseGenericSig,
5287-
derivedGenericSig,
5288-
derivedNominal);
5305+
baseNominal,
5306+
derivedNominal,
5307+
derivedParams);
52895308

52905309
if (getImpl().overrideSigCache.find(key) !=
52915310
getImpl().overrideSigCache.end()) {
52925311
return getImpl().overrideSigCache.lookup(key);
52935312
}
52945313

5295-
const auto derivedNominalSig = derivedNominal->getGenericSignature();
5296-
52975314
SmallVector<GenericTypeParamType *, 2> addedGenericParams;
5298-
if (const auto *gpList = derived->getAsGenericContext()->getGenericParams()) {
5299-
for (auto gp : *gpList) {
5315+
if (derivedParams) {
5316+
for (auto gp : *derivedParams) {
53005317
addedGenericParams.push_back(
53015318
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
53025319
}

0 commit comments

Comments
 (0)