Skip to content

[ConstraintSystem] Handle incorrect key path member references earlier #68048

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 3 commits into from
Aug 23, 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
3 changes: 3 additions & 0 deletions include/swift/Sema/ConstraintLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
/// a key path component.
bool isForKeyPathComponentResult() const;

/// Determine whether this locator points to a key path component.
bool isForKeyPathComponent() const;

/// Determine whether this locator points to the generic parameter.
bool isForGenericParameter() const;

Expand Down
12 changes: 6 additions & 6 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10332,7 +10332,9 @@ static ConstraintFix *fixMemberRef(
return fix;
}

if (locator->isForKeyPathDynamicMemberLookup()) {
if (locator->isForKeyPathDynamicMemberLookup() ||
locator->isForKeyPathComponent() ||
locator->isKeyPathSubscriptComponent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe isKeyPathSubscriptComponent should be folded into isForKeyPathComponent?

if (auto *fix = AllowInvalidRefInKeyPath::forRef(cs, decl, locator))
return fix;
}
Expand Down Expand Up @@ -12303,11 +12305,9 @@ ConstraintSystem::simplifyKeyPathConstraint(

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

if (auto *fix = AllowInvalidRefInKeyPath::forRef(
*this, choice.getDecl(), calleeLoc)) {
if (!hasFixFor(calleeLoc, FixKind::AllowTypeOrInstanceMember))
if (!shouldAttemptFixes() || recordFix(fix))
return SolutionKind::Error;
if (hasFixFor(calleeLoc, FixKind::AllowInvalidRefInKeyPath)) {
if (!shouldAttemptFixes())
return SolutionKind::Error;

// If this was a method reference let's mark it as read-only.
if (!storage) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/ConstraintLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ bool ConstraintLocator::isForKeyPathComponentResult() const {
return isLastElement<LocatorPathElt::KeyPathComponentResult>();
}

bool ConstraintLocator::isForKeyPathComponent() const {
return isLastElement<LocatorPathElt::KeyPathComponent>();
}

bool ConstraintLocator::isForGenericParameter() const {
return isLastElement<LocatorPathElt::GenericParameter>();
}
Expand Down
5 changes: 3 additions & 2 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ func test_keypath_with_method_refs() {

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

struct A {
Expand All @@ -808,7 +809,7 @@ func test_keypath_with_method_refs() {
}

let _: KeyPath<A, Int> = \.foo.bar // expected-error {{key path cannot refer to instance method 'foo()'}}
let _: KeyPath<A, Int> = \.faz.bar // expected-error {{key path cannot refer to static member 'faz()'}}
let _: KeyPath<A, Int> = \.faz.bar // expected-error {{key path cannot refer to static method 'faz()'}}
let _ = \A.foo.bar // expected-error {{key path cannot refer to instance method 'foo()'}}
let _ = \A.Type.faz.bar // expected-error {{key path cannot refer to static method 'faz()'}}
}
Expand Down