Commit Graph

74 Commits

Author SHA1 Message Date
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
Suyash Srijan
b30cc9dc1c [Sema] Diagnose 'nonisolated' attribute on wrapped properties (#60475)
* [Sema] Diagnose 'nonisolated' attribute on wrapped properties

* [AST] Update diagnostic wording for nonisolated_wrapped_property
2022-08-10 20:51:49 +01:00
Suyash Srijan
ad26df09ab Revert "[Sema] Propagate nonisolated attribute from wrapped property to the synthesized projectedValue property (#60421)" (#60462)
This reverts commit 2aa47b86dd.
2022-08-09 19:29:35 +01:00
Suyash Srijan
2aa47b86dd [Sema] Propagate nonisolated attribute from wrapped property to the synthesized projectedValue property (#60421)
* [Sema] Propagate nonisolated attribute from wrapped variable to the synthesized projectedValue variable

* [Test] Add some test cases
2022-08-09 01:40:22 +01:00
hank121314
c05afe20f0 test[Concurrency]: Add test to make sure actor isolation checking is performing in visitPatternBinding. 2022-06-21 11:19:11 +08:00
Doug Gregor
fdc6260bc1 Reimplement conformance isolation checking using the actor reference logic.
Reimplement the final client of ActorIsolationRestriction, conformance
isolation checking, to base it on the new "actor reference" logic.
Centralize the diagnostics emission so we have a single place where we
emit the primary diagnostic (which is heavily customized based on
actor isolation/distributed/etc.) and any relevant notes to make
adjustments to the witness and/or requirement, e.g., adding
'distributed', 'async', 'throws', etc. Improve the diagnostics
slightly by providing Fix-Its when suggesting that we add "async"
and/or "throws".

With the last client of ActorIsolationRestriction gone, remove it
entirely.
2022-04-07 17:10:02 -07:00
Doug Gregor
1b27c48cc6 Switch non-member actor isolation checking to the new implementation 2022-04-07 09:07:37 -07:00
Doug Gregor
eef2704c86 Reimplement actor isolation checking for referencing a declaration.
Start collapsing the several implementations of actor isolation checking
into a single place that determines what it means to reference a declaration
from a given context, potentially supplying an instance for an actor. This
is partly cleanup, and partly staging for the implementation of the
Sendable restrictions introduced in SE-0338. The result of this check
falls into one of three categories:

* Reference occurs within the same concurrency domain (actor/task)
* Reference leaves an actor context to a nonisolated context (SE-0338)
* Reference enters the context of the actor, which might require a
combination of implicit async, implicit throws, and a "distributed" check.

Throughout this change I've sought to maintain the existing semantics,
even where I believe they are incorrect. The changes to the test cases
are not semantic changes, but reflect the unification of some
diagnostic paths that changed the diagnostic text but not when or how
those diagnostics are produced. Additionally, SE-0338 has not yet been
implemented, although this refactoring makes it easier to implement
SE-0338.

Use this new actor isolation checking scheme to implement the most
common actor-isolation check, which occurs when accessing a member of
an instance.
2022-04-07 09:07:36 -07:00
Kavon Farvardin
9b1fcedf2f backtrack on part of SE-327 dealing with default-value exprs
This effectively reverts 6823744779

The blanket removal of isolation in default-value expressions had
unintented consequences for important workflows. It's still
a problem that needs to be addressed, but we need to be more precise
about the problematic situations.
2022-03-18 18:41:05 -07:00
Kavon Farvardin
13dd3d9ecf have the inferred isolation for properties change only in Swift 6
This patch delays the removal of redundant isolation for inferred
global-actor isolation to Swift 6 too, since we only warn about it
changing in Swift 5. Otherwise, only isolation that is a byproduct
of inference no longer needs an await, which will probably confuse
people.

This change is with respect to SE-327, which argues that the
non-static stored properties of ordinary structs do not need
global-actor isolation.
2022-02-04 14:43:12 -07:00
Kavon Farvardin
19bdfb1172 In a property-wrapper, global-actor isolation remains on wrappedValue
If a struct is a property-wrapper, then global-actor isolation
still applies to the `wrappedValue`, even if it's a stored property.
This is needed in order to support the propagation of global-actor
isolation through the wrapper, even when the programmer has opted
to use a stored property instead of a computed one for the
`wrappedValue`. Since this propagation is a useful pattern, I think
this exception is reasonable.
2022-02-04 14:41:49 -07:00
Kavon Farvardin
a60da756cf fix bug with unidentified custom attribute during serialization
In Swift 5 mode, I only warned about the global-actor attribute
becoming unnecessary in the future, yet I was still returning
None for the global-actor attribute checking request. This led
to the attribute remaining unidentified, but also not removed.
During module serialization, this problem was manifesting as
emitting a typeless attribute, which when deserialized would
trigger a segfault.
2022-02-04 14:41:31 -07:00
Kavon Farvardin
bd024caca9 [SE-327] Remove redundant global-actor isolation.
As part of SE-327, global-actor isolation applied to
the instance-stored properties of a value type do
not require any isolation, since there is no way to
create a race on access to that storage.

https://github.com/apple/swift-evolution/blob/main/proposals/0327-actor-initializers.md#removing-redundant-isolation

This change turns global-actor annotations on such
properties into an error in Swift 6+, and a warning
in Swift 5 and earlier.

In addition, inference for global-actor isolation
no longer applies global-actor isolation to such
properties. Since this latter change only results
in warnings in existing Swift 5 code, about a now
superflous 'await', this change will happen in
Swift 5+.

Fixes rdar://87568381
2022-02-03 15:19:59 -07: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
Kavon Farvardin
6823744779 reimplement nonisolated initializing exprs for instance properties
It's possible to create an impossible set of constraints for
instance-member stored properties of a type. For example:

@MainActor func getStatus() -> Int { /* ... */ }
@PIDActor func genPID() -> ProcessID { /* ... */ }

class Process {
  @MainActor var status: Int = getStatus()
  @PIDActor var pid: ProcessID = genPID()

  init() {} // Problem: what is the isolation of this init?
}

We cannot satisfy the isolation of the initilizing expressions,
which demand that genStatus and genPID are run with isolation
from a non-async designated initializer, which is not possible.

This patch changes the isolation for those initializer expressions
for instance members, saying that the isolation is unspecified.

fixes rdar://84225474

The first attempt to do this was in
https://github.com/apple/swift/pull/40652
But, I implemented that as a hard source break, since the isolation
was changed in a way that an error diagnostic would be emitted.
This commit reimplements the change more gently, as a warning for
Swift 5 users.
2022-01-19 13:01:22 -08:00
Doug Gregor
f18382cfd9 Don't infer 'nonisolated' on stored properties.
Fixes rdar://80424675.
2021-12-14 13:41:07 -08:00
Doug Gregor
5a6f58e686 Teach witness matching to apply minimal/strict diagnostics rules.
This downgrades some errors to warnings in Swift 5 mode, and ensures that
we diagnose all conditions in Swift 6 mode.
2021-12-02 22:24:09 -08:00
Doug Gregor
a77f05f627 Switch @_unsafeSendable and @_unsafeMainActor tests to @_predatesConcurrency 2021-11-18 09:01:31 -08:00
Doug Gregor
3ddf8c6921 Actor isolation inference: @IBAction implies @MainActor(unsafe)
@IBAction calls are always delivered in the main thread, so reflect that
by inferring `@MainActor(unsafe)` for such methods whenever no other
actor isolation is provided.

Implements rdar://84474640.
2021-11-08 16:37:58 -08:00
Doug Gregor
dec3a9a1d9 Propagate actor isolation through dynamic replacement.
Infer actor insolation through dynamic replacement, because we need
the same rules for the original and the replacements.
Fixes rdar://83153816.
2021-11-05 15:37:58 -07:00
Doug Gregor
68418cd660 [SE-0302] Crank up Sendable to also diagnose in-module missing conformances.
Extend the diagnostics for `Sendable` conformances to always diagnose
missing `Sendable` conformances for nominal types that are within the
same module. The intuition here is that if the type is in the same
module, it can be updated and evaluated at the same time as code
requiring the `Sendable` conformance is introduced.

Another part of rdar://78269348.
2021-08-16 14:01:42 -07:00
Doug Gregor
ea5ec22971 Allow a global-actor-isolated class to inherit from a nonisolated one.
Fixes rdar://80902604.
2021-07-30 11:15:01 -07:00
Doug Gregor
eeeea49764 Remove -enable-experimental-concurrency almost everywhere. 2021-07-26 21:24:43 -07:00
Doug Gregor
1e2012d816 Disable availability checking in tests that use concurrency 2021-07-20 12:46:26 -07:00
Doug Gregor
5dc4fcf91e A "defer" block is global-actor-isolated if its context is.
Fixes rdar://80799233.
2021-07-19 13:13:34 -07:00
Doug Gregor
96ced72349 [SE-0316] Check global actor isolation of a class vs. its superclass.
Close an actor-isolation hole where we permitted a class to have a
different global actor isolation than its superclass.
2021-07-01 14:28:39 -07:00
Doug Gregor
9ec20776c9 [SE-0306] Make actors semantically "final".
Treat actors as being semantically `final` throughout the type checker.
This allows, for example, a non-`required` initializer to satisfy a
protocol requirement.

We're leaving the ABI open for actor inheritance should we need it.

Addresses rdar://78269551.
2021-06-30 10:59:49 -07:00
Doug Gregor
5bcd5a8e25 Use "isolated" parameters to check for synchronous actor access.
Rework the checking of actor member access to rely on "isolated" parameters
(and captures thereof) to determine whether one can synchronously access
an actor or not. This allows synchronous access via an "isolated" parameter
as a general notion, which subsumes the declaration-based "self" access.

Simplify the checking of and diagnostic reporting for actor member
access by collapsing a number of redundant diagnostics down into a
single, parameterized diagnostic with a single point of emission. This
normalizes the logic a bit.
2021-06-08 00:00:12 -07:00
Doug Gregor
5004a5447b [Concurrency] Make "self" parameter of actor-isolated functions 'isolated'.
The notion of "actor-isolated" currently exists at the declaration level.
For functions, it is going to be captured in the function type itself,
where 'self' is declared to be 'isolated'. Model isolation both
ways: the 'self' of a method that is isolated to an actor instance
will be 'isolated' as well.

We are still using declaration-based checking of actor isolation.
However, by mirroring this information we can move more incrementally
over to doing checking based on 'isolated' parameters.
2021-06-07 23:59:38 -07:00
Doug Gregor
03f55d7bb4 Revert "Revert "Remove @actorIndependent attribute.""
This reverts commit 7c0b50e8ea.
2021-06-03 14:03:57 -07:00
Karoy Lorentey
5925cb2ed8 Merge pull request #37619 from apple/revert-37462-remove-actor-independent
Revert "Remove `@actorIndependent` attribute."
2021-05-25 10:37:51 -07:00
Holly Borla
27f86adee0 Merge pull request #37627 from hborla/local-default-init-wrapper
[Property Wrappers] Always check actor isolation and initializer effects in `PropertyWrapperInitializerInfoRequest`.
2021-05-25 09:43:28 -07:00
Holly Borla
1068494854 [NFC][Test] Add tests for actor isolation checking for local property
wrapper default initializers.
2021-05-24 19:39:14 -07:00
Karoy Lorentey
7c0b50e8ea Revert "Remove @actorIndependent attribute." 2021-05-24 14:43:32 -07:00
Doug Gregor
d6e8fd81f5 Remove @actorIndependent attribute.
`@actorIndependent` has been superseded by `nonisolated`. Remove
the old spelling entirely.
2021-05-17 12:21:10 -07:00
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
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
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
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
Holly Borla
97ecb9435f [ASTScope] Always skip implicit attributes.
ASTScope only cares about attributes when lookup can be done inside of the
attribute, which isn't the case for implicit attributes because they're
typically built fully type-checked. This also avoids a crash when an
implicit attribute does not have a source range.
2021-04-06 16:38:46 -07:00
Doug Gregor
1f33aa9586 Infer a type's actor isolation from the property wrappers it uses.
Fixes rdar://76252310.
2021-04-05 23:13:52 -07:00
Doug Gregor
f02a01e4f2 Add @_unsafeMainActor corresponding to @MainActor 2021-03-31 09:40:41 -07:00
Doug Gregor
5342706d1c [Actor inference] Infer global actors on a type based on conformances.
If a protocol is stated to be part of a global actor, infer that a type
that conforms to a protocol in its original definition is also part of
that global actor.

Addresses rdar://75992299.
2021-03-29 23:56:45 -07:00
Doug Gregor
1a9b58d9f9 Add support for @actorIndependent on closures.
Closures often end up being implicitly actor-isolated. Allow them to opt
out with `@actorIndependent`, as as other declarations can opt out of
actor isolation.
2021-03-25 22:37:53 -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
dbc021a73f [Concurrency] Don't let "unsafe" inference block other inference.
The presence of an unsafe global-actor attribute on something we can
infer from (e.g., a witnessed protocol requirement or overridden
declaration) was preventing propagation of actor-isolation attributes
from outer scopes. Only take "positive" results from such places.
2021-03-19 00:02:22 -07:00