apple / swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
https://swiftpackageindex.com/apple/swift-nio/documentation
Apache License 2.0
7.85k stars 633 forks source link

Remove all `#if swift(...)`s, all the ones I've seen in SwiftNIO are wrong. Needs to be `#if compiler` #2718

Open weissi opened 1 month ago

weissi commented 1 month ago

#if swift(...) is testing the language version and not the compiler version. We (almost) always want to check the compiler version.

(This ticket should be for all repos in the swift-nio* family)

Consider this program

#if swift(>=5.0)
print("I'm Swift language version 5+")
#endif
#if compiler(>=5.0)
print("I'm Swift compiler version 5+")
#endif
#if swift(>=6.0)
print("I'm Swift language version 6+")
#endif
#if compiler(>=6.0)
print("I'm Swift compiler version 6+")
#endif

output (on a recent Swift 6 preview snapshot)

language version 5 (default):

$ swiftc t6.swift && ./t6
I'm Swift language version 5+
I'm Swift compiler version 5+
I'm Swift compiler version 6+

language version 4:

$ swiftc -swift-version 4 t6.swift && ./t6
I'm Swift compiler version 5+
I'm Swift compiler version 6+

language version 6:

$ swiftc -swift-version 6 t6.swift && ./t6
I'm Swift language version 5+
I'm Swift compiler version 5+
I'm Swift language version 6+
I'm Swift compiler version 6+

Please note how compiler does the right thing but swift does not?

weissi commented 1 month ago

We went through the same during the Swift 4 to Swift 5 transition: https://gist.github.com/weissi/83d491d44ec4c7d7d93097b569339beb

But back then it was a little more difficult because #if compiler didn't exist in Swift 4.{0,1,2}. So we had to use the weird 4.0.150 versions to mean "Swift 4.1 compiler in Swift 4 language mode"