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-4209] Unnecessary message for non-optional to non-optional cast #46792

Open
an0 mannequin opened this issue Mar 9, 2017 · 3 comments
Open

[SR-4209] Unnecessary message for non-optional to non-optional cast #46792

an0 mannequin opened this issue Mar 9, 2017 · 3 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

@an0
Copy link
Mannequin

an0 mannequin commented Mar 9, 2017

Previous ID SR-4209
Radar None
Original Reporter @an0
Type Bug
Environment

Xcode 8.3 beta 4

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

md5: 61e8cb5062e3731d18a888c056d10308

relates to:

  • SR-7906 assigning cast value to implicitly unwrapped Optional causes (bogus?) warning

Issue Description:

// font is of type CFTypeRef, non-optional.
// CTFontCreateWithName's first parameter is of type CFString?, optional. This parameter should be non-optional but it is another unrelated API bug.
font = CTFontCreateWithName(font as! CFString, fontSize, nil)

Compiler gives this warning:

Treating a forced downcast to 'CFString' as optional will never produce 'nil'.

But it's not casted as optional. It is non-optional, only used in an optional context.

@belkadan
Copy link
Contributor

Self-contained example:

import CoreText
func test(name: CFTypeRef, fontSize: CGFloat) {
  var font = name
  font = CTFontCreateWithName(font as! CFString, fontSize, nil)
}

Output:

<stdin>:4:36: warning: treating a forced downcast to 'CFString' as optional will never produce 'nil'
  font = CTFontCreateWithName(font as! CFString, fontSize, nil)
                                   ^
<stdin>:4:38: note: use 'as?' to perform a conditional downcast to 'CFString'
  font = CTFontCreateWithName(font as! CFString, fontSize, nil)
                                     ^
                                     ?
<stdin>:4:31: note: add parentheses around the cast to silence this warning
  font = CTFontCreateWithName(font as! CFString, fontSize, nil)
                              ^
                              (                )

I'm not entirely sure this is wrong. It's not unlikely that in an optional context you'd want to pass through nil if the downcast failed, and the warning tells you how to silence it if that's not what you meant to do.

@an0
Copy link
Mannequin Author

an0 mannequin commented Mar 10, 2017

But "forced downcast will never produce 'nil'" is just the normal and expected behavior of the Swift language. It is a known fact that will never be false. Why should we get a "this fact will never be false" warning?

As to the fix-it suggestions, you will find the 1st one is just wrong if you try it because:

Conditional downcast to CoreFoundation type 'CFString' will always succeed.

@belkadan
Copy link
Contributor

That's true, we don't have a way to do conditional downcasts for CF types right now. We could check for that before emitting the first note.

Warnings aren't always meant to catch mistaken intent. Sometimes they're meant to catch likely typos, especially if they're the sort that could easily be missed in a code review. I'd put this in that category.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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
Projects
None yet
Development

No branches or pull requests

1 participant