-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Sema] Define availability specification macros with a frontend flag #33439
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
Conversation
|
What about if you could reference the availability of another declaration, as in #29559 ? Then you could set that based on normal conditionals, including standard I feel that that approach leads to more self-documenting code than literally passing in an availability string as a compiler argument. |
|
I see this and referencing availability as two different concerns. The compiler flag aspect of this PR is designed to compile the same source code to different availability versions, whereas #29559 still rely on availability defined in the sources. This feature allows frameworks developers to align many APIs for the same release and to backport the framework. That being said, with this solution the availability macros remain in the hand of the framework developers and are not exposed to the framework clients who will only see the classic availability specifications. So the framework developers can use the macro to fit their need and it shouldn't prevent further improvements to the availability attributes. |
|
What I mean is that you can achieve the same thing by writing: #if DARWIN_STATIC_STDLIB // or whatever
// @available(*)
typealias MinimumSupportedDarwin = Void
#else
@available(macOS 11, iOS ..., *)
typealias MinimumSupportedDarwin = Void
#endifAnd then using availability refs. This allows you to retarget the same sources to be compiled without availability (or with different availability), but it keeps the information passed through the command-line to just switches. The meaningful information is still kept as Swift source code (which is good, because debugging problems with this is likely to need source changes, and hopefully this allows for less digging through shell scripts to decipher where availability strings come from). |
|
@swift-ci Please smoke test |
Introduce availability macros defined by a frontend flag. This feature makes it possible to set the availability versions at the moment of compilation instead of having it hard coded in the sources. It can be used by projects with a need to change the availability depending on the compilation context while using the same sources. The availability macro is defined with the `-define-availability` flag: swift MyLib.swift -define-availability "_iOS8Aligned:macOS 10.10, iOS 8.0" .. The macro can be used in code instead of a platform name and version: @available(_iOS8Aligned, *) public func foo() {} rdar://problem/65612624
Availability macros can’t be used in inlinable code as inlinable is copied textually in the generated swiftinterface files. Further would could lift this limitation.
|
@swift-ci Please smoke test |
|
Moving out of draft state as it should now be ready for review. I've updated the description to be up to date with the current feature design and limitations. It sounds like the best approach here is to keep this feature simple as it would be mostly useful to framework developers with a specific need and it won’t address all use cases. The in-language alternative could be more useful to framework clients and address other Framework developers use-cases. For this reason, I redesigned the feature to be as little obtrusive as possible. It should affect the behavior of the compiler only when an availability macro is defined in the frontend flags. The implementation could be extended to associate identifiers in availability attributes to other sources than macros passed as arguments. |
|
@swift-ci Please smoke test Linux platform |
Introduce availability macros defined by a frontend flag. This feature makes it possible to set the availability versions at the moment of compilation instead of having it hard coded in the sources. It can be used by projects with a need to change the availability depending on the compilation context while using the same sources.
The availability macro is defined with the
-define-availabilityflag:swift-frontend MyLib.swift -define-availability "_iOS8Aligned:macOS 10.10, iOS 8.0” -define-availability "_myProject 1.0:macOS 10.12, iOS 10.0" ...The macro can be used in code instead of a platform name and version:
The use of availability macros in the
@availablespecification is expanded at parsing and should not be visible in any compiler outputs. This includes the swiftinterfaces which will show the full classic availability specification. Thefoofunction in the previous will be printed in the swiftinterfaces as:Verion numbers, or triples, are optional on macros. A macro without an explicit version defaults to the version 0.
Availability macros can’t be used in inlinable code as it is copied textually in the generated swiftinterface files. Further work would could lift this limitation.
rdar://problem/65612624