Skip to content

Commit 467456e

Browse files
committed
[SILGen] Get keypath emission logic off Substitution.
1 parent 911ed60 commit 467456e

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,16 +3651,19 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
36513651
hashable = hashable.subst(index.FormalType,
36523652
[&](Type t) -> Type { return genericEnv->mapTypeIntoContext(t); },
36533653
LookUpConformanceInSignature(*genericSig));
3654+
}
36543655

3656+
if (auto genericSig =
3657+
hashTy.castTo<SILFunctionType>()->getGenericSignature()) {
36553658
hashableSubsMap =
3656-
genericEnv->getGenericSignature()->getSubstitutionMap(
3659+
genericSig->getSubstitutionMap(
36573660
[&](SubstitutableType *type) -> Type { return formalTy; },
36583661
[&](CanType dependentType, Type replacementType,
36593662
ProtocolType *protoType)->Optional<ProtocolConformanceRef> {
36603663
return hashable;
36613664
});
36623665
}
3663-
3666+
36643667
auto hashWitness = subSGF.B.createWitnessMethod(loc,
36653668
formalTy, hashable,
36663669
hashRef, hashTy);
@@ -4117,25 +4120,25 @@ visitKeyPathApplicationExpr(KeyPathApplicationExpr *E, SGFContext C) {
41174120

41184121
auto keyPathDecl = E->getKeyPath()->getType()->getAnyNominal();
41194122
FuncDecl *projectFn;
4120-
SmallVector<Substitution, 4> subs;
4121-
4123+
4124+
SmallVector<Type, 2> replacementTypes;
41224125
if (keyPathDecl == SGF.getASTContext().getAnyKeyPathDecl()) {
41234126
// Invoke projectKeyPathAny with the type of the base value.
41244127
// The result is always `Any?`.
41254128
projectFn = SGF.getASTContext().getProjectKeyPathAny(nullptr);
4126-
subs.push_back(Substitution(E->getBase()->getType(), {}));
4129+
replacementTypes.push_back(E->getBase()->getType());
41274130
} else {
41284131
auto keyPathTy = E->getKeyPath()->getType()->castTo<BoundGenericType>();
41294132
if (keyPathDecl == SGF.getASTContext().getPartialKeyPathDecl()) {
41304133
// Invoke projectKeyPathPartial with the type of the base value.
41314134
// The result is always `Any`.
41324135
projectFn = SGF.getASTContext().getProjectKeyPathPartial(nullptr);
4133-
subs.push_back(Substitution(keyPathTy->getGenericArgs()[0], {}));
4136+
replacementTypes.push_back(keyPathTy->getGenericArgs()[0]);
41344137
} else {
41354138
projectFn = SGF.getASTContext().getProjectKeyPathReadOnly(nullptr);
41364139
// Get the root and leaf type from the key path type.
4137-
subs.push_back(Substitution(keyPathTy->getGenericArgs()[0], {}));
4138-
subs.push_back(Substitution(keyPathTy->getGenericArgs()[1], {}));
4140+
replacementTypes.push_back(keyPathTy->getGenericArgs()[0]);
4141+
replacementTypes.push_back(keyPathTy->getGenericArgs()[1]);
41394142

41404143
// Upcast the keypath to KeyPath<T, U> if it isn't already.
41414144
if (keyPathTy->getDecl() != SGF.getASTContext().getKeyPathDecl()) {
@@ -4150,8 +4153,15 @@ visitKeyPathApplicationExpr(KeyPathApplicationExpr *E, SGFContext C) {
41504153
}
41514154
}
41524155

4153-
auto genericArgsMap =
4154-
projectFn->getGenericSignature()->getSubstitutionMap(subs);
4156+
auto projectionGenericSig = projectFn->getGenericSignature();
4157+
auto genericArgsMap = projectionGenericSig->getSubstitutionMap(
4158+
[&](SubstitutableType *type) -> Type {
4159+
auto genericParam = cast<GenericTypeParamType>(type);
4160+
auto index =
4161+
projectionGenericSig->getGenericParamOrdinal(genericParam);
4162+
return replacementTypes[index];
4163+
},
4164+
LookUpConformanceInSignature(*projectionGenericSig));
41554165

41564166
return SGF.emitApplyOfLibraryIntrinsic(SILLocation(E),
41574167
projectFn, genericArgsMap, {root, keyPath}, C);

lib/SILGen/SILGenLValue.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,14 +1705,16 @@ namespace {
17051705
llvm_unreachable("not a writable key path type?!");
17061706
}
17071707

1708-
Substitution args[] = {
1709-
Substitution(keyPathTy->getGenericArgs()[0], {}),
1710-
Substitution(keyPathTy->getGenericArgs()[1], {}),
1711-
};
1712-
1713-
auto subMap = projectionFunction->getGenericSignature()
1714-
->getSubstitutionMap(args);
1715-
1708+
auto projectionGenericSig = projectionFunction->getGenericSignature();
1709+
auto subMap = projectionGenericSig->getSubstitutionMap(
1710+
[&](SubstitutableType *type) -> Type {
1711+
auto genericParam = cast<GenericTypeParamType>(type);
1712+
auto index =
1713+
projectionGenericSig->getGenericParamOrdinal(genericParam);
1714+
return keyPathTy->getGenericArgs()[index];
1715+
},
1716+
LookUpConformanceInSignature(*projectionGenericSig));
1717+
17161718
// The projection function behaves like an owning addressor, returning
17171719
// a pointer to the projected value and an owner reference that keeps
17181720
// it alive.
@@ -1761,13 +1763,17 @@ namespace {
17611763
SILType::getPrimitiveObjectType(keyPathTy->getCanonicalType()));
17621764
}
17631765

1764-
Substitution args[] = {
1765-
Substitution(keyPathTy->getGenericArgs()[0], {}),
1766-
Substitution(keyPathTy->getGenericArgs()[1], {}),
1767-
};
17681766
auto projectFn = C.getProjectKeyPathReadOnly(nullptr);
1769-
auto subMap = projectFn->getGenericSignature()
1770-
->getSubstitutionMap(args);
1767+
1768+
auto projectionGenericSig = projectFn->getGenericSignature();
1769+
auto subMap = projectionGenericSig->getSubstitutionMap(
1770+
[&](SubstitutableType *type) -> Type {
1771+
auto genericParam = cast<GenericTypeParamType>(type);
1772+
auto index =
1773+
projectionGenericSig->getGenericParamOrdinal(genericParam);
1774+
return keyPathTy->getGenericArgs()[index];
1775+
},
1776+
LookUpConformanceInSignature(*projectionGenericSig));
17711777

17721778
// Allocate a temporary to own the projected value.
17731779
auto &resultTL = SGF.getTypeLowering(keyPathTy->getGenericArgs()[1]);

lib/SILGen/SILGenPattern.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,8 +2631,10 @@ static void emitDiagnoseOfUnexpectedEnumCase(SILGenFunction &SGF,
26312631
ManagedValue metatype = SGF.B.createValueMetatype(loc, metatypeType, value);
26322632

26332633
Substitution sub{switchedValueSwiftType, /*Conformances*/None};
2634-
auto genericArgsMap =
2635-
diagnoseFailure->getGenericSignature()->getSubstitutionMap(sub);
2634+
auto diagnoseSignature = diagnoseFailure->getGenericSignature();
2635+
auto genericArgsMap = diagnoseSignature->getSubstitutionMap(
2636+
[&](SubstitutableType *type) -> Type { return switchedValueSwiftType; },
2637+
LookUpConformanceInSignature(*diagnoseSignature));
26362638

26372639
SGF.emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, genericArgsMap,
26382640
metatype,

0 commit comments

Comments
 (0)