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-14112] errors around casting optional CFString to String are confusing/cyclical #56498

Open
swift-ci opened this issue Jan 26, 2021 · 1 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

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14112
Radar rdar://problem/73742482
Original Reporter Rey (JIRA User)
Type Bug
Environment

MacOS 11.1 (20C69) , XCode 12.3 (12C33), building for MacOS 11 and up

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

md5: 89faac67204a3b8841879f98f4cb36f0

Issue Description:

I am attempting to use CTFont code, and wrote this code:

private func localizedNameForFontFamily(_ family: String) -> String
 {
 let font = CTFontCreateWithName(family as CFString, 0.0, nil) return CTFontCopyLocalizedName(font, kCTFontFamilyNameKey, nil) ?? family 
 }

which spawned this error: "CFString is not impicitly convertable to String, did you mean to use as to explicitly convert"

Following this advice gives us this code:

private func localizedNameForFontFamily(_ family: String) -> String
{
    let font = CTFontCreateWithName(family as CFString, 0.0, nil) return CTFontCopyLocalizedName(font, kCTFontFamilyNameKey, nil) as String ?? family 
} 

which then prompted the error "CFString? is not convertable to String, did you mean to use as! to force downcast?" which seems incorrect, since the actual issue is that we need to cast to `String?`, (adding the optional).

additionally, if you go with that suggestion again:

private func localizedNameForFontFamily(_ family: String) -> String
{
    let font = CTFontCreateWithName(family as CFString, 0.0, nil) return CTFontCopyLocalizedName(font, kCTFontFamilyNameKey, nil) as! String ?? family 
}

you get the warning "Forced cast from `CFString?` to `String` only unwraps and bridges, did you mean to use ! with as?"

which is particularly frustrating as it seems to be suggesting that we return to the code that was originally written!

the successfully compiling code here is:

private func localizedNameForFontFamily(_ family: String) -> String
{
    let font = CTFontCreateWithName(family as CFString, 0.0, nil) return CTFontCopyLocalizedName(font, kCTFontFamilyNameKey, nil) as String? ?? family 
} 

I would expect that at some point in this cycle the suggestion would be to cast to an optional String, rather than adjusting the type of casting that is happening.

@typesanitizer
Copy link

@swift-ci create

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants