Skip to content

[ConstraintSystem] Resolve key path function applications via resolveKeyPath #67971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {

if (TypeVar->getImpl().isKeyPathType()) {
auto objectTy = type->lookThroughAllOptionalTypes();
if (!isKnownKeyPathType(objectTy))
if (!(isKnownKeyPathType(objectTy) || objectTy->is<AnyFunctionType>()))
return llvm::None;

auto &ctx = CS.getASTContext();
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6827,8 +6827,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// unexpected state for key path diagnostics should we fail.
if (locator->isLastElement<LocatorPathElt::KeyPathType>() &&
type2->is<AnyFunctionType>())
return matchTypesBindTypeVar(typeVar1, type2, kind, flags, locator,
formUnsolvedResult);
return matchTypesBindTypeVar(typeVar1, type2, kind,
flags | TMF_BindingTypeVariable,
locator, formUnsolvedResult);
}

// Performance optimization: Propagate fully or partially resolved
Expand Down Expand Up @@ -12190,6 +12191,7 @@ ConstraintSystem::simplifyKeyPathConstraint(
return locator->isInKeyPathComponent() &&
tv->getImpl().canBindToHole();
})) {
(void)tryMatchRootAndValueFromType(keyPathTy);
return SolutionKind::Solved;
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -918,13 +918,12 @@ func testKeyPathHole() {
provideValueButNotRoot(\.x.y) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}}
provideValueButNotRoot(\String.foo) // expected-error {{value of type 'String' has no member 'foo'}}

func provideKPValueButNotRoot<T>(_ kp: KeyPath<T, String>) {} // expected-note {{in call to function 'provideKPValueButNotRoot'}}
func provideKPValueButNotRoot<T>(_ kp: KeyPath<T, String>) {}
provideKPValueButNotRoot(\.x) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}}
provideKPValueButNotRoot(\.x.y) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}}

provideKPValueButNotRoot(\String.foo)
// expected-error@-1 {{value of type 'String' has no member 'foo'}}
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
}

func testMissingMember() {
Expand Down