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-7015] The CoreFoundation conditional downcast diagnostic is not as helpful as it should be #49563

Closed
swift-ci opened this issue Feb 16, 2018 · 18 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 good first issue Good for newcomers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7015
Radar None
Original Reporter Anandabits (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee orakaro (JIRA)
Priority Medium

md5: 4c03f1e87c744b77d83845df192a1e31

Issue Description:

When the `as?` operator is used to target a CoreFoundation type the compiler produces a diagnostic which says "Conditional downcast to CoreFoundation type ... will always succeed". This does not offer any suggestions about what to do instead.

The diagnostic should be updated with a message that informs the developer to compare CFTypeIds (with a fixit if possible).

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 8, 2018

Comment by Vu Nhat Minh (JIRA)

While downcast will always succeed it it necessary to compare CFTypeIds? I was thinking of replacing with forced downcast but would like to know in which cases that could lead to problem? Thanks.

@swift-ci
Copy link
Collaborator Author

Comment by Naruki Chigira (JIRA)

orakaro (JIRA User)
I think real problem is that downcast will not always succeed. Actually, each types don't compared ( CAApply.cpp, SwiftObject.mm ). So I think there are 2 ways to fix this problem. Add to compare each types (if possible) or suggest using other cast operator.
I created PR before to try fix this problem by the way to suggest using operator `as` in this commit. I think `as` is somewhat better than `as!` because we should use `as` if cast actually succeed.
How do you think about above?

@swift-ci
Copy link
Collaborator Author

Comment by Matthew Johnson (JIRA)

None of the built-in cast operators are appropriate. In my code I am now using a custom `cfCast` operator that looks like this:

protocol CFCastable {
    /// The CFTypeID that identifies the conforming type.
    static var cfTypeID: CFTypeID { get }
}
extension CFString: CFCastable {
    static let cfTypeID = CFStringGetTypeID()
}
func cfCast<T: CFCastable>(_ typeRef: Any?, as target: T.Type) -> T? {
    guard typeRef != nil, CFGetTypeID(typeRef as AnyObject) == target.cfTypeID else {
        return nil
    }
    let result = typeRef as! T
    return result
}

@swift-ci
Copy link
Collaborator Author

Comment by Naruki Chigira (JIRA)

The diagnostic should be updated with a message that informs the developer to compare CFTypeIds

Does "informs the developer to compare CFTypeIds" means "informs the developer to create own function to compare CFTypeIds"...? I think that message is so difficult for developer...

@swift-ci
Copy link
Collaborator Author

Comment by Matthew Johnson (JIRA)

No, I don't think so. The developer can decide for themselves how to compare CFTypeIds. The important thing is to inform them that this is the correct way to implement this coercion.

@swift-ci
Copy link
Collaborator Author

Comment by Vu Nhat Minh (JIRA)

Anandabits (JIRA User) thanks for clear things out. Nice code example 👍

naru (JIRA User) I also created a PR which replace `as?` with `as!`, but as the comment pointed out there are cases that `fixItReplace` will produce code that won't compile. Your PR may faces the same problem.

if let cf = obj as? CFString { ... } // Conditional downcast to CoreFoundation type `CFString` will always succeed
if let cf = obj as CFString { ... } // Initializer for conditional binding must have Optional type, not 'CFString'

@swift-ci
Copy link
Collaborator Author

Comment by Naruki Chigira (JIRA)

Anandabits (JIRA User) Thank you for your detailed message! Perhaps I could understand what you said.

orakaro (JIRA User) Absolutely right! My PR has same problem.

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 8, 2018

Comment by Naruki Chigira (JIRA)

I posted new PR. Fixed message and added fix-it to resolve error.

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 8, 2018

Comment by Vu Nhat Minh (JIRA)

Hi, nice work and I will definitely look into it. I am sorry forgot to notify but I have been working on a PR too #16088 . It is actually reviewed and waiting for merge but doesn’t include a fix-it.

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 8, 2018

Comment by Naruki Chigira (JIRA)

orakaro (JIRA User) Oh.., sorry I missed your PR. I'll check what should I do on PR I opened.

-> I closed my PR and I’m not sure how I handle this bug. Anyway, congrats!!

@swift-ci
Copy link
Collaborator Author

Comment by Vu Nhat Minh (JIRA)

naru (JIRA User) Sorry for the late reply. The idea of ifx-it is interesting but I am not sure all CF types has same naming convention like `

CFStringGetTypeID` though. Thank you for your cooperation on this issue.

@swift-ci
Copy link
Collaborator Author

Comment by Vu Nhat Minh (JIRA)

Resolves by #16088

@swift-ci
Copy link
Collaborator Author

Comment by Naruki Chigira (JIRA)

orakaro (JIRA User) Thank you to see my PR and this was very interesting problem for me too 🙂

@mattneub
Copy link

Why is this marked as resolved? I'm still seeing this unhelpful diagnostic message all over the place.

@swift-ci
Copy link
Collaborator Author

Comment by Vu Nhat Minh (JIRA)

@mattneub Thanks for head-up. The PR is merged into master, but apparently not released yet. I will confirm with contributors team

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 1, 2019

Comment by Vu Nhat Minh (JIRA)

This seems to be auto-merged into Swift 5.

@mattneub
Copy link

mattneub commented Jan 1, 2019

orakaro (JIRA User) Thanks!

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

mran3 commented Apr 18, 2024

I see this still happening in 2024...

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 good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants