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-11864] Availability warnings in dead code paths #54278

Open
karwa opened this issue Nov 27, 2019 · 1 comment
Open

[SR-11864] Availability warnings in dead code paths #54278

karwa opened this issue Nov 27, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@karwa
Copy link
Contributor

karwa commented Nov 27, 2019

Previous ID SR-11864
Radar rdar://problem/57508973
Original Reporter @karwa
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: aa265d1621de94b205e34a88cfbf1b9e

relates to:

  • SR-11647 Swift 5.1 on Linux: Regression in OperationQueue concurrency

Issue Description:

Example background:

  • CGColor.init(red:green:blue:alpha🙂 is not available on tvOS

  • CGColor.init(srgbRed:green:blue:alpha) is `@available(tvOS 13, *)`

Example:

import CoreGraphics
 public extension Color {
     @available(tvOS 13, watchOS 13, *)
     var cgColor : CGColor {
       if #available(OSX 10.15, iOS 13, *) {
         return CGColor(srgbRed: CGFloat(r),
                        green: CGFloat(g),
                        blue: CGFloat(b),
                        alpha: CGFloat(a))
       } else {
         // ERROR: CGColor.init(red:...) is not available on tvOS.
         return CGColor(red: CGFloat(r),
                        green: CGFloat(g),
                        blue: CGFloat(b),
                        alpha: CGFloat(a)) 
       }
     }
  }
}

Compiling for the tvOS simulator, Xcode 11.2.1 (11B500)

The compiler complains about the unavailable initialiser in the "else" branch of the OSX/iOS conditional and won't compile this code. Despite the fact that the #available condition will always be true for tvOS, so it will never hit that "else" branch.

There is a workaround, which is to wrap the initialiser (still in the "else" branch) with:

#if !os(tvOS)
return CGColor(...)
#endif

Which is also strange, because now we're not returning anything on the "else" branch, but the compiler seems satisfied that it's dead and doesn't complain. If it knows that, it could also allow the use of the unavailable initialiser.

@typesanitizer
Copy link

The compiler complains about the unavailable initialiser in the "else" branch of the OSX/iOS conditional and won't compile this code. Despite the fact that the #available condition will always be true for tvOS, so it will never hit that "else" branch.

The program should type-check regardless of whether some conditions are statically known to be true or false. So this behavior is ok IMO.

There is a workaround, which is to wrap the initialiser (still in the "else" branch) with: [..]
which is also strange, because now we're not returning anything on the "else" branch, but the compiler seems satisfied that it's dead and doesn't complain.

Hmm, that shouldn't be happening if you compile for tvOS. To me, this seems like the buggy behavior.

@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.
Projects
None yet
Development

No branches or pull requests

2 participants