Skip to content

Commit cd20d14

Browse files
committed
[CS] Don't leave key path with holes unsolved
We currently leave a key path constraint unsolved if one of its components hasn't yet had its overload resolved. However, for e.g a missing member component, the overload type variable will be bound to a hole and an overload will never be resolved. Tweak the logic to consider the key path constraint trivially solved if one of its components has been marked as a hole, which will allow the key path type itself to be marked as a hole. Resolves SR-12437 & SR-12823. Resolves rdar://62201037.
1 parent 7d2a240 commit cd20d14

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7697,10 +7697,30 @@ ConstraintSystem::simplifyKeyPathConstraint(
76977697
return true;
76987698
};
76997699

7700-
// We have a hole, the solver can't infer the key path type. So let's
7701-
// just assume this is solved.
7702-
if (shouldAttemptFixes() && keyPathTy->isHole()) {
7703-
return SolutionKind::Solved;
7700+
// If we have a hole somewhere in the key path, the solver won't be able to
7701+
// infer the key path type. So let's just assume this is solved.
7702+
if (shouldAttemptFixes()) {
7703+
if (keyPathTy->isHole())
7704+
return SolutionKind::Solved;
7705+
7706+
// If the root type has been bound to a hole, we cannot infer it.
7707+
if (getFixedTypeRecursive(rootTy, /*wantRValue*/ true)->isHole())
7708+
return SolutionKind::Solved;
7709+
7710+
// If we have e.g a missing member somewhere, a component type variable
7711+
// will have been marked as a potential hole.
7712+
// FIXME: This relies on the fact that we only mark an overload type
7713+
// variable as a potential hole once we've added a corresponding fix. We
7714+
// can't use 'isHole' instead, as that doesn't handle cases where the
7715+
// overload type variable gets bound to another type from the context rather
7716+
// than a hole. We need to come up with a better way of handling the
7717+
// relationship between key paths and overloads.
7718+
if (llvm::any_of(componentTypeVars, [&](TypeVariableType *tv) {
7719+
return tv->getImpl().getLocator()->isForKeyPathComponent() &&
7720+
tv->getImpl().canBindToHole();
7721+
})) {
7722+
return SolutionKind::Solved;
7723+
}
77047724
}
77057725

77067726
// If we're fixed to a bound generic type, trying harvesting context from it.
@@ -7751,34 +7771,11 @@ ConstraintSystem::simplifyKeyPathConstraint(
77517771
// to determine whether the result will be a function type vs BGT KeyPath
77527772
// type, so continue through components to create new constraint at the
77537773
// end.
7754-
if (!overload || anyComponentsUnresolved) {
7774+
if (!overload) {
77557775
if (flags.contains(TMF_GenerateConstraints)) {
77567776
anyComponentsUnresolved = true;
77577777
continue;
77587778
}
7759-
7760-
if (shouldAttemptFixes()) {
7761-
auto typeVar =
7762-
llvm::find_if(componentTypeVars, [&](TypeVariableType *typeVar) {
7763-
auto *locator = typeVar->getImpl().getLocator();
7764-
auto elt = locator->findLast<LocatorPathElt::KeyPathComponent>();
7765-
return elt && elt->getIndex() == i;
7766-
});
7767-
7768-
// If one of the components haven't been resolved, let's check
7769-
// whether it has been determined to be a "hole" and if so,
7770-
// let's allow component validation to contiunue.
7771-
//
7772-
// This helps to, for example, diagnose problems with missing
7773-
// members used as part of a key path.
7774-
if (typeVar != componentTypeVars.end() &&
7775-
(*typeVar)->getImpl().canBindToHole()) {
7776-
anyComponentsUnresolved = true;
7777-
capability = ReadOnly;
7778-
continue;
7779-
}
7780-
}
7781-
77827779
return SolutionKind::Unsolved;
77837780
}
77847781

test/Constraints/rdar62201037.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-swift-frontend %s -verify -emit-sil -o /dev/null
2+
3+
struct R<T> {
4+
var str: String?
5+
}
6+
7+
func map<A, B>(e: (A) -> B) -> () -> R<B> {
8+
fatalError()
9+
}
10+
func map<A, B>(_ : (A) -> B) -> (A?) -> B? {
11+
fatalError()
12+
}
13+
14+
infix operator |>
15+
func |> <A, B> (g: A, h: (A) -> B) -> B { h(g) }
16+
17+
infix operator ^^^
18+
func ^^^ <A, B, C>(j: ((B) -> C) -> A, k: String) {}
19+
20+
extension WritableKeyPath {
21+
static func ^^^ (l: WritableKeyPath, m: Value) -> (Root) -> Root {
22+
fatalError()
23+
}
24+
}
25+
26+
func foo<T>(_ s: String, _ rt: R<T>?) -> String? {
27+
return rt.flatMap { _ in
28+
rt |> map(\.str ^^^ s)
29+
}
30+
.flatMap(\.str)
31+
}

test/expr/unary/keypath/keypath.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,40 @@ struct SR_12290 {
895895

896896
func testKeyPathHole() {
897897
_ = \.x // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{8-8=<#Root#>}}
898+
_ = \.x.y // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{8-8=<#Root#>}}
899+
898900
let _ : AnyKeyPath = \.x
899901
// expected-error@-1 {{'AnyKeyPath' does not provide enough context for root type to be inferred; consider explicitly specifying a root type}} {{25-25=<#Root#>}}
902+
let _ : AnyKeyPath = \.x.y
903+
// expected-error@-1 {{'AnyKeyPath' does not provide enough context for root type to be inferred; consider explicitly specifying a root type}} {{25-25=<#Root#>}}
900904

901905
func f(_ i: Int) {}
902906
f(\.x) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{6-6=<#Root#>}}
907+
f(\.x.y) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{6-6=<#Root#>}}
908+
909+
// FIXME(SR-12827): Instead of "generic parameter 'T' could not be inferred",
910+
// we should offer the same diagnostic as above.
911+
func provideValueButNotRoot<T>(_ fn: (T) -> String) {} // expected-note 2{{in call to function 'provideValueButNotRoot'}}
912+
provideValueButNotRoot(\.x) // expected-error {{generic parameter 'T' could not be inferred}}
913+
provideValueButNotRoot(\.x.y) // expected-error {{generic parameter 'T' could not be inferred}}
914+
provideValueButNotRoot(\String.foo) // expected-error {{value of type 'String' has no member 'foo'}}
915+
916+
func provideKPValueButNotRoot<T>(_ kp: KeyPath<T, String>) {} // expected-note 3{{in call to function 'provideKPValueButNotRoot'}}
917+
provideKPValueButNotRoot(\.x) // expected-error {{generic parameter 'T' could not be inferred}}
918+
provideKPValueButNotRoot(\.x.y) // expected-error {{generic parameter 'T' could not be inferred}}
919+
provideKPValueButNotRoot(\String.foo)
920+
// expected-error@-1 {{value of type 'String' has no member 'foo'}}
921+
// expected-error@-2 {{generic parameter 'T' could not be inferred}}
922+
}
923+
924+
func testMissingMember() {
925+
let _: KeyPath<String, String> = \.foo // expected-error {{value of type 'String' has no member 'foo'}}
926+
let _: KeyPath<String, String> = \.foo.bar // expected-error {{value of type 'String' has no member 'foo'}}
927+
928+
let _: PartialKeyPath<String> = \.foo // expected-error {{value of type 'String' has no member 'foo'}}
929+
let _: PartialKeyPath<String> = \.foo.bar // expected-error {{value of type 'String' has no member 'foo'}}
930+
931+
_ = \String.x.y // expected-error {{value of type 'String' has no member 'x'}}
903932
}
904933

905934
func testSyntaxErrors() { // expected-note{{}}

0 commit comments

Comments
 (0)