swiftlang / swift

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

hasFeature() by itself doesn't prevent parsing of new syntax #64627

Open gwynne opened 1 year ago

gwynne commented 1 year ago

The following code produces an error in Swift 5.8 and swift-DEVELOPMENT-SNAPSHOT-2023-03-23-a:

#if compiler(>=5.8)
#if hasFeature(VariadicGenerics)
func f<each T>(_: repeat each T) {}
#else
// Code for supporting older compilers
#endif
#else
// Repeat the code for supporting older compilers
#endif
$ swift test.swift
test.swift:3:13: error: expected '>' to complete generic parameter list
func f<each T>(_: repeat each T) {}
            ^

To support compilers older than 5.8, one must write:

#if compiler(>=5.8)
#if compiler(>=5.8) && hasFeature(VariadicGenerics)
func f<each T>(_: repeat each T) {}
#else
// Code for supporting older compilers
#endif
#else
// Repeat the code for supporting older compilers
#endif

This is awkward and requires inline comments to avoid confusion. It's already ugly enough to have to repeat the else branch when supporting 5.7 and earlier.

xwu commented 1 year ago

Yes, this is a limitation inherent to the fact that hasFeature is itself a new feature; we can't go back in time and ship hasFeature in past releases.

gwynne commented 1 year ago

Yes, but a compiler that does understand it (i.e. 5.8 or later) should apply the "don't parse new syntax" behavior without requiring the compiler directive as part of the same control statement.