Skip to content

Commit e785336

Browse files
author
Amritpan Kaur
committed
[CSSimplify] Resolve key path expression
by creating a BoundGenericType for the keypath expr with the keypath base and replacing the value with the value typeVar that will be resolved when the value typeVar is directly bound.
1 parent d666b04 commit e785336

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,6 +3982,20 @@ class ConstraintSystem {
39823982
bool resolveClosure(TypeVariableType *typeVar, Type contextualType,
39833983
ConstraintLocatorBuilder locator);
39843984

3985+
/// Given the fact a contextual type is now available for the type
3986+
/// variable representing one of the key path expressions, let's set a
3987+
/// pre-determined key path expression type.
3988+
///
3989+
/// \param typeVar The type variable representing a key path expression.
3990+
/// \param contextualType The contextual type this key path would be
3991+
/// converted to.
3992+
/// \param locator The locator associated with contextual type.
3993+
///
3994+
/// \returns `true` if it was possible to generate constraints for
3995+
/// the keyPath expression, `false` otherwise.
3996+
bool resolveKeyPath(TypeVariableType *typeVar, Type contextualType,
3997+
ConstraintLocatorBuilder locator);
3998+
39853999
/// Given the fact that contextual type is now available for the type
39864000
/// variable representing a pack expansion type, let's resolve the expansion.
39874001
///

lib/Sema/CSSimplify.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11517,6 +11517,28 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1151711517
return !generateConstraints(AnyFunctionRef{closure}, closure->getBody());
1151811518
}
1151911519

11520+
bool ConstraintSystem::resolveKeyPath(TypeVariableType *typeVar,
11521+
Type contextualType,
11522+
ConstraintLocatorBuilder locator) {
11523+
11524+
if (auto *BGT = contextualType->getAs<BoundGenericType>()) {
11525+
auto args = BGT->getGenericArgs();
11526+
if (isKnownKeyPathType(contextualType) && args.size() >= 1) {
11527+
auto root = BGT->getGenericArgs()[0];
11528+
11529+
auto *keyPathLocator = typeVar->getImpl().getLocator();
11530+
auto *keyPath = castToExpr<KeyPathExpr>(keyPathLocator->getAnchor());
11531+
auto *keyPathValueTV = getKeyPathValueType(keyPath);
11532+
contextualType = BoundGenericType::get(
11533+
args.size() == 1 ? getASTContext().getKeyPathDecl() : BGT->getDecl(),
11534+
/*parent=*/Type(), {root, keyPathValueTV});
11535+
}
11536+
}
11537+
11538+
assignFixedType(typeVar, contextualType);
11539+
return true;
11540+
}
11541+
1152011542
bool ConstraintSystem::resolvePackExpansion(TypeVariableType *typeVar,
1152111543
Type contextualType) {
1152211544
auto *locator = typeVar->getImpl().getLocator();

0 commit comments

Comments
 (0)