Skip to content

Commit 62719b0

Browse files
authored
Merge pull request #67157 from amritpan/kp-declcontext
[Constraint System] Store key path root, value, and decl context for use across constraint system.
2 parents e2b39ed + 1d8e7ef commit 62719b0

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,13 @@ class Solution {
14991499
llvm::DenseMap<std::pair<const KeyPathExpr *, unsigned>, Type>
15001500
keyPathComponentTypes;
15011501

1502+
/// The key path expression and its root type, value type, and decl context
1503+
/// introduced by this solution.
1504+
llvm::MapVector<const KeyPathExpr *,
1505+
std::tuple</*root=*/TypeVariableType *,
1506+
/*value=*/TypeVariableType *, DeclContext *>>
1507+
KeyPaths;
1508+
15021509
/// Contextual types introduced by this solution.
15031510
std::vector<std::pair<ASTNode, ContextualTypeInfo>> contextualTypes;
15041511

@@ -2164,6 +2171,12 @@ class ConstraintSystem {
21642171
std::tuple<const KeyPathExpr *, /*component index=*/unsigned, Type>>
21652172
addedKeyPathComponentTypes;
21662173

2174+
/// Maps a key path root, value, and decl context to the key path expression.
2175+
llvm::MapVector<const KeyPathExpr *,
2176+
std::tuple</*root=*/TypeVariableType *,
2177+
/*value=*/TypeVariableType *, DeclContext *>>
2178+
KeyPaths;
2179+
21672180
/// Maps AST entries to their targets.
21682181
llvm::MapVector<SyntacticElementTargetKey, SyntacticElementTarget> targets;
21692182

@@ -2796,6 +2809,9 @@ class ConstraintSystem {
27962809
/// The length of \c ImplicitValueConversions.
27972810
unsigned numImplicitValueConversions;
27982811

2812+
/// The length of \c KeyPaths.
2813+
unsigned numKeyPaths;
2814+
27992815
/// The length of \c ArgumentLists.
28002816
unsigned numArgumentLists;
28012817

@@ -3427,6 +3443,11 @@ class ConstraintSystem {
34273443
void recordCallAsFunction(UnresolvedDotExpr *root, ArgumentList *arguments,
34283444
ConstraintLocator *locator);
34293445

3446+
/// Record root, value, and declContext of keypath expression for use across
3447+
/// constraint system.
3448+
void recordKeyPath(KeyPathExpr *keypath, TypeVariableType *root,
3449+
TypeVariableType *value, DeclContext *dc);
3450+
34303451
/// Walk a closure AST to determine its effects.
34313452
///
34323453
/// \returns a function's extended info describing the effects, as

lib/Sema/CSGen.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,8 +3670,8 @@ struct VarRefCollector : public ASTWalker {
36703670
auto rootLocator =
36713671
CS.getConstraintLocator(E, ConstraintLocator::KeyPathRoot);
36723672
auto locator = CS.getConstraintLocator(E);
3673-
Type root = CS.createTypeVariable(rootLocator, TVO_CanBindToNoEscape |
3674-
TVO_CanBindToHole);
3673+
auto *root = CS.createTypeVariable(rootLocator, TVO_CanBindToNoEscape |
3674+
TVO_CanBindToHole);
36753675

36763676
// If a root type was explicitly given, then resolve it now.
36773677
if (auto rootRepr = E->getRootType()) {
@@ -3808,22 +3808,24 @@ struct VarRefCollector : public ASTWalker {
38083808
base = optTy;
38093809
}
38103810

3811-
auto baseLocator =
3811+
auto valueLocator =
38123812
CS.getConstraintLocator(E, ConstraintLocator::KeyPathValue);
3813-
auto rvalueBase = CS.createTypeVariable(
3814-
baseLocator, TVO_CanBindToNoEscape | TVO_CanBindToHole);
3815-
CS.addConstraint(ConstraintKind::Equal, base, rvalueBase, locator);
3813+
auto *value = CS.createTypeVariable(valueLocator, TVO_CanBindToNoEscape |
3814+
TVO_CanBindToHole);
3815+
CS.addConstraint(ConstraintKind::Equal, base, value, locator);
3816+
CS.recordKeyPath(E, root, value, CurDC);
38163817

38173818
// The result is a KeyPath from the root to the end component.
38183819
// The type of key path depends on the overloads chosen for the key
38193820
// path components.
3820-
auto typeLoc = CS.getConstraintLocator(
3821-
locator, LocatorPathElt::KeyPathType(rvalueBase));
3821+
auto typeLoc =
3822+
CS.getConstraintLocator(locator, LocatorPathElt::KeyPathType(value));
38223823

38233824
Type kpTy = CS.createTypeVariable(typeLoc, TVO_CanBindToNoEscape |
38243825
TVO_CanBindToHole);
3825-
CS.addKeyPathConstraint(kpTy, root, rvalueBase, componentTypeVars,
3826-
locator);
3826+
3827+
CS.addKeyPathConstraint(kpTy, root, value, componentTypeVars, locator);
3828+
38273829
return kpTy;
38283830
}
38293831

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14629,6 +14629,12 @@ void ConstraintSystem::recordCallAsFunction(UnresolvedDotExpr *root,
1462914629
getConstraintLocator(root, ConstraintLocator::ApplyArgument), arguments);
1463014630
}
1463114631

14632+
void ConstraintSystem::recordKeyPath(KeyPathExpr *keypath,
14633+
TypeVariableType *root,
14634+
TypeVariableType *value, DeclContext *dc) {
14635+
KeyPaths.insert(std::make_pair(keypath, std::make_tuple(root, value, dc)));
14636+
}
14637+
1463214638
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1463314639
ConstraintFix *fix, Type type1, Type type2, ConstraintKind matchKind,
1463414640
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {

lib/Sema/CSSolver.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ Solution ConstraintSystem::finalize() {
188188
solution.keyPathComponentTypes.insert(keyPathComponentType);
189189
}
190190

191+
// Remember key paths.
192+
for (const auto &keyPaths : KeyPaths) {
193+
solution.KeyPaths.insert(keyPaths);
194+
}
195+
191196
// Remember contextual types.
192197
for (auto &entry : contextualTypes) {
193198
solution.contextualTypes.push_back({entry.first, entry.second.first});
@@ -311,6 +316,11 @@ void ConstraintSystem::applySolution(const Solution &solution) {
311316
nodeType.getSecond());
312317
}
313318

319+
// Add key paths.
320+
for (const auto &keypath : solution.KeyPaths) {
321+
KeyPaths.insert(keypath);
322+
}
323+
314324
// Add the contextual types.
315325
for (const auto &contextualType : solution.contextualTypes) {
316326
if (!getContextualTypeInfo(contextualType.first)) {
@@ -624,6 +634,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
624634
numDefaultedConstraints = cs.DefaultedConstraints.size();
625635
numAddedNodeTypes = cs.addedNodeTypes.size();
626636
numAddedKeyPathComponentTypes = cs.addedKeyPathComponentTypes.size();
637+
numKeyPaths = cs.KeyPaths.size();
627638
numDisabledConstraints = cs.solverState->getNumDisabledConstraints();
628639
numFavoredConstraints = cs.solverState->getNumFavoredConstraints();
629640
numResultBuilderTransformed = cs.resultBuilderTransformed.size();
@@ -735,6 +746,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
735746
}
736747
truncate(cs.addedKeyPathComponentTypes, numAddedKeyPathComponentTypes);
737748

749+
/// Remove any key path expressions.
750+
truncate(cs.KeyPaths, numKeyPaths);
751+
738752
/// Remove any builder transformed closures.
739753
truncate(cs.resultBuilderTransformed, numResultBuilderTransformed);
740754

0 commit comments

Comments
 (0)