Skip to content

Commit a7f9182

Browse files
authored
Merge pull request #68048 from amritpan/kp-refs
[ConstraintSystem] Handle incorrect key path member references earlier
2 parents fb438e0 + 6eadf2d commit a7f9182

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

include/swift/Sema/ConstraintLocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
275275
/// a key path component.
276276
bool isForKeyPathComponentResult() const;
277277

278+
/// Determine whether this locator points to a key path component.
279+
bool isForKeyPathComponent() const;
280+
278281
/// Determine whether this locator points to the generic parameter.
279282
bool isForGenericParameter() const;
280283

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10332,7 +10332,9 @@ static ConstraintFix *fixMemberRef(
1033210332
return fix;
1033310333
}
1033410334

10335-
if (locator->isForKeyPathDynamicMemberLookup()) {
10335+
if (locator->isForKeyPathDynamicMemberLookup() ||
10336+
locator->isForKeyPathComponent() ||
10337+
locator->isKeyPathSubscriptComponent()) {
1033610338
if (auto *fix = AllowInvalidRefInKeyPath::forRef(cs, decl, locator))
1033710339
return fix;
1033810340
}
@@ -12303,11 +12305,9 @@ ConstraintSystem::simplifyKeyPathConstraint(
1230312305

1230412306
auto storage = dyn_cast<AbstractStorageDecl>(choice.getDecl());
1230512307

12306-
if (auto *fix = AllowInvalidRefInKeyPath::forRef(
12307-
*this, choice.getDecl(), calleeLoc)) {
12308-
if (!hasFixFor(calleeLoc, FixKind::AllowTypeOrInstanceMember))
12309-
if (!shouldAttemptFixes() || recordFix(fix))
12310-
return SolutionKind::Error;
12308+
if (hasFixFor(calleeLoc, FixKind::AllowInvalidRefInKeyPath)) {
12309+
if (!shouldAttemptFixes())
12310+
return SolutionKind::Error;
1231112311

1231212312
// If this was a method reference let's mark it as read-only.
1231312313
if (!storage) {

lib/Sema/ConstraintLocator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,10 @@ bool ConstraintLocator::isForKeyPathComponentResult() const {
598598
return isLastElement<LocatorPathElt::KeyPathComponentResult>();
599599
}
600600

601+
bool ConstraintLocator::isForKeyPathComponent() const {
602+
return isLastElement<LocatorPathElt::KeyPathComponent>();
603+
}
604+
601605
bool ConstraintLocator::isForGenericParameter() const {
602606
return isLastElement<LocatorPathElt::GenericParameter>();
603607
}

test/expr/unary/keypath/keypath.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ func test_keypath_with_method_refs() {
795795

796796
let _: KeyPath<S, Int> = \.foo // expected-error {{key path cannot refer to instance method 'foo()'}}
797797
// expected-error@-1 {{key path value type '() -> Int' cannot be converted to contextual type 'Int'}}
798-
let _: KeyPath<S, Int> = \.bar // expected-error {{key path cannot refer to static member 'bar()'}}
798+
let _: KeyPath<S, Int> = \.bar // expected-error {{key path cannot refer to static method 'bar()'}}
799+
// expected-error@-1 {{key path value type '() -> Int' cannot be converted to contextual type 'Int'}}
799800
let _ = \S.Type.bar // expected-error {{key path cannot refer to static method 'bar()'}}
800801

801802
struct A {
@@ -808,7 +809,7 @@ func test_keypath_with_method_refs() {
808809
}
809810

810811
let _: KeyPath<A, Int> = \.foo.bar // expected-error {{key path cannot refer to instance method 'foo()'}}
811-
let _: KeyPath<A, Int> = \.faz.bar // expected-error {{key path cannot refer to static member 'faz()'}}
812+
let _: KeyPath<A, Int> = \.faz.bar // expected-error {{key path cannot refer to static method 'faz()'}}
812813
let _ = \A.foo.bar // expected-error {{key path cannot refer to instance method 'foo()'}}
813814
let _ = \A.Type.faz.bar // expected-error {{key path cannot refer to static method 'faz()'}}
814815
}

0 commit comments

Comments
 (0)