Skip to content
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

[SR-12422] Identity KeyPath literal is not working properly according to SE-0249 #54861

Closed
swift-ci opened this issue Mar 26, 2020 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself expressions Feature: expressions implicit conversions Feature: implicit conversions key paths Feature: key paths (both native and Objective-C) swift 5.10 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12422
Radar rdar://problem/62201019
Original Reporter pacheco (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 11.4 (11E146)
macOS 10.15.3 (19D76)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: dff283ffe45d137ca02f1f96d564a0f1

Issue Description:

When attempting to use the identity KeyPath literal (.self) as a function according to functionality in SE-0249, the code fails to compile. Example:

let x: [Int?] = [1, nil, 2]
let x2 = x.compactMap(\.self) // doesn't work -> "Key path value type 'Optional<_>' cannot be converted to contextual type '_'"
let x3: [Int] = x.compactMap(\Int?.self) // doesn't work -> "Cannot convert value of type 'WritableKeyPath<Int?, Int?>' to expected argument type '(Int?) throws -> Int?'"

However if one assigns an identity KeyPath to a variable of type (Root) -> Root, it succeeds:

let xFunc: (Int?) -> Int? = \Int?.self
let x4 = x.compactMap(xFunc) // works

It also succeeds if we define an operator on KeyPath:

prefix operator ^
prefix func ^ <Root, Value>(_ kp: KeyPath<Root, Value>) -> (Root) -> Value { { root in root[keyPath: kp] } }

let x5 = x.compactMap(^\.self) // works

More examples can be found on the attached playground.

From my very basic understanding of this (and please excuse me if I'm saying something stupid!), could it be that the Identity KeyPath's special handling (from SE-0227 is not playing well with the new logic implemented in SE-0249 to convert KeyPath's to functions? Because if .self is translated to a MutableKeyPath<T, T> and MutableKeyPath: KeyPath, it should work like any other KeyPath.

Thanks!

@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@kateinoigakukun
Copy link
Member

All of the examples in the description are now working. Closing as well as #55343 (comment)

@AnthonyLatsis AnthonyLatsis added type checker Area → compiler: Semantic analysis key paths Feature: key paths (both native and Objective-C) expressions Feature: expressions unexpected error Bug: Unexpected error implicit conversions Feature: implicit conversions swift 5.10 labels Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself expressions Feature: expressions implicit conversions Feature: implicit conversions key paths Feature: key paths (both native and Objective-C) swift 5.10 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

4 participants