Commit Graph

43 Commits

Author SHA1 Message Date
Allan Shortlidge aed3492d93 Merge pull request #88765 from swiftlang/ks/respect-availability-for-obsoleted-api
Respect availability for obsoleted API
2026-05-28 07:42:11 -07:00
Keith Smiley 9ea12da9d3 Remove Swift available handling 2026-05-27 15:52:40 -07:00
Keith Smiley 2158e866d7 Revert "Only check active platforms"
This reverts commit fa02b97dd2.
2026-05-27 11:45:28 -07:00
Keith Smiley fa02b97dd2 Only check active platforms 2026-05-27 10:42:09 -07:00
Keith Smiley 2f58b30205 Respect availability for obsoleted API
If an obsoleted API is only called behind an availability check that is
never hit because it is unreachable for the current deployment target /
Swift version, it no longer errors.

Fixes https://github.com/swiftlang/swift/issues/88763
2026-05-27 10:41:39 -07:00
Allan Shortlidge c6713336cd Sema: Suggest anyAppleOS availability when appropriate in fix-its.
When emitting fix-its that would make an unavailable declaration available, use
`anyAppleOS` in the fix-it instead of the target platform if the unmet
availability constraint is in the `anyAppleOS` domain.

Resolves rdar://174558882.
2026-05-26 21:50:32 -07:00
Allan Shortlidge 7d2555f5ad AST: Fix getAvailabilityConstraintsForDecl() for members of extensions.
Fix a Swift 6.3 regression in which extension members that are obsolete in the
current Swift language version are still considered available in contexts that
are also obsolete in the current Swift language version.

When computing the AvailabilityConstraints for a member of an extension,
constraints are first gathered for the member and then again for the extension
in case there are some attributes on the extension that should be effectively
inherited by the member. After gathering constraints, the core implementation
of `getAvailabilityConstraintsForDecl()` removes any constraints that can be
ignored in the given `AvailabilityContext`. That determination depends on the
kind of declaration the constraints are for, though, and it was being performed
first for the member on its own constraints and then again for the extension
for the combination of their constraints. This could result in member
constraints being ignored incorrectly.

The fix is to check whether a constraint ought to be ignored before adding it
to the set in the first place, rather than post-processing the entire
collection of constraints.

Resolves rdar://165942115.
2026-05-08 12:37:38 -07:00
Allan Shortlidge 8dee947aa3 AST: Relax availability checking for decls in unavailable contexts.
Loosen availability checking to allow references to a decl that is unavailable
in a broader platform context in contexts which are unavailable in a more
specific platform context. For example, `@available(macOS, unavailable)` decls
should be allowed in `@available(macOSApplicationExtension, unavailable)`
contexts. This enhancement, which has always been desirable but wasn't high
priority, became more important with the introduction of `anyAppleOS`. Some
library authors may replace platform-specific availability annotations with
`anyAppleOS` availability and without this behavior change those attribute
updates would be source breaking.

Test updates assisted by claude.
2026-03-16 14:49:09 -07:00
Allan Shortlidge 98869a9d12 AST: Remove * as the bottom AvailabilityDomain.
Modeling the universal AvailabilityDomain as the bottom element of a lattice
containing all domains was useful for checking unavailability, but it makes
some other algorithms harder to write elegantly using availability domain
algebra. Instead, special case `*` in unavailability computations and only form
an availability domain lattice with the ABI stable platform domains,
`anyAppleOS`, and `Swift`.
2026-03-15 11:19:44 -07:00
Kavon Farvardin 3bf9cb5cd7 Availability: handle reparented protocols
Since reparentable protocols can have less availability
than the protocols inheriting from it, we need to first
loosen availability checking inheritance clauses to allow
for the statement of retroactive refinement.

Then, we need to tighten it in other places in terms of
expression checking, because we now cannot refer to
members of a generic type that originate from an unavailable
ancestor of a protocol to which the value conforms. Previously
it was not possible for that to be the case, so no checking
was performed.
2026-02-15 19:43:31 -08:00
Allan Shortlidge 463bf91f68 AST: Rename Swift runtime availability domain to StandaloneSwiftRuntime.
This name will distinguish the standalone Swift runtime availability domain
from the domain for the built-in Swift runtime on supported targets.
2025-10-21 21:55:25 -07:00
Allan Shortlidge 1a86cd9c26 AST: Introduce a Swift runtime availability domain.
Add support for the `Swift` availability domain, which represents availability
with respect to the Swift runtime. Use of this domain is restricted by the
experimental feature `SwiftRuntimeAvailability`.
2025-10-08 17:31:57 -07:00
Allan Shortlidge 4e9a883824 AST: Rename SwiftLanguage availability domain kind to SwiftLanguageMode. 2025-10-08 15:46:34 -07:00
Allan Shortlidge 0647da5416 AST: Introduce an "always enabled" custom availability domain kind.
An always enabled availability domain is implicitly available in all contexts,
so uses of declarations that are marked as `@available` in the domain are never
rejected. This is useful for an availability domain representing a feature flag
that has become permanently enabled.

Partially resolves rdar://157593409.
2025-09-11 14:39:05 -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 5ae0ad6a8f AST: Allow obsoletion in Swift version to disambiguate overloads consistently.
It must be possible to disambiguate overloads using the module-wide Swift
language version, even in contexts that are themselves obsolete in the current
Swift language version.

Resolves rdar://158620835.
2025-08-18 17:20:42 -07:00
Allan Shortlidge 93902835d0 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-08-13 11:26:07 -07:00
Allan Shortlidge 600e971b40 AST: Revert disambiguation of overloads by obsoletion version.
It caused a regression when building the CloudKit module from its interface in
the Xcode 26 SDKs.

This effectively reverts https://github.com/swiftlang/swift/pull/83384 (though
a few parts of that PR that did not cause any regressions have been kept).

Resolves rdar://157342004.
2025-08-11 21:50:40 -07:00
Allan Shortlidge 4d41db3b5d Sema: Rationalize availability checking in unavailable contexts.
Correct several behaviors of availability checking in unavailable contexts that
were inconsistent with the checking model:

- Avoid diagnosing unintroduced and obsolted declarations in contexts that are
  unavailable in the same domain.
- Diagnose unavailability normally in type signature contexts.
2025-08-08 07:57:44 -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 8cb20c3f4e AST: Fix a crash when handling duplicate available attrs. 2025-07-25 15:14:53 -07:00
Allan Shortlidge aeb5a46d9e AST: Emit warnings for redundant availability checks in custom domains. 2025-07-18 13:27:08 -07:00
Allan Shortlidge 6b29700554 Merge pull request #83162 from tshortli/misc-availability-cleanups
Miscellaneous availability cleanups
2025-07-18 12:03:46 -07:00
Allan Shortlidge 634f28176e AST: Move parentDeclForInferredAvailability() to Decl.
NFC.
2025-07-17 18:59:14 -07:00
Allan Shortlidge 4a7e76775a AST: Add llvm::raw_ostream conveniences for availability constructs.
Make it easier to debug availability by printing to output streams like
`llvm::errs()`.

NFC.
2025-07-17 18:59:01 -07:00
Allan Shortlidge 99380bf00f Sema: Sometimes allow universally unavailable declarations to be referenced.
In implicit contexts that are universally unavailable, allow writable key paths
to be formed to properties with setters that are also marked as universally
unavailable. This fixes a regression from the previous commit where the code
synthesized for `@Observable` properties in universally unavailable classes was
rejected by the availability checker.
2025-07-15 14:08:57 -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
Allan Shortlidge ae21f8d390 AST: Stop diagnosing potentially unavailable declarations in unavailable contexts.
Potential unavailability of a declaration has always been diagnosed in contexts
that do not have a sufficient platform introduction constraint, even when those
contexts are also unavailable on the target platform. This behavior is overly
strict, since the potential unavailability will never matter, but it's a
longstanding quirk of availability checking. As a result, some source code has
been written to work around this quirk by marking declarations as
simultaneously unavailable and introduced for a given platform:

```
@available(macOS, unavailable, introduced: 15)
func unavailableAndIntroducedInMacOS15() {
  // ... allowed to call functions introduced in macOS 15.
}
```

When availability checking was refactored to be based on a constraint engine in
https://github.com/swiftlang/swift/pull/79260, the compiler started effectively
treating `@available(macOS, unavailable, introduced: 15)` as just
`@available(macOS, unavailable)` because the introduction constraint was
treated as lower priority and therefore superseded by the unavailability
constraint. This caused a regression for the code that was written to work
around the availability checker's strictness.

We could try to match the behavior from previous releases, but it's actually
tricky to match the behavior well enough in the new availability checking
architecture to fully fix source compatibility. Consequently, it seems like the
best fix is actually to address this long standing issue and stop diagnosing
potential unavailability in unavailable contexts. The main risk of this
approach is source compatibility for regions of unavailable code. It's
theoretically possible that restricting available declarations by introduction
version in unavailable contexts is important to prevent ambiguities during
overload resolution in some codebases. If we find that is a problem that is too
prevalent, we may have to take a different approach.

Resolves rdar://147945883.
2025-04-11 11:50:29 -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 151c9dee39 AST: Consolidate abstract/contrete syntax decl lookup functions. 2025-03-15 23:47:29 -07:00
Allan Shortlidge 0462cfda11 AST: Teach AvailabilityContext to represent version-less availability.
This enables potential unavailability diagnostics to be emitted for decls that
are only available in version-less domains.
2025-03-06 22:29:50 -08: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 cb778b7ce4 AST/Sema: Expand potential unavailability diagnostics to arbitrary domains.
When emitting potential unavailability diagnostics, don't assume that they can
only apply to the target platform `AvailabilityDomain`.
2025-03-04 19:41:04 -08:00
Allan Shortlidge dc2fe53089 AST: Rename a couple of AvailabilityConstraint::Reason cases.
Choose names that don't imply availability is versioned, since custom
availability will support domains that are version-less (they are simply
available or unavailable).
2025-03-02 15:08:37 -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 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 f677490704 AST: Fix a regression in constraining an AvailabilityContext for a decl.
When building up AvailabilityContexts, we assume that all of the enclosing
decls have already been accounted for in the AvailabilityContext that we are
constraining. Therefore, it doesn't make sense to merge availability
constraints from the enclosing extension of the target decl.
2025-02-16 07:44:45 -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 a6b20b3fe5 AST: Re-introduce AvailabilityConstraint::Kind.
Now the Kind enum classifies availability constraints based on their
satisfiablility.

NFC.
2025-02-12 07:20:14 -08:00
Allan Shortlidge e87f80825d AST: Rename AvailabilityConstraint::Kind to Reason.
NFC.
2025-02-12 07:20:14 -08:00
Allan Shortlidge e47638940e AST: Introduce swift::getAvailabilityConstraintsForDecl().
This new query is designed to become the canonical source of information
regarding whether a declaration is available to use in a given
`AvailabilityContext`. It should be adopted as the foundational building block
for all other queries that answer more specific questions about the
availability of a specific delcaration.

The implementation of this query has been copied from a variety of sources
which should eventually be deleted once the new query has been fully adopted.

NFC.
2025-02-08 22:47:01 -08:00
Allan Shortlidge cedb4d98de AST: Move AvailabilityConstraint implementation to a separate file. 2025-02-08 22:47:01 -08:00