swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.34k forks source link

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

Open karwa opened 4 years ago

karwa commented 4 years ago
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:

Issue Description:

Example background:

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 commented 4 years ago

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