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
Comments
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. |
Comment by Naruki Chigira (JIRA) orakaro (JIRA User) |
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
} |
Comment by Naruki Chigira (JIRA)
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... |
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. |
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' |
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. |
Comment by Naruki Chigira (JIRA) I posted new PR. Fixed message and added fix-it to resolve error. |
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. |
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!! |
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. |
Comment by Vu Nhat Minh (JIRA) Resolves by #16088 |
Comment by Naruki Chigira (JIRA) orakaro (JIRA User) Thank you to see my PR and this was very interesting problem for me too 🙂 |
Why is this marked as resolved? I'm still seeing this unhelpful diagnostic message all over the place. |
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 |
Comment by Vu Nhat Minh (JIRA) This seems to be auto-merged into Swift 5. |
orakaro (JIRA User) Thanks! |
I see this still happening in 2024... |
Additional Detail from JIRA
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).
The text was updated successfully, but these errors were encountered: