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-5551] Keypath of implicitly unwrapped optional #48123

Closed
swift-ci opened this issue Jul 25, 2017 · 7 comments
Closed

[SR-5551] Keypath of implicitly unwrapped optional #48123

swift-ci opened this issue Jul 25, 2017 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5551
Radar rdar://problem/33525846
Original Reporter cuva (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 4

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee None
Priority Medium

md5: 807f5fda1f3703ae233a77354933c497

Issue Description:

We're unable to pass a key path referencing an implicitly unwrapped optional object to a function as:

let keyPath = \UIView.tintColor // ReferenceWritableKeyPath<UIView, UIColor!>

func doSomething(using keyPath: ReferenceWritableKeyPath<UIView, UIColor!>) {
  [...]
}

Fails with the following build error:

error: implicitly unwrapped optionals are only allowed at top level and as function results
func doSomething(using keyPath: ReferenceWritableKeyPath<UIView, UIColor!>)

Is this a known issue?

Should key path of implicitly unwrapped optionals be mapped to optionals?

@belkadan
Copy link
Contributor

@jckarter, did your work on regular Optionals fix this?

@jckarter
Copy link
Member

The error is correct, to the letter of SE-0054 at least, though we have lots of known issues with the implementation. `T!` is in theory an attribute that only applies to certain declarations and becomes plain `T?` once you bring it into arbitrary type position. ReferenceWritableKeyPath<UIView, UIColor?> ought to work.

@belkadan
Copy link
Contributor

Oops, I completely glanced past the actual text of the message. Maybe we can change the diagnostic here to suggest using Optional instead.

@belkadan
Copy link
Contributor

Ah, but the implicit type of the key path has the IUO in it. That's a key-path-specific bug.

let keyPath = \UIView.tintColor
let x: Int = keyPath

error: cannot convert value of type 'ReferenceWritableKeyPath<UIView, UIColor!>' to specified type 'Int'

@jckarter
Copy link
Member

Well, it's a systemic problem with the type checker still handling IUO as a first-class type. This isn't the only place it manifests.

@jckarter
Copy link
Member

@swift-ci create

@rjmccall
Copy link
Member

This has been fixed now that we don't treat IUO as a separate type. We give a fixit to use ? instead of !, and ? causes the code to compile.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

4 participants