Commit Graph

244 Commits

Author SHA1 Message Date
Allan Shortlidge
e390cce662 AST: Fix runtime unavailability for universally unavailable decls.
The logic that computes whether a decl is unavailable at runtime was allowing
`@available` attributes for specific availability domains override
`@available(*, unavailable)`. Universally unavailable decls are always
unavailable, regardless of their other `@available` attributes.
2025-09-03 07:04:25 -07:00
Allan Shortlidge
707c4aa52c AST: Factor out a utility for computing runtime unavailability.
In anticipation of needing to compute runtime unavailability to determine
whether declarations should be printed in swiftinterfaces, factor out the code
that computes runtime unavailability into a shared utility based on
`DeclAvailabilityConstraints`.

NFC.
2025-09-02 23:18:05 -07:00
Allan Shortlidge
bb89d339f0 AST: Allow overloads to be disambiguated by obsoletion version.
Previously, whether a declaration is unavailable because it is obsolete was
determined based solely on the deployment target and not based on contextual
availability. Taking contextual availability into account makes availability
checking more internally consistent and allows library authors to evolve APIs
by obsoleting the previous declaration while introducing a new declaration in the
same version:

```
@available(macOS, obsoleted: 15)
func foo(_ x: Int) { }

@available(macOS, introduced: 15)
func foo(_ x: Int, y: Int = 0) { }

foo(42) // unambiguous, regardless of contextual version of macOS
```

This change primarily accepts more code that wasn't accepted previously, but it
could also be source breaking for some code that was previously allowed to use
obsoleted declarations in contexts that will always run on OS versions where
the declaration is obsolete. That code was clearly taking advantage of an
availabilty loophole, though, and in practice I don't expect it to be common.

Resolves rdar://144647964.
2025-07-29 08:18:35 -07:00
Allan Shortlidge
0fa2ca38dc AST: Simplify and clarify AvailabilityConstraint.
Remove some unnecessary complexity from `AvailabilityConstraint`, rename some
of its members, and add more complete documentation.

NFC.
2025-07-25 15:15:03 -07:00
Allan Shortlidge
634f28176e AST: Move parentDeclForInferredAvailability() to Decl.
NFC.
2025-07-17 18:59:14 -07:00
Allan Shortlidge
d0929b67ce AST: Remove AvailabilityInference::isAvailableAsSPI().
It has been replaced by `Decl::isAvailableAsSPI()`.

NFC.
2025-07-17 14:14:21 -07:00
Allan Shortlidge
d09e117dc0 AST: Canonicalize platform versions for @backDeployed attrs.
Also, diagnose invalid platform versions.

Part of rdar://155558161.
2025-07-14 16:40:16 -07:00
Allan Shortlidge
a4f59f9481 AST: Return @backDeployed attribute along with version from Decl query.
Also, introduce a convenience on `BackDeployedAttr` for getting its associated
`AvailabilityDomain`.

NFC.
2025-07-06 19:44:53 -07:00
Anthony Latsis
dd78cd6dbe ASTBridging: Bridge swift::PlatformKind directly 2025-06-20 16:46:03 +01:00
Allan Shortlidge
e16c638fc3 AST: Warn for non-existent platform versions in @available attributes. 2025-06-10 22:11:05 -07:00
Allan Shortlidge
924eb4898b AST: Fix iOS -> visionOS version remap for @backDeployed attrs.
The version remapping for `@backDeployed` regressed due to a bug introduced by
https://github.com/swiftlang/swift/pull/81922.

Also, fix some visionOS tests that have gotten out of date because we don't
seem to be running them in CI.

Resolves rdar://152542983.
2025-06-05 19:19:07 -07:00
Allan Shortlidge
3914b35623 AST: Fix unused variable warning. 2025-06-03 09:45:25 -07:00
Allan Shortlidge
f4b4dc9889 AST/Sema: Fix remapping of iOS availability in diagnostics for visionOS.
When compiling for visionOS, iOS availability attributes are remapped into the
visionOS availability domain automatically. While the version remapping was
being performed correctly, there was a regression that caused the platform name
to be printed incorrectly in many diagnostics. Whenever an iOS version is
remapped to a visionOS version, availability diagnostics will now present
those versions as visionOS versions instead of iOS versions.

Resolves rdar://146293165.
2025-06-02 17:55:38 -07:00
Arnold Schwaighofer
13ff5abdb8 Introduce @specialized attribute
Implements SE-0460 -- the non-underscored version of @specialized.

It allows to specify "internal" (not abi affecting) specializations.

rdar://150033316
2025-05-23 13:12:47 -07:00
Allan Shortlidge
5cd5f31c9c Sema: Fix a regression in -require-explicit-availability diagnostics.
The changes in https://github.com/swiftlang/swift/pull/80040 caused the
compiler to start diagnosing extensions containing only members that are
either `@_spi`, `@_alwaysEmitIntoClient`, or unavailable when the
`-require-explicit-availability` flag is passed. Extensions should not be
diagnosed when they only contain members that would not be diagnosed
themselves.

Resolves rdar://148275432.
2025-04-02 17:12:38 -07:00
Becca Royal-Gordon
e88b99a6ff Inherit availability in @abi
ABI-only declarations now inherit `@available`, `@backDeployed`, etc. from their ABI counterpart. This will make it unnecessary to specify these attributes in `@abi`. Also some changes to make sure we suggest inserting `@available` in the correct place.

No tests because the enforcement is not yet in.
2025-03-26 10:47:13 -07:00
Allan Shortlidge
592b70f00f AST: Fix AvailabilityContext platform range for certain targets.
https://github.com/swiftlang/swift/pull/79807 caused a regression in which
`AvailabilityContext` stopped tracking the available version range for the
active platform domain for certain platforms. Fix this by reverting to checking
`AvailabilityDomain::isActive()` to determine when a given platform
`AvailabilityDomain` represents the target platform. The compiler's existing
mapping from target triple to platform domain is incomplete and it's not clear
to me whether fixing that could cause other regressions.

Resolves rdar://147413616.
2025-03-20 16:26:54 -07:00
Allan Shortlidge
5e0eb09fc0 AST: Skip codegen for decls that are unavailable in custom domains.
Regardless of the value specified for `-unavailable-decl-optimization`, decls
that are unavailable in custom availability domains should be treated as always
unreachable at runtime.

Part of rdar://138441307.
2025-03-17 22:46:07 -07:00
Allan Shortlidge
7ae3a86d9b AST: Introduce "root" availability domains.
A root availability domain is a direct descendant of the universal availability
domain.
2025-03-17 22:09:28 -07:00
Allan Shortlidge
feb81461b4 AST: Consolidate code invoking DeclRuntimeAvailabilityRequest.
NFC.
2025-03-17 22:09:28 -07:00
Allan Shortlidge
7ed2a0def6 AST: Rename SemanticDeclAvailability to DeclRuntimeAvailability. 2025-03-17 09:13:43 -07:00
Allan Shortlidge
2309793b70 AST: Refactor SemanticDeclAvailabilityRequest.
Generalize the implementation of `SemanticDeclAvailabilityRequest` in
preparation for adding a new case to `SemanticDeclAvailability`. Use the
centralized availability constraint query instead of implementing a bespoke
algorithm for gathering constraints. Simplify `SemanticDeclAvailability` by
removing a case that is no longer relevant.

Part of rdar://138441307.
2025-03-17 09:10:32 -07:00
Allan Shortlidge
fc2b5bebdc AST: Remove Decl::isSemanticallyUnavailable().
It can be replaced by querying the `AvailabilityContext` for a declaration
directly.
2025-03-16 23:33:56 -07:00
Allan Shortlidge
151c9dee39 AST: Consolidate abstract/contrete syntax decl lookup functions. 2025-03-15 23:47:29 -07:00
Allan Shortlidge
ff9b541c57 AST/Sema: Sink isExported() queries from Sema to AST. 2025-03-15 23:47:29 -07:00
Allan Shortlidge
542986032f AST: Unique CustomAvailabilityDomain instances.
Store `CustomAvailabilityDomain` instances in a folding set on `ASTContext`.
This instances of custom domains to be created without needing to cache them in
disparate locations.
2025-03-12 18:30:45 -07:00
Allan Shortlidge
0bf464b272 AST: Return optional AvailabilityRange from SemanticAvailableAttr queries.
Introduction, deprecation, and obsoleteion ranges should only be returned by
the accessors on `SemanticAvailableAttr` when the attribute actually has an
affect on the corresponding kind of availability.
2025-03-06 13:02:19 -08:00
Allan Shortlidge
cad1ee75d5 AST: Optimize the layout of AvailabilityRange.
`AvailabilityRange` is now being used as a currency type in more of the
compiler, and some of those uses are in permanent `ASTContext` allocations. The
class wraps the `VersionRange` utility, which is itself a wrapper around
`llvm::VersionTuple` with some additional storage for representing sentinel
values. Even though the two sentinel values can be be represented with just a
single bit of additional storage on top of the 16 bytes required to represent
`VersionTuple`, because of alignment requirements the sentinel values end up
bloating the layout of `VersionRange` by many bytes.

To make `AvailabilityRange` and `VersionRange` more efficient to store, we can
instead reserve two unlikely `llvm::VersionTuple` bit patterns as the sentinel
values instead. The values chosen are the same ones LLVM uses to represent
version tuple tombstones and empty keys in a `DenseMap`.
2025-03-05 07:15:15 -08:00
Allan Shortlidge
b64df7384e AST: Simplify AvailabilityRange construction.
Introduce a constructor that takes an `llvm::VersionTuple` directly, instead of
needing to spell out `VersionRange::allGTE(<tuple>)` which is unnecessarily
verbose.
2025-03-04 19:41:04 -08:00
Allan Shortlidge
3b9ca2f397 AST: Consolidate availability version remapping.
Introduce `SemanticAvailableAttr` conveniences to compute the deprecated and
obsoleted ranges for an attribute and ensure they remap versions when needed.
2025-03-01 20:23:11 -08:00
Allan Shortlidge
a0010cbf05 Merge pull request #79718 from tshortli/multiple-unavailable-domains
AST: Allow multiple unavailable domains in `AvailabilityContext`
2025-03-01 09:24:11 -08:00
Allan Shortlidge
b02d44352e AST: Introduce StableAvailabilityDomainComparator. 2025-02-28 19:09:33 -08:00
Allan Shortlidge
c89df2fc38 AST/Sema: Adopt AvailabilityDomain arguments in more diagnostics.
Update explicit unavailability and deprecation diagnostics to take
`AvailabiiltyDomain` instead of a platform string.
2025-02-28 13:57:00 -08:00
Allan Shortlidge
272b2b43de AST: Accept AvailabilityDomains as diagnostic arguments.
This simplifies the code to emit availabilty diagnostics and ensures that they
display domain names consistently. While updating existing diagnostics, improve
consistency along other dimensions as well.
2025-02-28 09:18:38 -08:00
Allan Shortlidge
aaa0e0a7b3 AST/Parse: Parse custom availability domain specs in if #available(...).
Delay resolution of availability domain identifiers parsed in availability
specifications until type-checking. This allows custom domain specifications to
be written in `if #available` queries.
2025-02-27 23:07:55 -08:00
Allan Shortlidge
62c8e72f23 AST: Centralize AvailabilityDomain lookup.
Implement lookup of availability domains for identifiers on
`AvailabilityDomainOrIdentifier`. Add a bit to that type which represents
whether or not lookup has already been attempted. This allows both
`AvailableAttr` and `AvailabilitySpec` to share a common implementation of
domain lookup.
2025-02-25 22:00:31 -08:00
Allan Shortlidge
8ebf2aa7e6 AST: Add printing utilities for AvailabilityDomain and AvailabilitySpec. 2025-02-25 19:45:39 -08:00
Allan Shortlidge
b2c57f8119 AST: Canonicalize AvailableAttr versions on-demand.
Translate macOS 10.16 to macOS 11 on-demand when calling
`SemanticAvailableAttr` version accessors. This removes the need for
`AvailableAttr` to have mutable version members that
`SemanticAvailableAttrRequest` can update.
2025-02-22 09:40:24 -08:00
Allan Shortlidge
f4f9d85503 AST/Sema: Sink SemanticAvailableAttrRequest from Sema into AST.
Unblocks some work on the `@abi` attribute.
2025-02-20 10:29:38 -08:00
Allan Shortlidge
3c8a57f86d AST: Use consolidated availability constraint query for diagnostics.
Switch to calling `swift::getAvailabilityConstraintsForDecl()` to get the
unsatisfied availability constraints that should be diagnosed.

This was intended to be NFC, but it turns out it fixed a bug in the recently
introduced objc_implementation_direct_to_storage.swift test. In the test,
the stored properties are as unavailable as the context that is accessing them
so the accesses should not be diagnosed. However, this test demonstrates a
bigger issue with `@objc @implementation`, which is that it allows the
implementations of Obj-C interfaces to be less available than the interface,
which effectively provides an availability checking loophole that can be used
to invoke unavailable code.
2025-02-16 07:44:45 -08:00
Allan Shortlidge
670bfc0499 AST: Remove unused ignoreAppExtensions parameter.
It's no longer used after the rewrite of the algorithm for determining if a
declaration is unreachable at runtime.
2025-02-09 11:20:55 -08:00
Allan Shortlidge
16de339051 AST: Enable -unavailable-code-optimization for zippered libraries.
Correct the determination of whether a declaration is unreachable at runtime
when compiling a zippered library.

Resolves rdar://125930716.
2025-02-09 11:20:48 -08:00
Allan Shortlidge
b9e1cbd3ac AST: Enable -unavailable-decl-optimization on visionOS.
Using availability domains, reimplement the algorithm that determines whether a
declaration is unavailable at runtime. The new algorithm takes ABI compatible
platforms into account, ensuring that declarations that are available on iOS do
not get treated as unreachable at runtime when compiling for visionOS.

Resolves rdar://116742214.
2025-02-09 11:19:08 -08:00
Allan Shortlidge
cedb4d98de AST: Move AvailabilityConstraint implementation to a separate file. 2025-02-08 22:47:01 -08:00
Allan Shortlidge
aed1a013e4 Sema: Loosen more available than enclosing extension diagnostic.
When diagnosing a declaration that is more available than its context, to
preserve source compatibility we need to downgrade the diagnostic to a warning
when the outermost declaration is an extension. This logic regressed with
https://github.com/swiftlang/swift/pull/77950 and my earlier attempt to fix
this (https://github.com/swiftlang/swift/pull/78832) misidentified what had
regressed.

Really resolves rdar://143423070.
2025-02-03 14:07:27 -08:00
Allan Shortlidge
f9799aeba9 AST: Introduce a Custom availability domain kind. 2025-01-31 17:31:07 -08:00
Allan Shortlidge
4b773a45ca AST: Move AvailabilityDomain ordering to Availabilty.cpp.
Availability attribute inferrence is currently the only thing that depends on
having a total ordering on AvailabilityDomains.
2025-01-31 17:31:07 -08:00
Allan Shortlidge
7b8cbd7109 AST: Store unresolved domain strings in AvailableAttr.
This is the first step towards resolving the AvailabilityDomain associated with
an AvailableAttr during type checking instead of parsing.
2025-01-26 13:50:56 -08:00
Allan Shortlidge
a6eb7e494b AST: Fix weak linking for potentially unavailable accessors in extensions.
In https://github.com/swiftlang/swift/pull/78454 queries for the platform
availability of decl were consolidated into
`Decl::getAvailableAttrForPlatformIntroduction()`. In addition to checking the
attributes directly attached to the decl, this method also checks whether the
decl is a member directly contained inside of an extension and checks for
attributes attached to the extension as well. Previously, this logic was only
used for availability checking diagnostics, where special casing extension
members was a requirement. As a result of the consolidation, though, the logic
is now also shared by the query that determines whether to weakly link symbols
associated with a decl. That determination already had its own way of handling
members of extensions but it seemed like consolidating the logic would stil be
a net improvement that would reduce overall complexity.

Unfortunately, the existing approach to getting the availability of the
enclosing extension had a subtle bug for both AccessorDecl and OpaqueTypeDecl.
If an AvailableAttr was not directly attached to the immediate decl, then
`Decl::getAvailableAttrForPlatformIntroduction()` would check if the enclosing
decl context was an extension and look at its attributes as well. For
AccessorDecl and OpaqueTypeDecl, checking the enclosing decl context would
accidentally skip over the VarDecl and AbstractFunctionDecl that are formally
the parents of those decls for the purposes of attribute inheritance. As a
result, the availability of the enclosing property or function could be ignored
if the enclosing extension had explicit availability attributes.

The fix is to use `AvailabilityInference::parentDeclForInferredAvailability()`
instead of `getDeclContext()` when looking for the immediately enclosing
extension.

Resolves rdar://143139472.
2025-01-24 17:43:42 -08:00
John McCall
95dca81d24 Merge pull request #78729 from rjmccall/isolated-sending-results-compiler-fix
Assorted fixes for runtime metadata mangling and demangling
2025-01-23 21:33:50 -05:00