Files
swift-mirror/test/Parse/ConditionalCompilation/compiler_version.swift
Becca Royal-Gordon 3843c7cd5e Update SWIFT_COMPILER_VERSION language features
The `SWIFT_COMPILER_VERSION` define is used to stamp a vendor’s version number into a Swift compiler binary. It can be queried from Swift code using `#if _compiler_version` and from Clang by using a preprocessor definition called `__SWIFT_COMPILER_VERSION`. These are unsupported compiler-internal features used primarily by Apple Swift.

In Swift 1.0 through 5.5, Apple Swift used a scheme for `SWIFT_COMPILER_VERSION` where the major version matched the embedded clang (e.g. 1300 for Apple Clang 13.0.0) and the minor version was ignored. Starting in Swift 5.6, Apple Swift started using major and minor version numbers that matched the Swift.org version number. This makes them easier to understand, but it means that version 1300.0.x was followed by version 5.6.x. Not only did version numbers go backwards, but also the old logic to ignore minor versions was now a liability, because it meant you would not be able to target a change to 5.7.x compilers but not 5.6.x compilers.

This commit addresses the problem by:

* Modifying the existing `#if _compiler_version(string-literal)` feature so it transforms the major version into a major and minor that will compare correctly to new version numbers. For instance, “1300.*” is transformed into “1.300”, which will compare correctly to a “5.6” or “5.7” version even if it doesn’t really capture the fact that “1300” was a Swift 5.5 compiler. As a bonus, this allows you to use the feature to backwards-compatibly test new compilers using the existing feature: “5007.*” will be seen by compilers before 5.7 as an unknown future version, but will be seen by 5.7 compilers as targeting them.

* Modifying the `__SWIFT_COMPILER_VERSION` clang define similarly so that, to preprocessor conditions written for the old scheme, a 5.7 compiler will appear to have major version 5007.

* Adding a new variant of `#if _compiler_version` with the same syntax as `#if swift` and `#if compiler`—that is, taking a comparison operator and a bare set of dotted version numbers, rather than a string literal. Going forward, this will be how version checks are written once compatibility with compilers before this change is no longer a concern.

These changes are only lightly tested because tests have to work without any compiler version defined (the default in most configurations), but I’ve tested what I can.

Fixes rdar://89841295.
2022-04-27 18:27:52 -07:00

82 lines
2.3 KiB
Swift

// RUN: %target-typecheck-verify-swift
#if _compiler_version("999.*.999.999.999")
let w = 1
#else
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif
#if _compiler_version("600.*.10.10")
#if os(iOS)
let z = 1
#else
let z = 1
#endif
#else
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#if os(iOS)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#else
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif
#if !_compiler_version("777.*.7")
// This shouldn't emit any diagnostics.
%#^*&
#endif
#if _compiler_version("700a.*.10") // expected-error {{version component contains non-numeric characters}}
#endif
#if _compiler_version("...") // expected-error {{found empty version component}}
// expected-error@-1 {{found empty version component}}
// expected-error@-2 {{found empty version component}}
#endif
#if _compiler_version("") // expected-error {{version requirement is empty}}
let y = 1
#else
let thisWillStillParseBecauseConfigIsError = 1
#endif
#if _compiler_version("700.0.100") // expected-warning {{the second version component is not used for comparison in legacy compiler versions}} {{28-29=*}}
#endif
#if _compiler_version("5.7.100") // expected-warning {{the second version component is not used for comparison in legacy compiler versions; are you trying to encode a new Swift compiler version for compatibility with legacy compilers?}} {{24-27=5007.*}}
#endif
#if _compiler_version("700.*.1.1.1.1") // expected-error {{version must not have more than five components}}
#endif
#if _compiler_version("9223372.*.1.1.1") // expected-error {{version component out of range: must be in [0, 9223371]}}
#endif
#if _compiler_version("700.*.1000.1.1") // expected-error {{version component out of range: must be in [0, 999]}}
#endif
#if _compiler_version("700.*.1.1000.1") // expected-error {{version component out of range: must be in [0, 999]}}
#endif
#if _compiler_version("700.*.1.1.1000") // expected-error {{version component out of range: must be in [0, 999]}}
#endif
// New style _compiler_version()
#if _compiler_version(<4.0)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif
#if !_compiler_version(>=4.3.2.1.0)
// This shouldn't emit any diagnostics.
asdf asdf asdf asdf
#endif