Skip to content

Commit b7a7024

Browse files
committed
[ConstraintLocator] Add information about expected value type to key path locator
Record a "value" type associated with this location to be able to reference during inference.
1 parent 1a07733 commit b7a7024

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

include/swift/Sema/ConstraintLocator.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,22 @@ class LocatorPathElt::ContextualType final : public StoredIntegerElement<1> {
910910
}
911911
};
912912

913+
class LocatorPathElt::KeyPathType final
914+
: public StoredPointerElement<TypeBase> {
915+
public:
916+
KeyPathType(Type valueType)
917+
: StoredPointerElement(PathElementKind::KeyPathType,
918+
valueType.getPointer()) {
919+
assert(valueType);
920+
}
921+
922+
Type getValueType() const { return getStoredPointer(); }
923+
924+
static bool classof(const LocatorPathElt *elt) {
925+
return elt->getKind() == PathElementKind::KeyPathType;
926+
}
927+
};
928+
913929
namespace details {
914930
template <typename CustomPathElement>
915931
class PathElement {

include/swift/Sema/ConstraintLocatorPathElts.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ CUSTOM_LOCATOR_PATH_ELT(KeyPathDynamicMember)
114114
SIMPLE_LOCATOR_PATH_ELT(KeyPathRoot)
115115

116116
/// The type of the key path expression.
117-
SIMPLE_LOCATOR_PATH_ELT(KeyPathType)
117+
CUSTOM_LOCATOR_PATH_ELT(KeyPathType)
118118

119119
/// The value of a key path.
120120
SIMPLE_LOCATOR_PATH_ELT(KeyPathValue)

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,10 +3258,11 @@ namespace {
32583258
// The result is a KeyPath from the root to the end component.
32593259
// The type of key path depends on the overloads chosen for the key
32603260
// path components.
3261-
auto typeLoc =
3262-
CS.getConstraintLocator(locator, ConstraintLocator::KeyPathType);
3261+
auto typeLoc = CS.getConstraintLocator(
3262+
locator, LocatorPathElt::KeyPathType(rvalueBase));
3263+
32633264
Type kpTy = CS.createTypeVariable(typeLoc, TVO_CanBindToNoEscape |
3264-
TVO_CanBindToHole);
3265+
TVO_CanBindToHole);
32653266
CS.addKeyPathConstraint(kpTy, root, rvalueBase, componentTypeVars,
32663267
locator);
32673268
return kpTy;

lib/Sema/ConstraintLocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool ConstraintLocator::isKeyPathType() const {
122122
// The format of locator should be `<keypath expr> -> key path type`
123123
if (!anchor || !isExpr<KeyPathExpr>(anchor) || path.size() != 1)
124124
return false;
125-
return path.back().getKind() == ConstraintLocator::KeyPathType;
125+
return path.back().is<LocatorPathElt::KeyPathType>();
126126
}
127127

128128
bool ConstraintLocator::isKeyPathRoot() const {

0 commit comments

Comments
 (0)