Commit Graph

151 Commits

Author SHA1 Message Date
Anthony Latsis
2cd90bdd69 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-22 18:23:36 +01:00
Anthony Latsis
96be6cf6a6 DiagnosticEngine: Do not describe an accessor's storage for %kindonly
This is the desired behavior is most cases. In the future, we should
consider adding format specifiers for short/detailed descriptions.
2025-04-05 12:31:19 +01:00
Allan Shortlidge
0fd2f3fc1c Sema: Diagnose @_spi_available on declarations that cannot be unavailable.
The attribute makes the declaration unavailable from the perspective of clients
of the module's public interface and was creating a loophole that admitted
inappropriate unavailability.
2025-03-07 19:44:48 -08: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
e90530990a Parse/Sema: Move some AvailabilitySpec diagnostics from Parse to Sema.
Eventually, querying the `AvailabilityDomain` associated with an
`AvailabilitySpec` will require invoking a request that takes a `DeclContext`.
This means that any diagnostics related to the domain identified by an
`AvailabilitySpec` need to be emitted during type-checking rather than parsing.
This change migrates several `AvailabilitySpec` diagnostics from Parse to Sema
to unblock further work.
2025-02-19 11:40:56 -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
174e57cdad Sema: Always allow method overrides to be as available as the context.
When a method override is as available as the class it's a member of, then it
can't be any more available. It doesn't make sense to diagnose such a method as
less available than the method it overrides. This regressed recently for
methods belonging to classes that are nested inside extensions. The
availability of the derived class may be defined by its context, but the
compiler was only checking the availability attributes directly on the class.

Resolves rdar://143600638.
2025-01-26 22:34:53 -08:00
Allan Shortlidge
239720897a AST: Rename TypeRefinementContext to AvailabilityScope. 2024-11-12 11:34:25 -08:00
Allan Shortlidge
d2f77f45c1 Merge pull request #77341 from tshortli/unavailable-types-in-unavailable-signatures
Sema: Fixes for availability checking in unavailable contexts
2024-11-01 12:40:24 -07:00
Allan Shortlidge
7c75b26f1b Sema: Make suppression of availability checking for types more consistent.
Availability checking for types was only suppressed when the immediate context
for the use of the type was explicitly marked unavailable. Availability is
lexical so the checking should be suppressed in the entire scope instead.
2024-11-01 08:18:13 -07:00
Allan Shortlidge
85ded8549c Sema: TypeRefinementContextBuilder must not skip defer blocks.
https://github.com/swiftlang/swift/pull/76621 caused a regression by skipping
the AST nodes nested under `defer` blocks. The node associated with a `defer`
block is implicit because it is a kind of closure context synthesized by the
compiler. However, the nodes it contains are not implicit and so they must be
visited by the `TypeRefinementContextBuilder`.

Resolves rdar://139012152
2024-10-31 23:13:08 -07:00
Allan Shortlidge
21837e9fde Sema: Reject @available on observing accessors.
Marking an observer unavailable (or potentially unavailable) has no effect
since the compiler emits calls to them unconditionally.

Resolves rdar://80337141.
2024-09-11 16:42:48 -07:00
Ben Langmuir
fd1875dcfb [test] Move availability tests to later fake OS versions
10.50 was once greater than any real macOS version, but now it compares
less than real released versions, which makes these tests depend on the
deployment target unnecessarily. Update these tests to use even larger
numbers to hopefully keep them independent a little longer.
2024-08-21 11:38:54 -07:00
Allan Shortlidge
a288422a99 Sema: Fix override availability checking for protocols.
Given the following test case, override availability checking produced an
erroneus diagnostic:

```
@available(macOS 10.15, *)
protocol P {
    associatedtype A
    var a: A { get set }
}

@available(macOS 13.0, *)
protocol Q: P {
    // error: overriding _modify accessor for 'a' must be as available as
    //        declaration it overrides
    var a: A { get set }
}
```

The synthesized `_modify` accessor in `Q` is explicitly marked available in
macOS 13, which is less available than the `_modify` accessor in `P`. The
availability of `Q` should limit the required availability of the override and
prevent the diagnostic, but the implementation had a bug where it checked the
availability of the context's type instead of the contextual self type's
declaration. In the example, the context's type is `Self` which does not have
annotated availability.

Resolves rdar://133573707.
2024-08-19 22:23:55 -07:00
Doug Gregor
8cd2f34654 Generalize tests for both diagnostic styles, or force the LLVM style
These tests are using FileCheck to check the result of diagnostic
formatting in ways that don't match the new formatter. Force the old
formatter or, where possible, generalize so that they match both
formatters.
2024-02-19 02:48:37 -10:00
Doug Gregor
37959de29e Establish type refinement contexts for pattern binding decls directly
The type refinement context builder had a bunch of logic to try to
model type refinement contexts for the first variable declaration that
shows up within a pattern binding declaration. Instead, model this
more syntactically by creating a type refinement context for the
pattern binding declaration itself. This both addresses a regression
in the handling of `if #available` within a closure that's part of an
initializer, and fixes a bug in the same area where similar code has
explicit availability annotations.
2023-08-02 15:07:09 -07:00
Allan Shortlidge
9812c90023 Sema: Ban unavailable stored properties.
Marking a stored property as unavailable with `@available` should be banned,
just like it already is banned for potential unavailability. Unavailable code
is not supposed be reachable at runtime, but unavailable properties with
storage create a back door into executing arbitrary unavailable code at
runtime:

```
@available(*, unavailable)
struct Unavailable {
  init() {
    print("Unavailable.init()")
  }
}

struct S {
  @available(*, unavailable)
  var x = Unavailable()
}

_ = S() // prints "Unavailable.init()"
```

Resolves rdar://107449845
2023-04-04 08:52:39 -07:00
Allan Shortlidge
620462ddf6 Sema: Don't diagnose declarations more available than unavailable container.
It turns out that we must allow declarations with `introduced:` availability
nested inside of other declarations that are `unavailable` in order to
influence weak linking. Stop diagnosing declarations as being more available
than their unavailable containers and revert some changes to availability
inference that were designed to avoid creating these nestings but caused
regressions for declarations marked `@_spi_available.`

Reverts parts of https://github.com/apple/swift/pull/64310,
https://github.com/apple/swift/pull/64015, and
https://github.com/apple/swift/pull/62900.
2023-03-21 13:55:05 -07:00
Anthony Latsis
14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Allan Shortlidge
f796dc9417 Sema: Don't diagnose explicitly unavailable decls as "more available than unavailable scope."
Fixes a logic error introduced in https://github.com/apple/swift/pull/61242.

Resolves rdar://102877953
2022-12-09 17:33:58 -08:00
Allan Shortlidge
cebfe57cab Sema: Diagnose declarations that are annotated to be more available than their unavailable enclosing scope.
Resolves rdar://92552186
2022-09-21 18:00:43 -07:00
swift-ci
234ffb08d2 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-10 22:13:32 -07:00
Allan Shortlidge
bd7a394cbb Sema: Allow explicitly available overrides to be as available as their context.
Previously, the following test case produced an erroneous diagnostic:

```
class A {
  init() {}
}

@available(macOS 12, *)
class B: A {
  @available(macOS 12, *)
  override init() { // error: overriding 'init' must be as available as declaration it overrides
    super.init()
  }
}
```

The overridden `init()` constructor is as available as it can possibly be. Removing the explicit `@available` annotation suppressed the diagnostic.

To fix this, we check to see if the override is as available as its self type and accept it if it is.

You may be wondering how this works when the `@available` annotation is removed from `override init()` in the example. It turns out that `AvailabilityInference::availableRange()` returns a result that is based only on the explicit availability of the decl in question without taking the availability of the context into account (except when the context is an extension). So with the explicit annotation gone, both the base `init()` and the override are both considered to be "always" available. This is pretty unintuitive and arguably wrong. However, it seems like a lot of existing code depends on this behavior so I've left it for now.

Resolves rdar://96253347
2022-07-01 17:53:47 -07:00
Ben Barham
f2bd6ce9cb [next] Remove subminor version from various tests
The "0" subminor is no longer included after
114b4d96e4 (which generally doesn't add a
subminor). Remove it from checks depending on it.
2022-05-11 17:06:29 -07:00
Robert Widmann
48849dc1b3 Shop Over-Availability Diagnostics
Adopt descriptive language and mention the actual availability bounds that are at issue when a declaration is more available than its enclosing scope.
2022-05-06 16:59:46 -07:00
Allan Shortlidge
c78090bc40 Sema: When computing potential unavailability of a decl, first check whether the decl is explicitly unavailable and the context is also unavailable. If those conditions are met, treat the decl as if it were always available since unavailable code is allowed to reference unavailable decls.
Previously, `TypeChecker::checkDeclarationAvailability()` would behave this way for most explicitly unavailable decls by _accident_. An explicitly unavailable decl has no introduction version, and the existing logic therefore would fail to find a lower bound on the availability of the decl. The edge case that exposed the fragility of this logic was an unavailable extension containing a member with it's own explicit availability. The unavailability of the extension ought to trump the availability of the member, but the existing logic couldn't detect that.

The compiler also ought to diagnose the conflicting availability annotations but I'd like to address that separately.

Resolves rdar://92551870
2022-05-05 07:21:30 -07:00
Allan Shortlidge
6f8057d1e0 Sema: Avoid diagnosing potential unavailability of type components (extension nominal type, superclass, etc.) on declarations that are explicitly unavailable.
Resolves rdar://92179327
2022-04-26 16:03:10 -07:00
Allan Shortlidge
5091a7827b NFC: Use 'potentially unavailable' instead of 'unavailable' in the availability_versions.swift test to distinguish between test cases for types that are only available starting in a certain release versus types that are marked strictly unavailable. 2022-04-25 16:50:58 -07:00
Allan Shortlidge
e34196df0b Fix tests that depended on availability for lazy vars. 2022-02-04 10:28:11 -08:00
Slava Pestov
c508ef56ac Sema: Check conditional availability earlier in conformance checking
We would only check conditioanlavailability once we had committed
to a best witness. Instead, let's use this information to disambiguate
among multiple best witnesses, if we have them.

Access control and unconditional unavailability are still only
checked once the witness is chosen.

Fixes rdar://problem/77259046.
2021-07-09 18:03:38 -04:00
Slava Pestov
9d5baaae9b Merge pull request #36327 from slavapestov/fix-storage-availability
Fix availability checking for stored properties and enum cases with associated values
2021-03-06 09:23:01 -05:00
Slava Pestov
511ada4a50 Sema: Ban potential unavailability on enum cases with associated values
This cannot be supported for the same reasons struct and class stored
properties with potential unavailability cannot be supported.

It is possible that some users relied on this, because it worked in
some cases where the type metadata for the potentially unavailable
type was not actually needed for anything (eg, if the type was @frozen
and trivial, or a simple class reference). If this becomes an issue
we can downgrade this to a warning.

Note that just like the previous fix for stored properties, we do
still allow writing an '@available' attribute on the enum element if
the OS version is not any newer than the deployment target. This allows
annotating APIs with the OS version where they were introduced.
2021-03-05 18:03:05 -05:00
Slava Pestov
cb9c1f93ee Sema: Allow @available on stored properties as long as its as new as the deployment target 2021-03-05 18:03:05 -05:00
Slava Pestov
c346c4fd3b Sema: Relax witness availability check
We were checking that the witness is at least as available as
the intersection of the protocol and the conforming type.

In the case where the conformance is defined in an extension,
this should actually be the intersection of the protocol and
the _availability of the extension_.

Fixes <rdar://problem/74114880>.
2021-03-05 16:58:57 -05:00
Slava Pestov
8fd29a57cc Sema: Add some missing checks for where clauses on non-generic declarations
Access control, availability and exportability checking missed these
because of a baked-in assumption that getGenericParams() == nullptr
rules out the presence of a trailing where clause.
2020-10-16 00:34:56 -04:00
Slava Pestov
1888724166 Sema: Disallow protocols from refining less available protocols
Concrete types can conform to unavailable protocols because
the witness table for the conformance is not required for use
with the concrete type itself.

However, protocols cannot have unavailable base protocols.
I believe this was an oversight of the original implementation
here.
2020-10-16 00:27:26 -04:00
Alexis Laferrière
e509d6883a Revert "[Sema] Fix availability checking in inlinable code" 2020-10-14 10:52:11 -07:00
Alexis Laferrière
fb76ff1aea [Sema] Report non-constructor unavailable overrides as warnings
Report unavailable overrides as a warning to avoid breaking sources.
2020-10-12 19:13:55 -07:00
Alexis Laferrière
1ca852e77a [Sema] Accept unavailable constructors override in unavailable types
Preserve the old behavior of accepted unavailable override of
constructors as this change would be source breaking.
2020-10-09 10:57:01 -07:00
Alexis Laferrière
2f182c2b78 [Sema] Consider unavailable functions as being unreachable
This has the effect of rejecting unavailable overrides to available
methods in a similar way as overrides that are less available than the
introduction are rejected.
2020-10-09 10:40:21 -07:00
Nathan Hawes
9bcb54910e [AST] Prefer the 'macOS' spelling over 'OSX' when printing the platform kind.
This affects module interfaces, interface generation in sourcekitd, and
diagnostics. Also fixes a fixit that was assuming the 'OSX' spelling when
computing the source range to replace.

Resolves rdar://problem/64667960
2020-07-08 13:51:25 -07:00
Mishal Shah
92ca9fc924 [Apple Silicon] Generalize tests for other macOS architectures
Most of the changes fall into a few categories:
* Replace explicit "x86_64" with %target-cpu in lit tests
* Cope with architecture differences in IR/asm/etc. macOS-specific tests
2020-07-02 16:27:46 -07:00
Slava Pestov
2ef101c815 Sema: Don't add local functions to TC.definedFunctions
Instead, check them and their error handling right away.

In addition to fixing the crash in the radar, this also causes
us to emit unused variable warnings in functions containing
local functions.

Eventually, TC.definedFunctions should go away altogether.

Fixes <rdar://problem/53956342>.
2019-08-07 00:37:21 -04:00
Brent Royal-Gordon
3494c0bd88 [TypeChecker] Rephrase platforms in availability diagnostics
This does several different things to improve how platforms are described in availability diagnostics:

• Mentions the platform in diagnostics for platform-specific @available(unavailable) attributes.
• Replaces “OS X” with “macOS”.
• Replaces “fooOS application extension” with “application extensions for fooOS”.
• Replaces “on fooOS” with “in fooOS”.

Fixes <rdar://problem/49963341>.
2019-04-30 16:32:43 -07:00
Nate Chandler
c21678d34a Corrected tests by removing implicit returns. 2019-04-24 10:04:20 -07:00
Pavel Yaskevich
63b802ca88 [AST/Printing] Don't omit empty labels in special names
This makes diagnostics more verbose and accurate, because
it's possible to distinguish how many parameters there are
based on the message itself.

Also there are multiple diagnostic messages in a format of
`<descriptive-kind> <decl-name> ...` that get printed as
e.g. `subscript 'subscript'` if empty labels are omitted.
2018-09-24 18:36:53 -07:00
Slava Pestov
463a467046 Merge pull request #18026 from slavapestov/inherited-availability-attribute-part-2
Sema: Fix availability of inherited designated initializers, part 2
2018-07-30 18:40:11 -07:00
Jordan Rose
6290d9be60 Introduce DescriptiveDeclKind::Property (#18183)
...and collapse StaticVar/ClassVar and StaticLet/ClassLet into
StaticProperty/ClassProperty.

"var" and "let" aren't great nouns to use in diagnostics to begin with,
especially alongside semantic terms like "instance method". Focus on
the type vs. non-type aspect instead with "property", which better
matches how people talk about member vars (and lets) anyway.
2018-07-30 09:23:59 -07:00
Slava Pestov
336f51e5bc Sema: Fix availability of inherited designated initializers, part 2
We need to explicitly handle nested types here.

Fixes the (re-opened) <rdar://problem/40853731>,
<https://bugs.swift.org/browse/SR-7881>.
2018-07-24 16:13:45 -07:00
Slava Pestov
cc06543e87 Sema: Remove third pass over parameter list when validating generic functions and subscripts
The code for performing the third pass using the GenericToArchetypeResolver
is still there, but it only runs for non-generic declarations.

The next step is to consolidate this path with the generic path.
2018-07-18 01:54:24 -07:00