Skip to content

Commit 37406e8

Browse files
authored
Merge pull request swiftlang#20360 from rjmccall/key-path-hash-read
[NFC] Don't hardcode how to read Hashable.hashValue in key paths
2 parents ddcd8d3 + 95fe154 commit 37406e8

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,83 +3166,60 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
31663166
auto entry = hash->begin();
31673167
auto indexPtr = entry->createFunctionArgument(params[0].getSILStorageType());
31683168

3169-
Scope scope(subSGF, loc);
3170-
3171-
auto hashMethod = cast<VarDecl>(
3172-
hashableProto->lookupDirect(C.Id_hashValue)[0])
3173-
->getGetter();
3174-
auto hashRef = SILDeclRef(hashMethod);
3175-
auto hashTy = subSGF.SGM.Types.getConstantType(hashRef);
3176-
31773169
SILValue hashCode;
31783170

3171+
// For now, just use the hash value of the first index.
31793172
// TODO: Combine hashes of the indexes using an inout Hasher
31803173
{
3174+
ArgumentScope scope(subSGF, loc);
3175+
31813176
auto &index = indexes[0];
31823177

3178+
// Extract the index value.
31833179
SILValue indexAddr = subSGF.B.createPointerToAddress(loc, indexPtr,
31843180
indexLoweredTy.getAddressType(),
31853181
/*isStrict*/ false);
31863182
if (indexes.size() > 1) {
31873183
indexAddr = subSGF.B.createTupleElementAddr(loc, indexAddr, 0);
31883184
}
3189-
3185+
3186+
VarDecl *hashValueVar =
3187+
cast<VarDecl>(hashableProto->lookupDirect(C.Id_hashValue)[0]);
3188+
31903189
auto formalTy = index.FormalType;
31913190
auto hashable = index.Hashable;
3192-
SubstitutionMap hashableSubsMap;
31933191
if (genericEnv) {
31943192
formalTy = genericEnv->mapTypeIntoContext(formalTy)->getCanonicalType();
31953193
hashable = hashable.subst(index.FormalType,
31963194
[&](Type t) -> Type { return genericEnv->mapTypeIntoContext(t); },
31973195
LookUpConformanceInSignature(*genericSig));
31983196
}
31993197

3200-
if (auto genericSig =
3201-
hashTy.castTo<SILFunctionType>()->getGenericSignature()) {
3202-
hashableSubsMap = SubstitutionMap::get(
3203-
genericSig,
3198+
// Set up a substitution of Self => IndexType.
3199+
auto hashGenericSig =
3200+
hashValueVar->getDeclContext()->getGenericSignatureOfContext();
3201+
assert(hashGenericSig);
3202+
SubstitutionMap hashableSubsMap = SubstitutionMap::get(
3203+
hashGenericSig,
32043204
[&](SubstitutableType *type) -> Type { return formalTy; },
32053205
[&](CanType dependentType, Type replacementType,
32063206
ProtocolDecl *proto)->Optional<ProtocolConformanceRef> {
32073207
return hashable;
32083208
});
3209-
}
3210-
3211-
auto hashWitness = subSGF.B.createWitnessMethod(loc,
3212-
formalTy, hashable,
3213-
hashRef, hashTy);
32143209

3215-
auto hashSubstTy = hashTy.castTo<SILFunctionType>()
3216-
->substGenericArgs(SGM.M, hashableSubsMap);
3217-
auto hashInfo = CalleeTypeInfo(hashSubstTy,
3218-
AbstractionPattern(intTy), intTy,
3219-
None,
3220-
ImportAsMemberStatus());
3221-
3222-
auto arg = subSGF.emitLoad(loc, indexAddr,
3223-
subSGF.getTypeLowering(AbstractionPattern::getOpaque(), formalTy),
3224-
SGFContext(), IsNotTake);
3225-
3226-
if (!arg.getType().isAddress()) {
3227-
auto buf = subSGF.emitTemporaryAllocation(loc, arg.getType());
3228-
arg.forwardInto(subSGF, loc, buf);
3229-
arg = subSGF.emitManagedBufferWithCleanup(buf);
3230-
}
3231-
3232-
{
3233-
auto hashResultPlan = ResultPlanBuilder::computeResultPlan(subSGF,
3234-
hashInfo, loc, SGFContext());
3235-
ArgumentScope argScope(subSGF, loc);
3236-
hashCode =
3237-
subSGF
3238-
.emitApply(std::move(hashResultPlan), std::move(argScope), loc,
3239-
ManagedValue::forUnmanaged(hashWitness),
3240-
hashableSubsMap,
3241-
{arg}, hashInfo, ApplyOptions::None, SGFContext())
3242-
.getUnmanagedSingleValue(subSGF, loc);
3243-
}
3210+
// Read the storage.
3211+
ManagedValue base = ManagedValue::forBorrowedAddressRValue(indexAddr);
3212+
hashCode =
3213+
subSGF.emitRValueForStorageLoad(loc, base, formalTy, /*super*/ false,
3214+
hashValueVar, PreparedArguments(),
3215+
hashableSubsMap,
3216+
AccessSemantics::Ordinary,
3217+
intTy, SGFContext())
3218+
.getUnmanagedSingleValue(subSGF, loc);
3219+
3220+
scope.pop();
32443221
}
3245-
scope.pop();
3222+
32463223
subSGF.B.createReturn(loc, hashCode);
32473224
}();
32483225

0 commit comments

Comments
 (0)