Skip to content

Commit 5fa54df

Browse files
author
Amritpan Kaur
committed
[CSSimplify] Delay matchTypes for key path values
until they are directly bound.
1 parent e785336 commit 5fa54df

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

include/swift/Sema/ConstraintLocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
254254

255255
/// Determine whether given locator points to the keypath value
256256
bool isKeyPathValue() const;
257-
257+
258258
/// Determine whether given locator points to the choice picked as
259259
/// as result of the key path dynamic member lookup operation.
260260
bool isResultOfKeyPathDynamicMemberLookup() const;

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ class TypeVariableType::Implementation {
486486
/// a type of a key path expression.
487487
bool isKeyPathType() const;
488488

489+
/// Determine whether this type variable represents a value type of a key path
490+
/// expression.
491+
bool isKeyPathValue() const;
492+
489493
/// Determine whether this type variable represents a subscript result type.
490494
bool isSubscriptResultType() const;
491495

lib/Sema/CSSimplify.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4430,6 +4430,13 @@ ConstraintSystem::matchTypesBindTypeVar(
44304430
: getTypeMatchFailure(locator);
44314431
}
44324432

4433+
if (typeVar->getImpl().isKeyPathType()) {
4434+
if (flags.contains(TMF_BindingTypeVariable))
4435+
return resolveKeyPath(typeVar, type, locator)
4436+
? getTypeMatchSuccess()
4437+
: getTypeMatchFailure(locator);
4438+
}
4439+
44334440
assignFixedType(typeVar, type, /*updateState=*/true,
44344441
/*notifyInference=*/!flags.contains(TMF_BindingTypeVariable));
44354442

@@ -6690,6 +6697,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
66906697
return getTypeMatchSuccess();
66916698
}
66926699

6700+
// If type variable represents a key path value type, defer binding it to
6701+
// contextual type in diagnostic mode. We want it to be bound from the
6702+
// last key path component to help with diagnostics.
6703+
if (shouldAttemptFixes()) {
6704+
if (typeVar1 && typeVar1->getImpl().isKeyPathValue() &&
6705+
!flags.contains(TMF_BindingTypeVariable))
6706+
return formUnsolvedResult();
6707+
}
6708+
66936709
assert((type1->is<TypeVariableType>() != type2->is<TypeVariableType>()) &&
66946710
"Expected a type variable and a non type variable!");
66956711

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ bool TypeVariableType::Implementation::isKeyPathType() const {
144144
return locator && locator->isKeyPathType();
145145
}
146146

147+
bool TypeVariableType::Implementation::isKeyPathValue() const {
148+
return locator && locator->isKeyPathValue();
149+
}
150+
147151
bool TypeVariableType::Implementation::isSubscriptResultType() const {
148152
if (!(locator && locator->getAnchor()))
149153
return false;

0 commit comments

Comments
 (0)