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-15292] Key path literal gives bogus diagnostic message for unwrapping #57614

Open
swift-ci opened this issue Oct 7, 2021 · 5 comments
Open
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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 7, 2021

Previous ID SR-15292
Radar None
Original Reporter jumhyn (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: 2fa2ab67b572f5c821108b477ec42d1d

Issue Description:

This code:

let _: KeyPath<String?, Int> = \String.count

Fails to compile with the error "Value of optional type 'String?' must be unwrapped to refer to member 'count' of wrapped base type 'String'" which is clearly wrong.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 7, 2021

Comment by Frederick Kellison-Linn (JIRA)

cc @LucianoPAlmeida seems potentially related to SR-13079

@LucianoPAlmeida
Copy link
Collaborator

Yeah, they are related in the sense of key path root optional mismatch
I'm thinking on how to improve this ... it could probably be something like `cannot convert key path root type 'String' to specified type 'String?'`, with a fix-it to make it optional to match the contextual type and unwrap to access unwrapped `String` member: `let _: KeyPath<String?, Int> = String?.!.count`, that for sure will be a better diagnostic.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 7, 2021

Comment by Frederick Kellison-Linn (JIRA)

Yeah even without the fix-it that would be a much better diagnostic.

I believe this exhibits a similar issue:

class Base {}
class Derived: Base {
    var x: Int = 0
}

let _: KeyPath<Base, Int> = \Derived.x // ERROR: value of type 'Base' has no member 'x'

Due to how we generate constraints for key path expressions:

        // Allow \Derived.property to be inferred as \Base.property to
        // simulate a sort of covariant conversion from
        // KeyPath<Derived, T> to KeyPath<Base, T>.
        CS.addConstraint(ConstraintKind::Subtype, rootObjectTy, root, locator);

@LucianoPAlmeida
Copy link
Collaborator

Ah that is interesting, I was more focused on optionality mismatches and actually don't remember having seeing this issue, does this constraint is in-place to allow code like this

class Base {
  var x: Int = 0
}
class Derived: Base {}

let _: KeyPath<Base, Int> = \Derived.x // Ok

? Maybe I didn't understand it correctly ...

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 7, 2021

Comment by Frederick Kellison-Linn (JIRA)

I believe that's the idea, yeah. I haven't dug into the history to be sure though. I stumbled across this behavior when I thought I had broken something during my prototype for function-function conversions with keypath literals, but then I realized this behavior exists for 'normal' key paths as well.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the compiler The Swift compiler in itself label Dec 13, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants