Commit Graph

73 Commits

Author SHA1 Message Date
Allan Shortlidge fb7c8816a5 AST: Fix availability scopes for if #unavailable else branches.
When an `if #unavalable` statement also includes a secondary (non-availability)
condition, the availability scope for the else branch should not be refined.
The else branch can be reached because the secondary condition failed, in which
case the availability condition may not hold.

Resolves rdar://165863221.
2026-05-14 16:41:36 -07:00
Allan Shortlidge 801c73e990 test: Refactor platform-specific availability scope tests into a separate file. 2026-05-14 16:28:15 -07:00
Allan Shortlidge d937d43137 Merge pull request #88942 from tshortli/extension-member-availability
AST: Fix `getAvailabilityConstraintsForDecl()` for members of extensions
2026-05-12 11:16:04 -07:00
Allan Shortlidge 18e4fd76b5 Merge pull request #88886 from tshortli/improve-use-any-apple-os-diags
Sema: Improve `UseAnyAppleOSAvailability` diagnostics
2026-05-08 14:41:40 -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 8adfcd2ab0 tests: Improve availability_swift_language_mode.swift.
- Use -verify instead of FileCheck.
- Use the Swift 5 and Swift 6 language modes so the test doesn't need to be
  updated for Swift 4 deprecation.
- Add test cases for overload selection in contexts that are obsoleted in the
  current Swift language mode.
- Add a test case for members of extensions.
2026-05-08 12:37:38 -07:00
Allan Shortlidge 4023eaa3ad SILGen: Skip unreachable witness thunks consistently.
When a protocol requirement is unreachable due to a custom availability domain
constraint, emission of the thunk and witness table entry for the requirement
should be skipped. The previous logic did not handle accessors or constructors
and it used a bespoke query instead of `Decl::isAvailableDuringLowering()`.

Resolves rdar://174172039.
2026-05-07 20:49:19 -07:00
Allan Shortlidge 2edd0ed10b Sema: Offer UseAnyAppleOSAvailability fix-its for more attribute combinations.
Expand the fix-its to cover declarations with availability that is split
accross multiple attributes instead listed together in a single short form
`@available` attribute.

Resolves rdar://175677817.
2026-05-06 11:25:55 -07:00
Allan Shortlidge 2038f6b837 Sema: Avoid diagnosing implict decls with UseAnyAppleOSAvailability.
Part of rdar://175677817.
2026-05-06 09:38:27 -07:00
Allan Shortlidge 6d50016b9c Sema: Avoid diagnosing @_spi_available attrs with UseAnyAppleOSAvailability.
Part of rdar://175677817.
2026-05-06 09:38:26 -07:00
Allan Shortlidge a8e7b1a887 Sema: Correct source locs for UseAnyAppleOSAvailability fix-its.
The previously emitted fix-its would only replace the last availability spec
instead of all of the specs. I didn't notice this until I tried it in an
editor.

Resolves rdar://173814157.
2026-04-01 14:49:31 -07:00
Allan Shortlidge 31d9759976 Merge pull request #88141 from tshortli/suggest-any-apple-os-for-just-two-platforms
Sema: Suggest anyAppleOS on decls with availability on two platforms
2026-03-27 09:43:14 -07:00
Allan Shortlidge e72805e1b3 AST: Skip useless availability diags in fragile funcs with opaque result types.
Removing an availability check in these functions could change the ABI of the
function and result in a miscompilation.

Resolves rdar://127829648.
2026-03-26 15:18:13 -07:00
Allan Shortlidge 68ce3026b4 Sema: Suggest anyAppleOS on decls with availability on two platforms.
Relax `-Wwarning UseAnyAppleOSAvailability` so that it triggers on any
declaration with matching availability on more than one Apple platform - don't
require availability annotations to be present for all of macOS, iOS, watchOS,
and tvOS.

Resolves rdar://173437121.
2026-03-26 14:28:10 -07:00
Allan Shortlidge 1f26971a88 Sema: Introduce an opt-in UseAnyAppleOSAvailability diagnostic group.
Enabling `UseAnyAppleOSAvailability` diagnostics will cause the compiler to
diagnose declarations where `anyAppleOS` availability can be used a concise
replacement for platform-specific availability annotations.

Resolves rdar://163819878.
2026-03-20 16:01:55 -07:00
elsa a5cdb72bbc Merge pull request #87829 from elsakeirouz/custom-error-for-non-conformance-to-borrowingseq
Custom error for unavailable conformance to a sequence
2026-03-18 15:18:00 +00:00
Elsa Keirouz 0c2fa1f2c2 DesugarForEachStmt: custom error for unavailable sequence conformance 2026-03-17 11:27:21 +00: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
Elsa Keirouz 09778e1af7 add Borrowing for loop tests 2026-03-09 12:40:01 +00:00
Allan Shortlidge 5e094d03a3 Merge pull request #87522 from tshortli/embedded-concurrency-availability 2026-03-03 05:46:36 -08:00
Allan Shortlidge 9724348f86 AST: Allow anyAppleOS availability without an experimental feature.
Resolves rdar://170988964.
2026-03-02 14:05:39 -08:00
Allan Shortlidge c7ce5a8aed Parse: Allow availability macros to contain empty expansions.
In some cases, it's useful to allow an availability macro to expand to an empty
availability specification list. Allow developers to express this using macros
that are defined to exactly the string `*`, while continuing to reject `*` in
availability macros that contain other entries.
2026-03-02 12:14:29 -08:00
Kavon Farvardin 858cf2b37a Reparenting: adjust name lookup for associated types
We don't consider an associated type of a @reparentable protocol
to be an overridden decl of any protocols inheriting from it.
If we did, then it'd affect how getReducedType and other bits of
comparison between associated types work, because they prefer
anchors, i.e., associated types not overriding anything else
(see `swift::compareAssociatedTypes`).

That worked fine until availability checking brought to light that
name lookup also relies getOverriddenDecls() to determine which
associated type decl one would be the one we refer to in a protocol.
So in this case it's not the reduced type, but rather the actual
`DeclRefTypeRepr` that name-lookup finds for `Element`, that actually
_does_ need to view that one coming from the @reparentable one as being
"overridden", or shadowed I guess, by any other protocols downstream of it.

rdar://170819864
2026-02-20 18:04:35 -08:00
Kavon Farvardin fa7e9b3437 Merge pull request #87232 from kavon/reparenting-availability-2-redo
Reparenting: handle less-available reparented base protocols
2026-02-17 00:37:29 -08: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
Kavon Farvardin aba9338a85 Reparenting: handle unavailable base conformance descriptor
It's now possible to have a base protocol that's less available
than one inheriting from it:

```
@available(macOS 200)
@reparentable public protocol P {}

public protocol Q: P {}
```

In this case, the protocol conformance descriptor for a client type
conforming to Q would contain in its resilient witnesses a base conformance
descriptor for Q conforming to P. That symbol (ending in 'Tb') won't be
available on older versions of the library. So, we choose to weak-link
that symbol (i.e., expect it to be extern_weak) in order to allow the client
targeting an older deployment target to still work.
2026-02-13 16:37:19 -08:00
finagolfin e3cf8db4d1 [test][android] Enable Availability/availability_custom_domains_witness_table because it is passing (#87200)
This is breaking the Android CI because it is unexpectedly passing,
added in #87010 today.
2026-02-12 20:40:17 -08:00
Sam Pyankov 2fca555568 CustomAvailability: filter unavailable protocol requirements from witness tables
Protocol requirements marked with disabled availability domains are filtered
from witness tables during SIL generation.

rdar://169732762
2026-02-11 16:22:35 -08:00
Pavel Yaskevich b1ccdc30c4 [CSSimplify] Match root/value of a key path even when superclass check fails
This helps to propagate types bi-directionally and avoid extraneous
failures related to generic parameter inference when the real issue
is mutability or type erasure.
2025-12-18 16:39:16 -08:00
Pavel Yaskevich 270bbc5720 [CSDiagnostics] Produce a tailored diagnostic for key path mutability mismatches
If the type of a key path literal is read-only due to setter
availability constraints but the context requires a writable
key path, let's produce a tailed availability diagnostic that
points to the offending setter.

Resolves: rdar://157249275
2025-12-18 14:58:21 -08:00
Doug Gregor e624915ed9 [Custom availability] Serialize custom domains described on the command line
When a custom domain is described on the command line, there is no
backing declaration for it. Serialize such custom domains by
identifier and look them up globally at the point of deserialization.
When that fails, warn and drop the annotation.

This is all a stopgap until we have a way to spell custom availability
domains in the Swift language itself.
2025-12-02 12:12:50 -08:00
Ryan Mansfield 01d4323a70 XFAIL availability_custom_domains_clang_build test on Android
The test requires availability_domain.h which is clang builtin header
that is not available in the Android NDK's bundled clang.
2025-11-19 16:34:05 -05:00
Xi Ge d144524209 CustomAvailability: synthesized dynamic availability checking function should be private
When compiling a Swift module in incremental mode, each Swift source file is compiled into an object file
and we use linker to link them together. Because the predicate function for checking dynamic feature
availability is eagerly synthesized per compilation unit, the linker will complain about duplicated
symbols for them. Setting their visibility as private ensures that linker doesn't see them, thus addressing
the linker errors.

One workaround for this problem is to enable WMO.

rdar://164971313
2025-11-18 09:19:26 -08:00
Xi Ge e31c1cc7f8 CustomAvailability: avoid exposing the synthesized function for dynamic availability checking as an API entry
For dynamic features defined from a header, we synthesize a pure Clang function to check whether the feature should
be enabled at runtime. Swift modules don't have capability to deserialize this clang predicate function, leading to
crasher as a result. This change fixes the crasher by hiding the synthesized function to be a module internal one.

rdar://164410957
2025-11-10 09:26:28 -08:00
Doug Gregor b0a565e458 [Custom availability] Fix conformance availability diagnostic
Emit a proper diagnostic for a conformance that is not available due to
custom availability that doesn't have version information, eliminating
an assertion.
2025-11-03 22:35:57 -08:00
Allan Shortlidge 3b77e2e64a AST: Introduce experimental support for an anyAppleOS availability domain.
`anyAppleOS` represents a meta-platform for availability checks that can be
used to check availability across all of Apple's operating systems. It
supports versions 26.0 and up since version 26.0 is the first OS version number
that is aligned accross macOS, iOS, watchOS, tvOS, and visionOS.

Apple platform-specific availability specification take precedence over
`anyAppleOS` availability specifications when specified simultaneously.

Resolves rdar://153834380.
2025-10-31 15:21:30 -07:00
Alexis Laferrière 4c9a9ca9ad Merge pull request #85039 from xymus/exportability-var-diag
Sema: Custom diagnostic for var decls referencing a restricted type
2025-10-22 10:01:09 -07:00
Allan Shortlidge 16dee385bf AST/Basic: Introduce the StandaloneSwiftAvailability experimental feature.
This experimental feature will be used to force the compiler to treat `Swift`
runtime availability as separate from platform availability when compiling for
targets that have the Swift runtime built-in.
2025-10-21 21:55:24 -07:00
Alexis Laferrière 2d339c1260 Sema: Custom diagnostic for var decls referencing a restricted type
Replace the `here` part of the generic exportability diagnostic for
variables with: `in a property declaration marked public or in a
'@frozen' or '@usableFromInline' context`.

The full diagnostic now looks like:
```
error: cannot use struct 'ImportedType' in a property declaration marked
public or in a '@frozen' or '@usableFromInline' context;
'HiddenDependency' has been imported as implementation-only
```

This should be improved further to support implicitly exported memory
layouts in non-library-evolution and embedded.
2025-10-21 11:30:52 -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 2d8465b043 AST: Introduce the SwiftLanguageMode availability domain spelling.
Require `-enable-experimental-feature SwiftRuntimeAvailability` to use this new
spelling.
2025-10-08 15:46:34 -07:00
Henrik G. Olsson cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07:00
Allan Shortlidge ee988c0da8 Sema: Infer key path immutability during expression availability checking.
Accessor availability diagnostics for key path expressions were first
introduced by https://github.com/swiftlang/swift/pull/83931. Those changes were
insufficient because sometimes key path expressions are generated in the AST
with more mutability than is needed by the context and so setter availability
could be diagnosed inappropriately. Key path expression binding was refined in
https://github.com/swiftlang/swift/pull/84491 to make this less likely to
occur. However, there are still some circumstances in which a mutable key path
is generated in the AST and then immediately coerced into an immutable key path
to satisfy the contextual type. This change infers the immutability of these key
path expressions by looking through surrounding conversion expressions.
2025-09-25 07:13:18 -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 2d5824ef1a Sema: Diagnose key path setter availability.
Diagnose the availability of the specific accessors that are referenced
implicitly via a key path reference. This causes setter availability to be
diagnosed when passing a key path to a function that takes a `WritableKeyPath`.

Resolves rdar://157232221.
2025-08-27 07:58:35 -07:00
Allan Shortlidge 5e2dfdf4e6 Merge pull request #83714 from tshortli/allow-obsolete-in-swift-overrides 2025-08-14 07:16:50 -07:00
Allan Shortlidge 550d7bb754 Sema: Allow overrides to be unavailable in the current Swift language mode.
Fixes a regression from https://github.com/swiftlang/swift/pull/83354.

Resolves rdar://158262522
2025-08-13 17:57:05 -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 6e3361eb53 Tests: Add more obsoletion tests. 2025-08-13 11:25:33 -07:00