Commit Graph

43 Commits

Author SHA1 Message Date
Pavel Yaskevich
9e562881fe [TypeChecker] Improve diagnostics for access to actor-isolated values outside of the actor
Replaces generic `expression is 'async' but is not marked with 'await`
diagnostic with a tailed one for cases where there is an access to an
actor-isolated value outside of its actor without `await` keyword.

This makes the diagnostics for async and sync contexts consistent
and actually identifies a problem instead of simply pointing out
the solution.

Resolves: rdar://151720646
2025-05-23 00:20:18 -07:00
Michael Gottesman
3ed4059a60 [sema] Change non-sendable -> non-Sendable in diagnostics.
This matches send non sendable but importantly also makes it clear that we are
talking about something that doesn't conform to the Sendable protocol which is
capitalized.

rdar://151802975
2025-05-22 11:37:58 -07:00
Anthony Latsis
9a414c4de4 AST: Quote attributes more consistently in DiagnosticsParse.def 2025-04-22 18:23:40 +01:00
Anthony Latsis
2cd90bdd69 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-22 18:23:36 +01:00
Doug Gregor
3f1a6c84c2 Update tests for isolated conformances being enabled by default 2025-04-13 15:41:57 -07:00
Doug Gregor
1b1f28decb Tighten up diagnostics wording a bit 2025-03-20 22:13:19 -07:00
Doug Gregor
9ea735b9ed Rework diagnostics for conformance isolation failures
A protocol conformance can be ill-formed due to isolation mismatches
between witnesses and requirements, or with associated conformances.
Previously, such failures would be emitted as a number of separate
errors (downgraded to warnings in Swift 5), one for each witness and
potentially an extra for associated conformances. The rest was a
potential flood of diagnostics that was hard to sort through.

Collect all of the isolation-related problems for a given conformance
together and produce a single error (downgraded to a warning when
appropriate) that describes the overall issue. That error will have up
to three notes suggesting specific courses of action:
* Isolating the conformance (when the experimental feature is enabled)
* Marking the witnesses as 'nonisolated' where needed
*

The diagnostic also has notes to point out the witnesses/associated
conformances that have isolation problems. There is a new educational
note that also describes these options.

We give the same treatment to missing 'distributed' on witnesses to a
distributed protocol.
2025-03-20 21:23:16 -07:00
Doug Gregor
cd99fb57fd [Diagnostics] Remove unhelpful notes from witness-isolation diagnostics
When diagnosing an isolation mismatch between a requirement and witness,
we would produce notes on the requirement itself suggesting the addition of
`async`. This is almost never what you want to do, and is often so far
away from the actual conforming type as to be useless. Remove this note,
and the non-function fallback that just points at the requirement, because
they are unhelpful.

This is staging for a rework of the way we deal with conformance-level
actor isolation problems.
2025-03-19 17:18:52 -07:00
Anthony Latsis
eeb991122a [NFC] Switch from backticks to quotes in several diagnostics 2025-01-29 07:49:27 +00:00
Anthony Latsis
f85eb97337 ConformanceChecker: Make actor_isolated_witness call out the protocol 2025-01-14 14:49:22 +00:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Allan Shortlidge
c02fc4724d Tests: Remove -disable-availability-checking from many Concurrency tests.
Instead, use the `%target-swift-5.1-abi-triple` substitution to compile the tests
for deployment to the minimum OS versions required for use of _Concurrency APIs.
2024-10-18 16:21:51 -07:00
Pavel Yaskevich
22b9dd7f7b [Concurrency] Allow global actor mismatches while overriding @preconcurrency members in Swift 5 mode
Downgrade a mismatch on global actor attributes to a warning until
Swift 6 to enable class authors to introduce concurrency annotations
to overridable members.

Resolves: rdar://131347583
2024-09-13 11:28:47 -07:00
Holly Borla
b24b0883bb Merge pull request #75668 from hborla/actor-isolation-inference-source
[Concurrency] Compute the source of actor isolation in `ActorIsolationRequest`.
2024-08-05 08:01:00 -07:00
Holly Borla
5fa35b5c3e [Concurrency] Compute the source of actor isolation in ActorIsolationRequest. 2024-08-04 18:48:57 -07:00
Pavel Yaskevich
38aa71de91 [CSApply] Allow marker existential to superclass conversions
If existential is a protocol composition type where all of the
protocols are `@_marker`, narrowly allow coercion to its superclass
bound (if it matches). Both types have the same representation
which makes it okay.

This is only a problem in Swift 5 mode without strict concurrency
checks. In this mode `@preconcurrency` stripping happens
outside of the solver which means that no conversion restrictions
are recorded for members that got `& Sendable` stripped from
their types.

For example:

```swift
struct S {
  @preconcurrency static let member: KeyPath<String, Int> & Sendable
}

func test() {
  _ = S.member
}
```

Since `member` is `@preconcurrency` its type would get concurrency
annotations stripped, which includes `& Sendable` which means that
the solver uses `KeyPath<String, Int>` type for the reference and
not the original `KeyPath<String, Int> & Sendable`, this is a problem
for `ExprRewritter::adjustTypeForDeclReference` because conversion
between existential and its superclass bound requires a constraint
restriction which won't be available in this case.

Resolves: rdar://132700409
2024-08-01 12:41:02 -07:00
Pavel Yaskevich
cdf28bc482 [Concurrency] Allow sendability mismatches while overriding @preconcurrency properties in Swift 5 mode
Downgrade a mismatch on `any Sendable` -> `Any` to a warning until
Swift 6 to enable class authors to introduce concurrency annotations
to overridable properties.

Resolves: rdar://122193606
2024-07-08 09:29:50 -07:00
Pavel Yaskevich
789a31f8c7 Revert "[Sema] Warn about 'Any' to 'any Sendable' override" 2024-05-30 23:29:02 -07:00
Angela Laar
2a75075aa0 Merge pull request #73158 from angela-laar/any-to-any-sendable-override
[Sema] Warn about 'Any' to 'any Sendable' override
2024-05-30 17:15:28 -07:00
Doug Gregor
5a0e70a2bc Update diagnostic text to address code review feedback 2024-05-20 22:06:40 -07:00
Doug Gregor
640042f396 Suggest @preconcurrency on conformances it could help
When diagnosing a case where an actor-isolated witness cannot satisfy
a non-isolated requirement, also suggest that the conformance could be
annotated with `@preconcurrency`.
2024-05-20 22:06:40 -07:00
Angela Laar
f6404cde71 [Sema] Warn about 'Any' to 'any Sendable' override
Until Swift 6, warn about overriding Any property in parent class to any Sendable
2024-04-23 03:06:42 -07:00
Michael Gottesman
99e3f7fb13 [region-isolation] Make RegionBasedIsolation an upcoming feature for swift 6.
To make the tests pass, I had to teach sil-opt how to setup upcoming features
since it did not know how to parse them.

rdar://124100266
2024-03-05 15:15:14 -08:00
Holly Borla
e47a66cc33 [Concurrency] Enable IsolatedDefaultValues under strict concurrency checking. 2024-01-10 20:53:42 -08:00
Holly Borla
813f952ff4 [Concurrency] Suppress concurrency diagnostics in extensions of preconcurrency
declarations if strict checking is not enabled.
2024-01-05 15:48:57 -08:00
Holly Borla
6212085d7c [Concurrency] Downgrade isolated call diagnostics when the callee has
an inferred `@preconcurrency` annotation.
2024-01-05 14:44:53 -08:00
Holly Borla
88e37b2ae5 [Concurrency] Downgrade missing 'await' errors to warnings when referencing
preconcurrency declarations.
2023-12-05 20:58:18 -08:00
Michael Gottesman
cb46851194 [region-isolation] Rename the experimental feature to RegionBasedIsolation.
This ensures that the pass is called TransferNonSendable but the experimental
feature is RegionBasedIsolation.
2023-10-26 12:01:44 -07:00
Michael Gottesman
0bad8f9b67 [region-isolation] Rename SendNonSendable.cpp -> TransferNonSendable.cpp. 2023-10-26 12:01:44 -07:00
Holly Borla
57214ce7fb [Concurrency] Try harder to downgrade preconcurrency errors to warnings in
Swift 5 mode.

When you annotate a ValueDecl with `@preconcurrency`, the compiler should allow
concurrency violations by downgrading errors in the actor isolation checker to
warnings in Swift 5 mode. Previously, the actor isolation checker only checked
whether the caller's context was preconcurrency when deciding to downgrade, so
referencing preconcurrency declarations directly remained errors. Preconcurrency
was also dropped when computing actor isolation for declarations imported from
clang, which are always preconcurrency.
2023-10-04 07:07:41 -07:00
Holly Borla
2e9c64d36c [Concurrency] Do not allow actor isolation violations in function conversions
to impact constraint solving.
2023-09-21 19:44:03 -07:00
Michael Gottesman
b53af9419c [send-non-sendable] Add REQUIRES: asserts to concurrency tests that use SendNonSendable. 2023-08-31 19:25:23 -07:00
Michael Gottesman
026f1735b5 [send-non-sendable] Update concurrency tests so that we run them in all concurrency modes as appropriate.
This means that:

1. In test cases where minimal is the default (swift 5 without
-warn-concurrency), I added RUN lines for targeted, complete, and complete +
sns.

2. In test cases where complete is the default (swift 6, -warn-concurrency,
specified complete with -strict-concurrency), I added a send non-sendable run
line.

In each of these cases, I added additional expected-* lines as appropriate so
the tests can compile in each mode successfully.
2023-08-30 13:40:17 -07:00
Kavon Farvardin
3f6a0ccb90 Allow a global-actor to be dropped for some non-async functions.
It's ok to drop the global-actor qualifier `@G` from a function's type if:

- the cast is happening in a context isolated to global-actor `G`
- the function value will not be `@Sendable`
- the function value is not `async`

It's primarily safe to drop the attribute because we're already in the
same isolation domain. So it's OK to simply drop the global-actor
if we prevent the value from later leaving that isolation domain.
This means we no longer need to warn about code like this:

```
@MainActor func doIt(_ x: [Int], _ f: @MainActor (Int) -> ()) {
  x.forEach(f)
// warning: converting function value of type '@MainActor (Int) -> ()' to '(Int) throws -> Void' loses global actor 'MainActor'
}
```

NOTE: this implementation is a bit gross in that the constraint solver
might emit false warnings about casts it introduced that are actually
safe. This is mainly because closure isolation is only fully determined
after constraint solving. See the FIXME's for more details.

resolves rdar://94462333
2023-01-05 17:54:00 -08:00
Doug Gregor
4fc4bd79e8 [Constraint solver] Track "isolated by preconcurrency" in the solver.
Rather than only setting the isolated-by-preconcurrency bit during
constraint application, track the closures it will be set for as part
of the constraint system and solution. Then, use that bit when
performing "strict concurrency context" checks and type adjustments,
so we don't treat an inferred-to-by-`@Sendable`-by-preconcurrency
closure in the solver as if it weren't related to preconcurrency.

Fixes the spurious warning from
https://github.com/apple/swift/issues/59910.
2022-07-07 23:46:30 -07:00
Doug Gregor
89ab4aac30 Fix the "early" computation of a return type to respect @preconcurrency.
Fixes rdar://91087622.
2022-03-31 12:46:00 -07:00
Becca Royal-Gordon
e04ec5a7d9 Weaken some type checks for @preconcurrency decls
In an async context, trying to pass a non-`@Sendable` function to an `@Sendable` parameter or trying to assign a `@MainActor` method to a non-`@MainActor`-typed variable were hard errors. We now think that this a mistake for `@preconcurrency` APIs in Swift 5 mode, as it hinders retroactive adoption of `@Sendable` and `@MainActor` by libraries.

This PR weakens these errors to warnings *only* when the decl which contains the attribute in its type signature is `@preconcurrency` and *only* when in Swift 5 mode (with or without -warn-concurrency). For non-`@preconcurrency` decls, it is still an error.

Fixes <rdar://88703266>.
2022-02-15 13:35:55 -08:00
Doug Gregor
7149702d12 Rename @_predatesConcurrency to @preconcurrency.
Introduce the `@preconcurrency` attribute name for `@_predatesConcurrency`,
which has been the favored name in the pitch thread so far. Retain the
old name for now to help smooth migration.
2022-01-26 08:39:01 -08:00
Doug Gregor
7e375f7df6 Use new Sendable staging infrastructure consistently.
The main effect of this change is that diagnostics about Sendable
conformances now follow the same minimal/full logic used for other
Sendable diagnostics, rather than having their own separate
computation.
2021-11-30 18:22:53 -08:00
Doug Gregor
f32572b4cd Apply @_predatesConcurrency adjustments to variables, subscripts. 2021-11-18 21:12:28 -08:00
Doug Gregor
5cc00c289d Remove the @_unsafeSendable/@_unsafeMainActor attributes.
These have been subsumed by @_predatesConcurrency. Leave in minimal
parser support that warns about the removal, so we don't fully break
existing source code.
2021-11-18 11:21:03 -08:00
Doug Gregor
6907c94a57 Protocols that predate concurrency downgrade Sendable checking.
When the Error and CodingKey protocols introduced their Sendable
requirements, we carved out special Sendable checking rules that
suppressed diagnostics when a type becaming Sendable implicitly via
conformance to Error or CodingKey. Extend that logic to any protocol
that predates concurrency, so that existing libraries can benefit from
the same staging we used for Error and CodingKey.

In the future, we can mark Error and CodingKey to eliminate the
special case.
2021-11-17 14:18:01 -08:00
Doug Gregor
71a980d93b Add @_predatesConcurrency attribute for declarations.
Introduce the `@_predatesConcurrency` attribute, which specifies that a
given declaration existed prior to the introduction of Swift
Concurrency, has been updated to use concurrency features (global
actors, Sendable, etc.), but should retain its pre-concurrency behavior
for clients that have not yet opted into concurrency.

Implement type and actor-isolation adjustments to
`@_predatesConcurrency` declarations to subsume the `@_unsafeMainActor`,
`@_unsafeSendable`, and `@MainActor(unsafe)` use cases. This is the
bulk of the semantic transformations needed for this new attribute,
but is not yet complete.

Part of rdar://84448438.
2021-11-16 21:39:31 -08:00