Commit Graph

46 Commits

Author SHA1 Message Date
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
b45d278bce AST: Fix AvailabilityContext::forDeclSignature().
For declarations that aren't in source files there isn't an `AvailabilityScope`
tree to consult to find their `AvailabilityContext`. The fallback mechanism
used by `AvailabilityContext::forLocation()` walks up the `DeclContext`
hierarchy to build up the context for implicit code but this isn't entirely
correct since it skips over certain kinds of decls (like property
declarations). Instead, we need to walk up the parent decl hierarchy that is
used for availability inference to ensure that no decls with relevant
availability attributes are skipped.
2025-07-25 21:30:53 -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
030a3da88c AST: Fix AvailabilityContext::forDeclSignature().
It had a bug that would cause it to return incorrect results for some decls
with invalid source locations. Since not all declarations introduce a new
`DeclContext`, it's not sufficient to walk the decl context hiearchy when
computing availability for a decl.
2025-07-21 17:00:35 -07:00
Nate Chandler
2ec007d1a1 [NFC] AST: Add always available availability ctxt. 2025-04-17 09:23:23 -07:00
Allan Shortlidge
303f7d8452 AST: Build scopes for if #available queries on custom availability domains.
Resolves rdar://138441298 and rdar://138441307.
2025-04-01 09:03:41 -07:00
Allan Shortlidge
56e5ca70ba AST: Introduce AvailabilityDomain::isActivePlatform(). 2025-04-01 07:46:46 -07:00
Allan Shortlidge
e943c4bffd AST: Build statement condition availability scopes using AvailabilityContext.
Preparation for building availability scopes for `if #available` statements
that query non-platform availability domains.
2025-04-01 07:46:46 -07:00
Allan Shortlidge
119109d474 AST: Fix rendering of available ranges in AvailabilityContext::print(). 2025-04-01 07:46:46 -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
2dd544a73c AST/Sema: Sink AvailabilityContext for location queries from Sema to AST. 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
c6a5e6f745 AST: Reimplement AvailabilityContext storage using TrailingObjects.
This makes more efficient use of the permanent memory allocated for
`AvailabilityContext` representations and also fixes a leak that was introduced
in https://github.com/swiftlang/swift/pull/79718 where the small vector for
unavailable domain storage was not being cleaned up on `ASTContext`
deallocation.

Resolves rdar://145929932.
2025-03-03 18:29:34 -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
b630fe5479 AST: Rename Range to PlatformRange in AvailabilityContext::Info. 2025-03-02 14:45:50 -08:00
Allan Shortlidge
866db542a3 Gardening: Use const consistently for ASTContext parameters. 2025-03-02 14:45:50 -08:00
Allan Shortlidge
f34e135cfa AST: Add verification for AvailabilityContext. 2025-02-28 19:09:34 -08:00
Allan Shortlidge
9455a0fece AST: Track multiple unavailable domains in AvailabilityContext. 2025-02-28 19:09:34 -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
e87f80825d AST: Rename AvailabilityConstraint::Kind to Reason.
NFC.
2025-02-12 07:20:14 -08:00
Allan Shortlidge
862dccff1b AST: Adopt availability constraints query in AvailabilityContext.
Call `swift::getAvailabilityConstraintsForDecl()` to gather the constraints
that should be added to an AvailabilityContext when constraining it to the
availability of a given declaration.
2025-02-08 22:47:01 -08:00
Allan Shortlidge
ddb5f23306 AST: Add AvailabilityContext::containsUnavailableDomain().
Replaces AvailabilityContext::getUnavailableDomain().
2025-02-06 09:50:07 -08:00
Allan Shortlidge
889bc3138d unittests: Add tests for AvailabilityContext. 2025-02-06 09:50:07 -08:00
Allan Shortlidge
fe138e014a AST: Track an unavailable domain instead of platform in AvailabilityContext.
Now that most of the compiler tracks availability in terms of
AvailabilityDomain, it's time to do so in AvailabilityContext as well. This
will ensure that the compiler accurately suppresses diagnostics about a decl
being unavailable in an arbitrary domain when the context of the use is already
unavailable in that domain.

With this change, most of the special-casing for the Embedded Swift availability
domain has been removed from the compiler, outside of parsing and interface
printing.
2025-01-22 06:40:11 -08:00
Allan Shortlidge
0b5d787a73 AST: Rename AvailabilityContext::PlatformInfo.
It's about to get updated to contain more than just platform-specific
availability context information.

NFC.
2025-01-21 11:44:28 -08:00
Allan Shortlidge
c615faacd7 AST: Rename AvailabilityContext's storage member.
NFC.
2025-01-21 11:44:27 -08:00
Doug Gregor
c043f1138b Drop the "allows unsafe" modeling as availability
With the move to unsafe effects, we no longer model `unsafe` as an
availability problem. Remove all of that supporting code.
2025-01-10 10:39:16 -08:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Allan Shortlidge
00aae6ead5 AST: Return SemanticAvailableAttr from Decl::getUnavailableAttr(). 2024-12-19 17:22:51 -08:00
Doug Gregor
f33077a363 Ensure that @safe(unchecked) only applies to bodies (not declarations) 2024-12-13 13:31:21 -08:00
Doug Gregor
cf7fcf2da9 Fix test and improve dumper 2024-12-12 23:03:45 -08:00
Doug Gregor
268d5ccbde Suppress strict safety diagnostics in @unsafe declarations
When a declaration is `@unsafe`, don't emit strict safety diagnostics
for uses of unsafe entities, constructs, or types within it. This
allows one to account for all unsafe behavior in a module using strict
memory safety by marking the appropriate declarations `@unsafe`.

Enhance the strict-safety diagnostics to suggest the addition of
`@unsafe` where it is needed to suppress them, with a Fix-It. Ensure
that all such diagnostics can be suppressed via `@unsafe` so it's
possible to get to the above state.

Also includes a drive-by bug fix where we weren't diagnosing unsafe
methods overriding safe ones in some cases.

Fixes rdar://139467327.
2024-12-12 21:22:41 -08:00
Allan Shortlidge
c5398e17d3 AST: Introduce Decl::getDeprecatedAttr().
It replaces `DeclAttr::getDeprecated()` as the designated way to query for the
attribute that makes a decl deprecated.
2024-12-02 07:35:58 -08:00
Allan Shortlidge
3e50a90c45 AST: Introduce Decl::getUnavailableAttr().
It replaces `DeclAttr::getUnavailable()` and `AvailableAttr::isUnavailable()`
as the designated way to query for the attribute that makes a decl unavailable.
2024-12-02 07:35:58 -08:00
Allan Shortlidge
36230cd9c6 AST: Use an accessor to get the PlatformKind from an AvailableAttr. 2024-11-21 09:10:36 -08:00
Allan Shortlidge
c3f8352d5c AST: Introduce AvailabilityContext::forDeploymentTarget(). 2024-11-19 17:19:51 -08:00
Allan Shortlidge
6bf0e628be AST: Rename AvailabilityContext::getDefault() to forInliningTarget().
Mirrors the factory methods on `AvailabilityRange`.
2024-11-19 17:19:51 -08:00
Allan Shortlidge
6ac729ea41 Sema: Allow calls to @_unavailableInEmbedded functions in compatible contexts.
Treat `@_unavailableInEmbedded` as if it were `@available(Embedded,
unavailable)` and apply platform compatibility logic in the availability
checker. Revert back to disallowing calls to universally unavailable functions
(`@available(*, unavailable)`) in all contexts.
2024-11-01 13:03:30 -07:00
Allan Shortlidge
ea56972981 AST: Refactor constraining operations on AvailabilityContext. 2024-11-01 13:03:29 -07:00
Allan Shortlidge
7797681960 AST: Fix AvailabilityContext's containment check for platforms.
The unavailable platform kind of the outer AvailabilityContext must not inherit
availability from the platform of the inner context.
2024-11-01 08:18:13 -07:00
Allan Shortlidge
536e666aba Sema: Introduce TypeChecker::availabilityAtLocation().
Implement a query that returns the `AvailabilityContext` for a given
`SourceLoc` and `DeclContext`. Re-implement the existing type checker query
that just returns an `AvailabilityRange` on top of the new query.
2024-10-22 08:39:30 -07:00
Allan Shortlidge
e1995ce86a AST: Fix nesting of unavailable AvailabilityContexts. 2024-10-22 08:39:29 -07:00
Allan Shortlidge
2ba37da7e7 AST: Refactor AvailabilityContext::PlatformInfo operations. 2024-10-22 08:39:29 -07:00
Allan Shortlidge
b346385544 AST: Refactor AvailabilityContext into a value type.
Make the pointer to uniqued storage an implementation detail of an
`AvailabilityContext` value. This way clients of `AvailabilityContext` don't
need to think about pointers and can have access to mutating operations on a
context when appropriate.
2024-10-21 15:16:08 -07:00
Allan Shortlidge
0730209302 AST: Move AvailabilityContext into its own header and cpp file. 2024-10-21 15:16:08 -07:00