Commit Graph

181 Commits

Author SHA1 Message Date
Sima Nerush
f726e7e3da [Concurrency] Allow 'nonisolated' to be applied to mutable storage of 'Sendable' type on a globally-isolated value type. 2024-07-04 08:46:42 -07:00
Michael Gottesman
474aa47732 [concurrency] Standardize sending of non-isolated -> nonisolated to match the keyword 'nonisolated'.
rdar://130827967
2024-06-29 18:09:38 -07:00
Michael Gottesman
4697546e09 [sending] Mark Task.init,detached, and friends as sending methods instead of __owned @Sendable so we can capture non-isolated values. 2024-06-21 02:24:03 -07:00
Holly Borla
128a8bc942 [Concurrency] Don't ignore mismatching isolation for overrides of
Clang-imported superclass methods.
2024-06-08 16:58:30 -07:00
Holly Borla
135e3f2ee9 Merge pull request #73889 from hborla/deprecate-anyactor
[Concurrency] Deprecate `AnyActor`.
2024-05-30 09:36:35 -07:00
Holly Borla
c20b0e0956 [Concurrency] Obsolete AnyActor in Swift 6 using a typealias. 2024-05-29 22:59:19 -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
Doug Gregor
e1a4a1e8e6 Don't diagnose accesses to global/static variables within the same module
When SE-0412 (strict concurrency for global variables) is enabled, each
global or static mutable variable will be diagnosed if it isn't
explicitly on a global actor or `nonisolated(unsafe)`.

Suppress diagnostics for references to such global variables when they
are in the same module as the declaration of the global variable
itself. While these diagnostics are technically correct, they are not
strictly necessary since we've already diagnosed the global variable
itself (with more actionable advice), and they tend to pile on the
developer in a manner that is not helpful.
2024-05-19 18:35:19 -07:00
Doug Gregor
d4ce618e2f Provide more Fix-It guidance for concurrency-unsafe global variables (SE-0412)
When diagnosing a concurrency-unsafe global or static variable, provide
Fix-Its with specific guidance and advice. This is intended to aid the
workflow for folks enabling strict concurrency checking or Swift 6.
There are up to three Fix-Its attached to a diagnostic about
concurrency-unsafe global/static variables:

* convert 'global' to a 'let' constant to make the shared state
immutable, which replaces `var` with `let`
* restrict 'global' to the main actor if it will only be accessed from the
main thread, which adds `@MainActor`
* unsafely mark %0 as concurrency-safe if all accesses are protected
by an external synchronization mechanism, which adds `nonisolated(unsafe)`

I fretted over two things before deciding on this path:

1. For the second note, the reality is that any global actor will
suffice, but `@MainActor` is orders of magnitude more common than any
other global actor, so "common case convenience" wins over "precise
but less useful.
2. For the third note, `nonisolated(unsafe)` should only be used
sparingly, and surfacing it via Fix-It could cause overuse. However,
developers need to know about it, and this is how we do that. It comes
last in the list of notes (after the better options) and says "unsafe"
in not one but two places.
2024-05-19 15:05:39 -07:00
Michael Gottesman
de85b79423 [concurrency] Make GlobalActorIsolatedTypesUsability an upcoming swift 6 feature.
rdar://118244451
2024-05-13 18:40:58 -07:00
Holly Borla
87f49a5571 [Concurrency] Allow isolation on subclasses of non-Sendable, non-isolated
superclasses.
2024-05-07 15:40:01 -07:00
Holly Borla
39c7962661 [Concurrency] Only treat 'nonisolated let' properties in actors as isolated
if access is from outside the module or the property type is not 'Sendable'.
2024-04-25 22:28:16 -07:00
Slava Pestov
a991179017 Concurrency: Local functions should inherit isolation from context
Instead of looking at captures, which wasn't sound with
recursive local functions; we would look at the captures of
the function currently being type checked, which are not
available yet.
2024-04-18 13:14:49 -04:00
Sima Nerush
0abcf8d268 [Concurrency] Infer @Sendable for globally isolated function types.
Since globally isolated functions may never be called concurrently, they are implicitly Sendable.
2024-04-02 18:51:33 -07:00
Holly Borla
61548230d7 [Concurrency] Don't allow erasing global actor isolation when the function
value crosses an isolation boundary.
2024-03-26 18:53:41 -07:00
Sima Nerush
05859b442c [Features] Gate global-actor-isolated types usability changes behind an experimental feature flag. 2024-03-23 20:03:01 -07:00
Sima Nerush
043cc26661 [Concurrency] Infer nonisolated for a mutable storage of a value type accessed from within the module. 2024-03-23 20:01:45 -07:00
Holly Borla
d3ed4a40e8 Merge pull request #72214 from hborla/isolated-struct-properties
[Concurrency] Allow struct members to have isolation annotations.
2024-03-09 17:30:19 -08:00
Holly Borla
8bfe832c47 [Concurrency] Allow struct members to have isolation annotations. 2024-03-09 14:15:04 -08: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
9ba481ad53 [Diagnostics] Clarify the wording of error_in_future_swift_version. 2024-03-01 12:05:51 -08:00
Holly Borla
e32fd8a322 [Concurrency] Downgrade errors about actor-isolated witnesses to warnings until
Swift 6 when the witness is a non-Sendable 'let'.
2024-02-07 19:37:30 -08:00
Holly Borla
c98d93d75f Merge pull request #70909 from hborla/nonisolated-nonsendable 2024-01-28 13:10:23 -08:00
Holly Borla
14ced21afa [Concurrency] Update tests for downgraded warning. 2024-01-26 10:55:31 -08:00
Holly Borla
009d7d0c70 [Concurrency] nonisolated can only be applied to actor properties with
`Sendable` type.
2024-01-26 08:54:28 -08:00
Sophia Poirier
74d2127583 Merge pull request #71067 from sophiapoirier/nonisolated-mutable-stored-property-diagnostic
[Concurrency] clarify diagnostic for 'nonisolated' stored properties being due to mutability
2024-01-24 11:43:43 -08:00
Sophia Poirier
27b57c6ce3 [Concurrency] clarify diagnostic for 'nonisolated' stored properties being due to mutability 2024-01-23 15:15:29 -08:00
Holly Borla
20829faa43 [Concurrency] Handle cases where a property initializer is subsumed by another
property for IsolatedDefaultValues.

For property wrappers and init accesors, skip property initializers that are
subsumed, e.g. by an init accessor or a backing property wrapper initializer,
and always consider the subsuming initializer to determine whether compiler
synthesized initializers should have `nonisolated` applied.

This change also lessens the source break of SE-0411 by still emitting
member initializers in implicit constructors when the initializer violates
actor isolation to preserve the behavior of existing code when concurrency
diagnostics are downgraded to warnings in Swift 5 mode.
2024-01-19 18:12:25 -08:00
Holly Borla
b99995c2b2 [Concurrency] Downgrade isolated_default_argument_context to a warning until
Swift 6 for stored properties.
2024-01-16 10:47:52 -08:00
Holly Borla
d37b9763b6 [Concurrency] Promote StrictConcurrency to an upcoming feature flag. 2024-01-11 21:23:25 -08:00
Holly Borla
e47a66cc33 [Concurrency] Enable IsolatedDefaultValues under strict concurrency checking. 2024-01-10 20:53:42 -08:00
Pavel Yaskevich
dbf1146963 [Concurrency] NFC: Switch tests to use -strict-concurrency=complete instead of -warn-concurrency 2023-12-08 14:10:51 -08:00
Holly Borla
ab497d95cf [Concurrency] Downgrade the error for accessing isolated static lets across
actors to a warning.
2023-11-13 19:05:41 -05:00
Holly Borla
439c51d7d2 Merge pull request #69562 from hborla/cross-actor-reference-init-fix
[Concurrency] Only apply Sendable initialization rules to actor isolated storage of `self`.
2023-11-01 07:19:00 -07:00
Holly Borla
8f44abc67f [Concurrency] Only apply Sendable initialization rules to actor isolated
storage of 'self'.
2023-10-31 17:04:26 -07:00
Michael Gottesman
64f273cb8d Merge pull request #69437 from gottesmm/region-isolation-makeover
[region-isolation] Rename pass/feature flag and so some other cleanups.
2023-10-26 15:38:05 -07: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
Ben Barham
36686d720d [Sema] Move predates concurrency remarks to warnings
Remarks are intended to be enabled via eg. `-R...`, where as
`(add|remove)_predates_concurrency_import` is a diagnostic that's always
output without any `-R` flag. Move it to a warning instead.

Resolves rdar://114207080.
2023-10-25 14:37:25 -07:00
Holly Borla
6afd38fcd3 [Concurrency] Diagnose initializing actor isolated stored properties with
non-Sendable type from global actor isolated initializers.
2023-10-18 22:37:19 -07:00
Holly Borla
c9bfe8c3dd [Concurrency] Don't allow subclasses of nonisolated classes to add global
actor isolation.

Adding global actor isolation via subclassing admits data races because
actor-isolated types are Sendable while nonisolated classes are not (unless
otherwise annotated), so this allowed bypassing Sendable checking.
2023-10-11 12:41:00 -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
Holly Borla
90ccc75efa [Concurrency] Do not allow accessing actor-isolated superclass properties
from nonisolated subclass initializers.
2023-09-08 20:53:30 -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
Holly Borla
84eaeb766f [Concurrency] Diagnose non-Sendable 'self' arguments crossing actor isolation
boundaries in member reference expressions.
2023-08-22 21:28:33 -07:00
Doug Gregor
d78a5edb99 Handle all isolation checking for function calls in one place
Isolation checking for calls had two separate implementation places:
one that looked at the declaration being called (for member
declarations) and one that worked on the actual call expression. Unify
on the latter implementation, which is more general and has access to
the specific call arguments. Improve diagnostics here somewher so we
don't regress in that area.

This refactoring shouldn't change the actual semantics, but it makes
upcoming semantic changes easier.
2023-06-26 13:39:56 -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
218a3f79e0 [Constraint solver] Downgrade/ignore concurrency issues more generally.
Instead of the `warning` Boolean threaded through the solver's
diagnostics, thread `DiagnosticBehavior` to be used as the behavior
limit. Use this for concurrency checking (specifically dropped
`@Sendable` and dropped global actors) so the solver gets more control
over these diagnostics.

This change restores the diagnostics to a usable state after the prior
change, which introduced extra noise. The only change from existing
beavior is that dropping a global actor from a function type is now
always a warning in Swift < 6. This is partly intentional, because
there are some places where dropping the global actor is well-formed.
2022-07-01 11:45:44 -07:00