Commit Graph

15 Commits

Author SHA1 Message Date
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