Commit Graph

67 Commits

Author SHA1 Message Date
Doug Gregor
2b9ca315fe [Concurrency] Remove asyncHandler attribute.
The `asyncHandler` attribute turned out to be the wrong solution
to the problem of creating a sync->async bridge. Remove it.
2021-05-13 17:01:39 -07:00
Pavel Yaskevich
ecfff76552 [Concurrency] Actor cannot conform to global actor isolated protocol
Attempting to conform an actor to a global actor isolated protocol
creates a clash in isolation when members are accessed so, let's
detect and diagnose that.

Resolves: rdar://75849035
2021-05-12 16:00:25 -07:00
Alexis Laferrière
7487c12d05 Merge pull request #37269 from xymus/test-macro
[Concurrency] Define and use the SwiftStdlib 5.5 availability macro in tests
2021-05-10 10:46:53 -07:00
Doug Gregor
052cc7d022 [Concurrency] Revert 'nonisolated let' change.
The change made to SE-0306 to require 'nonisolated let' is undercutting
the effectiveness of the model. Revert while we work on a better
solution.
2021-05-06 17:30:11 -07:00
Alexis Laferrière
3310a55682 [Test] Use the SwiftStdlib 5.5 macro in Concurrency tests 2021-05-06 13:48:49 -07:00
Holly Borla
b6a3434851 [Concurrency] Emit a tailored note for a missing await if the call is implicitly
asynchronous due to actor isolation.
2021-05-05 11:35:40 -07:00
Holly Borla
8e78c8b35c [CSApply] Propagate 'async' from contextual types to closures, unless the closure
is an argument to a 'reasync' function.
2021-05-05 11:35:40 -07:00
Evan Wilde
d0d9bef703 Merge pull request #37056 from etcwilde/ewilde/fix-await-fixits
[Concurrency] Fix await fix-it placement
2021-04-26 08:12:50 -07:00
Evan Wilde
3da0a540eb Update tests
This patch updates all the tests to accept the new error messages.
*Gack* so many things needed cleaning up.
2021-04-24 07:51:18 -07:00
Doug Gregor
b88e678694 [SE-0306] Disable actor inheritance.
Actor inheritance was removed in the second revision of SE-0306. Remove
the ability to inherit actors.

Note that this doesn't fully eliminate all vestigates of inheritance
from actors. There are simplifications that need to be performed
still, e.g., there's no need to distinguish
designated/convenience/required initializers. That will follow.
2021-04-23 15:08:57 -07:00
Doug Gregor
7cc19b5807 [SE-0306] Require 'nonisolated' on lets for synchronous cross-actor access. 2021-04-23 02:20:35 -07:00
Doug Gregor
6928c3f1c9 Make sure that we perform the actor hop for @_inheritActorContext 2021-04-21 17:35:17 -07:00
Doug Gregor
b9f1e7f626 [Concurrency] Add @_inheritActorContext hidden parameter attribute.
This new attribute can be used on parameters of `@Sendable async` type
to indicate that the closures arguments passed to such parameters
should inherit the actor context where they are formed, which is not
the normal behavior for `@Sendable` closures.

Another part of rdar://76927008.
2021-04-21 15:23:27 -07:00
Evan Wilde
284ac2a7a7 Update actor isolation tests
Updated the tests for non-global-actor protected deinit.
We can't access main-actor synchronous functions, main-actor static
properties, but can access non-static main-actor stored properties in
the deinit.
2021-04-20 10:18:22 -07:00
Doug Gregor
14a311b36e [Global actors] Check calls to global-actor-qualified functions, not references
Check actor isolation of calls to functions with global-actor-qualified
type. This closes a pre-existing loophole where a value of
global-actor-qualified function type could be called from any context.
Paired with this, references to global-actor-qualified function
declarations will get global-actor-qualified function type whenever
they are referenced within an experience, i.e., whenever we form a
value of that type. Such references can occur anywhere (one does not
need to be on the actor), and carrying the global actor along with the
function type ensures that they can only be called from the right
actor. For example:

    @MainActor func onlyOnMainActor() { ... }

    func callIt(_ fn: @MainActor () -> Void) {
      fn() // error: not on the main actor, so cannot synchronously call
           // this wasn't previously diagnosed
    }

    func passIt() {
      callIt(onlyOnMainActor)  // okay to pass the function
                               // used to be an error
    }

While here, fix up some broken substitution logic for
global-actor-qualified function types and "override" actor isolation.
2021-04-16 00:26:39 -07:00
Konrad `ktoso` Malawski
34f8e768b5 spawnDetached to detach, non-suspending group.spawn, spawnUnlessCancelled 2021-04-03 09:53:32 +09:00
Konrad `ktoso` Malawski
6e746304e1 [Concurrency] Update Task and Group APIs based on Review 1 2021-04-03 09:52:03 +09:00
Doug Gregor
d3d2d75b4d Merge pull request #36698 from DougGregor/actor-escaping-isolation
[Actors] Permit non-sendable, escaping closures to be actor-isolated.
2021-04-02 13:01:17 -07:00
Mike Ash
1173b737aa [Concurrency] Add availability to Concurrency APIs.
This allows programs to target older OSes while using Concurrency behind an availability check. When targeting older OSes, the symbols are weak-linked and the compiler will require the use of Concurrency features to be guarded by an availability check.

rdar://75850003
2021-04-01 10:42:08 -04:00
Doug Gregor
5ecb9ebec2 [Actors] Permit non-sendable, escaping closures to be actor-isolated.
Remove the heuristic that escaping closures cannot be actor-isolated.
This is in line with the long term plan for actor isolation, but opens
up some holes in the short term because Sendable closures are not enforced
throughout the Swift ecosystem.

One significant effect of this change is that we will now, statically,
fail to detect many cases where actor isolation can be broken.
2021-03-31 23:22:56 -07:00
Doug Gregor
360fe608bb [Actor inference] Infer global actors on all value members of a type.
Rather than restricting inference of global actors to instance members,
as we do for actor types, infer global actors for all value members,
which also includes static variables and methods, initializers, and
destructors.
2021-03-29 23:31:31 -07:00
Doug Gregor
c76dac7155 Banish @asyncHandler to a hidden flag.
We don't want @asyncHandler to be part of the concurrency model, so put
it behind a different flag.
2021-03-25 16:45:21 -07:00
Doug Gregor
81af954f50 [Actor isolation] Initializers don't infer global actor isolation.
Initializers within a global-actor-isolated type are not inferred to
be global-actor-isolated themselves, because that isn't generally what
one wants: usually, you want to be able to create an instance of
the type by calling the initializer from somewhere.

Within such initializers, allow access to the stored properties on
"self" regardless.

Fixes rdar://75450300.
2021-03-23 21:44:38 -07:00
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -07:00
Doug Gregor
508274c90e Update "non-concurrent-value" diagnostics to say "non-sendable" 2021-03-18 23:14:47 -07:00
Doug Gregor
dbc63ebdca Synchronous closures can have global actors as part of their type 2021-03-17 00:51:58 -07:00
Doug Gregor
5b2afec56a Only produce ConcurrentValue-related diagnostics under -warn-concurrency. 2021-03-15 12:13:44 -07:00
Doug Gregor
09d1215259 Declarations within Actor-conforming protocols are actor-isolated.
The Actor protocol is used only to describe actors. When a protocol's
Self type conforms to the actor protocol, any instance declarations on
the protocol or extensions thereof are considered to be actor-isolated
to 'self'.

Because the instance requirements of such a protocol are
actor-isolated to 'self', they can be witnessed by actor-isolated
instance declarations on an actor type. For example:

```swift
protocol P: Actor {
  func f() // okay, actor-isolated to self
}

extension P {
  func g() { f() } // okay, actor-islated to self
}

actor MyActor: P {
  func f() { } // okay, witnesses actor-isolated requirement
}
```
2021-03-09 17:19:41 -08:00
Doug Gregor
30bfe4dcc0 Update diagnostics to talk about non-isolated rather than @actorIndependent 2021-03-07 22:16:34 -08:00
Doug Gregor
b412a44817 Allow cross-actor calls to synchronous global actor functions.
... and add some tests I've had lying around locally.
2021-03-07 14:30:30 -08:00
Doug Gregor
2f2c194272 Adopt 'nonisolated' in many tests, make sure its a modifier 2021-03-07 11:37:57 -08:00
Kavon Farvardin
0c675c74cf Improve error messages for implicitly-async in sync contexts
when an expression that would be validly considered implicitly-async appears
within a sync context, we try to be a bit more specific about the problem
being that it's a sync context.
2021-03-06 10:26:44 -08:00
Doug Gregor
09e1c34143 Merge pull request #36014 from DougGregor/global-actor-closures
[Concurrency] Allow async closures to be annotated with global actors.
2021-03-05 12:37:31 -08:00
Kavon Farvardin
e4076b64a1 adding test of SILGen for async actor prop accesses 2021-03-04 18:37:32 -08:00
Kavon Farvardin
74eb1a7ae6 allow property/subscript access expressions for actors in typechecker
We now mark some DeclRefExpr and LookupExprs as implicitly async
during typechecking, depending on whether they appear in a context
that is only performing a read / get operation, and whether they
are cross-actor operations.

also resolves rdar://72403401 by improving the error messages
(no more vague "'await' in async context" when its clearly a call!)
2021-03-04 18:37:32 -08:00
Doug Gregor
6dd87a2947 [Concurrency] Ensure we do implicit-async from @actorIndependent & global-actors contexts
We weren't allowing implicit "async" promotions for cross-actor references
from functions defined within an actor that were @actorIndependent
or part of a global actor. Ensure we do promotion there.

... and because these lead to misleading diagnostics in a lot of places,
don't do implicit "async" promotion if we're not in an async context
already. We still need to improve the diagnostics here.
2021-02-24 13:24:50 -08:00
Doug Gregor
54dc437a25 [Concurrency] Ensure we get the right context for captured variables.
The declaration context of an explicitly-captured value is the context
of the original, captured declaration itself... not the closure in which
the value is captured. Account for this in data race checking, by tracking
the effective capture context for such variables. This eliminates some
erroneous complains about accesses to explicitly-captured variables in
concurrent code.

Part of rdar://74281361.
2021-02-23 16:38:16 -08:00
Doug Gregor
5bb290cb12 [Concurrency] Allow async closures to be annotated with global actors.
Allow an asynchronous closure to be annotated with a global actor,
which isolates it to that actor. For example:

```swift
Task.runDetached { @MainActor in
  // runs on main actor
}
```
2021-02-16 23:50:32 -08:00
Evan Wilde
f2f3e3d339 Check actor isolation in TapExprs
The actor isolation checker wasn't checking that tap expressions weren't
incorrectly using actor-isolated state. As a result, we could read actor
isolated state anywhere we wanted to. This was most prominent in string
interpolations.
2021-02-16 13:27:46 -08:00
Doug Gregor
3a9ddf2f10 Merge pull request #35927 from DougGregor/concurrentvalue-classes 2021-02-11 18:24:53 -08:00
Kavon Farvardin
9342540661 add fix-it to note for "add 'async' to function"
resolves rdar://72313654
2021-02-11 14:44:49 -08:00
Doug Gregor
6dd1c4768a [Concurrency] Put the flow-sensitive concurrent captures check behind a flag
We're not quite ready to commit to the flow-sensitive check that would
allow a concurrent function to read from a mutable local capture so
long as the captured variable wasn't changed after the point of
capture. Put it behind a flag and implement the more restrictive rule
(no access to mutable local captures in concurrent code). We can relax
it later.
2021-02-11 11:43:07 -08:00
Doug Gregor
7be05deafc [Concurrency] Simplify checking for local functions.
Treat them as local captures and require them to be @concurrent.
2021-02-11 00:37:21 -08:00
Evan Wilde
8b80331c3d Updating tests to use actor
This patch updates the `actor class` spelling to `actor` in almost all
of the tests. There are places where I verify that we sanely handle
`actor` as an attribute though. These include:

 - test/decl/class/actor/basic.swift
 - test/decl/protocol/special/Actor.swift
 - test/SourceKit/CursorInfo/cursor_info_concurrency.swift
 - test/attr/attr_objc_async.swift
 - test/ModuleInterface/actor_protocol.swift
2021-02-10 08:09:13 -08:00
Doug Gregor
923ccfdf97 [Concurrency] Fix actor isolation and restrictions on initializers.
Initializers are actor-isolated when they are part of an actor or have
a global actor. However, uses of actor initializers need to be treated
as cross-actor references so we proper `ConcurrentValue` checking for
values passed into the initializer.

Fixes rdar://74064751.
2021-02-09 16:19:37 -08:00
Doug Gregor
5d337f79b6 Diagnose unsafe global variable accesses within concurrent closures/functions.
Concurrent closures and functions are, of course... concurrent. Treat
them as such for the warnings about unsafe accesses to global variables.
2021-01-29 14:43:38 -08:00
Doug Gregor
a554ad632b [Concurrency] Diagnose mutating accesses to locals from concurrent code.
Replace the existing warning about any access to a local variable from
concurrently-executing code with a more tailored error:
concurrently-executing code may read a mutable varable, but cannot
modify it. This is safe so long as we either always do by-value
captures in concurrent closures or we ensure that no mutation of that
variable can occur after the point of capture.

We'll follow up with one of those. For now... be careful out there.

Since we're promoting this to an error, narrow it down to concurrent
closures and local functions, dropping the assumption that escaping
closures "may execute concurrently."
2021-01-29 14:27:24 -08:00
Doug Gregor
f4c5a267b7 @concurrent functions are actor-independent
Concurrent functions need to be actor-independent because they wouldn't
ever be safe to run concurrently while isolated on an actor. This
allows us to elimate the "may execute concurrently with" check related
to actor isolation, which is a cleaner overall story.
2021-01-28 22:07:54 -08:00
Doug Gregor
8eae527ca6 Tighten up actor isolation checking for closures and local functions.
Make sure that we check the isolation of the context in which a reference
to `self` is made, rather than the context in which `self` is declared,
when checking whether we are within actor-isolated code. This ensures
that we report errors as actor-isolation errors rather than falling
back to the "may execute concurrently with" checking.
2021-01-28 21:56:07 -08:00
Doug Gregor
1d082e1fb4 Require that concurrently-executed local functions be @concurrent. 2021-01-28 13:03:13 -08:00