Commit Graph

64 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
Mykola Pokhylets
5376e56776 Fixed no copying IsIsolated flag when cloning subscript params 2025-04-30 23:30:22 +02:00
Anthony Latsis
72c0d4cc75 Diag: Handle CustomAttr in formatDiagnosticArgument 2025-03-28 02:03:38 +00:00
Michael Gottesman
527616d105 [sema] Move some extra type checking from getIsolationFromAttribute to the attribute checker.
This never belonged in ActorIsolationRequest since it fits perfectly in the
attribute checker. This also simplifies the logic before I add code to
getIsolationFromAttribute to handle ExecutionAttribute.
2025-01-28 12:39:05 -08:00
John McCall
e99ec88ce3 Use common code paths for constructor concurrency prologs if we're not in
the async self-isolated actor initializer case.

Fixes rdar://138394497, a bug where we didn't set up isolation correctly for
an async parameter-isolated initializer, but also probably a non-trivial number
of other latent differences between initializers and normal functions.
2024-11-13 17:10:32 -05:00
Allan Shortlidge
4f728cda40 Concurrency: Look through l-value types in ActorIsolation::getActor().
When computing the actor type for an expression, be sure to look through
l-value types to get the underlying type.

Resolves rdar://139470254.
2024-11-07 18:32:52 -08: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
55b8d9538d [CSSimplify] Rework how/when mismatches between optional types are fixed
- Don't attempt to insert fixes if there are restrictions present, they'd inform the failures.

  Inserting fixes too early doesn't help the solver because restriction matching logic would
  record the same fixes.

- Adjust impact of the fixes.

  Optional conversions shouldn't impact the score in any way because
  they are not the source of the issue.

- Look through one level of optional when failure is related to optional injection.

  The diagnostic is going to be about underlying type, so there is no reason to print
  optional on right-hand side.
2024-09-10 10:35:05 -07:00
Michael Gottesman
4bb2e4f3b1 [region-isolation] Improve the error we emit for closure literals captured as a sending parameter.
Specifically:

I changed the main error message to focus on the closure and that the closure
is being accessed concurrently.

If we find that we captured a value that is the actual isolation source, we
emit that the capture is actually actor isolated.

If the captured value is in the same region as the isolated value but is not
isolated, we instead say that the value is accessible from *-isolated code or
code within the current task.

If we find multiple captures and we do not which is the actual value that was
in the same region before we formed the partial apply, we just emit a note on
the captures saying that the closure captures the value.

I changed the diagnostics from using the phrase "task-isolated" to use some
variant of accessible to code in the current task.

The idea is that in all situations we provide a breadcrumb that the user can
start investigating rather than just saying that the closure is "task-isolated".

From a preconcurrency perspective, I made it so that we apply the preconcurrency
behavior of all of the captures. This means that if one of the captures is
preconcurrency, we apply the preconcurrency restriction to the closure. This is
one step towards making it so that preconcurrency applies at the region level...
we just are not completely there yet.

rdar://133798044
2024-08-14 10:37:31 -07:00
Michael Gottesman
e7e035f60c [region-isolation] Convert the transfer non sendable typed error to be a new short-error, long-note form error.
I also messed with the text a little bit.

This eliminates the last of the old style diagnostics.
2024-07-31 13:10:02 -07:00
Konrad `ktoso` Malawski
df7df7e4c0 Merge pull request #75031 from Sajjon/cyon_typo_batch__test__concurrency
Typos: `test/concurrency`
2024-07-08 14:24:13 +09:00
Michael Gottesman
62c6de7713 [region-isolation] Reuse ActorInstance::lookThroughInsts when computing the actor instance for SILIsolationInfo purposes.
We are already using this routine in other parts of TransferNonSendable to
ensure that we look through common insts that SILGen inserts that do not change
the actual underlying actor instance that we are using. In this case, I added
support for casts, optional formation, optional extraction, existential ref
initialization.

As an example of where this came up is the following test case where we fail to
look through an init_existential_ref.

```swift
public actor MyActor {
  private var intDict: [Int: Int] = [:]

  public func test() async {
    await withTaskGroup(of: Void.self) { taskGroup in
      for (_, _) in intDict {}
      await taskGroup.waitForAll() // Isolation merge failure happens here
    }
  }
}
```

I also added the ability to at the SIL level actual test out this merge
condition using the analysis test runner. I used this to validate that this
functionality works as expected in a precise way.

rdar://130113744
2024-07-06 23:02:11 -07:00
Alexander Cyon
7074c29bf3 [test/concurrency] Fix typos 2024-07-06 13:25:45 +02: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
1a07152ee0 [NFC][Concurrency] Remove -disable-region-based-isolation-with-strict-concurrency
from tests.
2024-06-19 20:48:59 -07:00
Holly Borla
7af38860ed [Concurrency] Deprecate and obsolete AnyActor.
This marker protocol isn't useful for abstracting over actors and distributed
actors because it doesn't have any runtime requirements, so there's no way
to switch to the given actor. Instead, you should use `isolated (any Actor)?`
parameters, and the compiler will compute a local actor value from a
distributed actor using `DistributedActor.asLocalActor`.
2024-05-24 08:57:17 -07:00
Michael Gottesman
a3d95b993d [concurrency] Add REQUIRES: asserts to a test. 2024-04-24 11:32:09 -07:00
Michael Gottesman
2e5b3bc257 [region-isolation] Do not treat functions/class_methods that are isolated to a #isolated as being globally isolated to that.
Instead, we need to consider the isolation at the apply site.

rdar://126285681
rdar://125078448
2024-04-11 16:00:55 -07:00
Meghana Gupta
5934b9466c Disable test Concurrency/isolated_parameters.swift 2024-04-08 11:53:46 -07:00
Michael Gottesman
b3e837c16c [region-isolation] Enable region isolation by default with strict-concurrency.
I added a disable flag -disable-region-based-isolation-with-strict-concurrency
so that we do not need to update the current tests. It is only available when
asserts are enabled to ensure users cannot use it.

rdar://125918028
2024-04-04 13:07:32 -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
e653423b13 [Concurrency] Make sure to record the actor for #isolation when used as
a default argument for a regular (not isolated) parameter.
2024-03-04 21:24:58 -08:00
Holly Borla
a8a55eb1b1 [Concurrency] Look through caller-side default arguments for #isolation
when computing the isolated actor expression for a call.
2024-03-04 20:10:03 -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
26621b804c [Concurrency] Enable optional isolated parameters. 2024-02-16 16:28:45 -08:00
Holly Borla
ef265399bd Merge pull request #71503 from hborla/invalid-isolated-param
[Concurrency] Always diagnose invalid isolated parameter types in type resolution.
2024-02-09 08:59:41 -08:00
Holly Borla
aa99cbc80e [Concurrency] Always diagnose invalid isolated parameter types in type resolution. 2024-02-08 23:15:50 -08:00
Finagolfin
8e7a5c6e53 [Test] Make sure macros are enabled for the tests that use the new #isolation macro 2024-01-28 02:20:07 +05:30
Holly Borla
917eacbee1 Merge pull request #71197 from hborla/isolated-parameter-sendable 2024-01-27 08:48:24 -08:00
Holly Borla
fef9a54242 [Concurrency] Don't consider callees with isolated parameters as exiting
to nonisolated unless the callee isolation is statically known to be
nonisolated.
2024-01-26 18:04:16 -08:00
John McCall
b0fb03d8c7 Create a uniform representation for function type isolation.
Not quite NFC because apparently the representation bleeds into what's
accepted in some situations where we're supposed to be warning about
conflicts and then making an arbitrary choice.  But what we're doing
is nonsense, so we definitely need to break behavior here.

This is setting up for isolated(any) and isolated(caller).  I tried
to keep that out of the patch as much as possible, though.
2024-01-25 22:11:01 -05:00
Holly Borla
a43779c396 [Concurrency] Handle #isolation in isolated argument checking. 2024-01-24 22:00:40 -08:00
Holly Borla
f2b088518c [Concurrency] Implement more precise actor isolation for isolated argument
values.

Teach ActorIsolation that a `nil` isolated argument is statically nonisolated,
and a reference to GlobalActor.shared statically has global actor isolation.

This change also models arbitrary actor instance isolation using VarDecls
when possible, which allows comparing two ActorIsolation values that may
represent different actor instances. Previously, ActorIsolation was
modeled only by storing the nominal actor type and the parameter index,
so the actor isolation value for two different actors was considered
to be equal. Now, the nominal actor type is only used for isolated `self`
in cases where there is no implicit self parameter decl, such as for
stored properties.
2024-01-24 20:24:34 -08:00
Konrad `ktoso` Malawski
14acfb20bd [Concurrency] isolated fixes; ban Array<T> and handle Self in actor (#70955) 2024-01-17 22:42:45 -08:00
Konrad `ktoso` Malawski
5a3cad992d [Concurrency] allow isolated on dynamic self types (#70925) 2024-01-17 12:16:33 +09:00
Holly Borla
f15ef1a055 [Concurrency] Allow isolated parameters to have optional type. 2024-01-09 21:37:24 -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
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
Kavon Farvardin
16af507d9e [Concurrency] tighten-up rules about isolated generic parameters
We were missing a check for conformance to `Actor` or `DistributedActor`
when an isolated parameter's type is a generic parameter.

Previously, if you used a generic parameter constrained to just `AnyActor`,
you'd crash the compiler in LowerHopToExecutor because it doesn't know
how to obtain the executor for such a value. Since `AnyActor` has no
`unownedExecutor` requirement, there's no way to get the executor without
emitting code to do dynamic casts down to `Actor` or `DistributedActor`.

Rather than have the compiler silently emit dynamic casting, I figured
it's best to ban it. This forces people to either do the dynamic casts
themselves, or use one of the more specific types to constrain their
parameter.

For other generic parameters, we would silently treat the function
as though it is nonisolated (i.e., as if the `isolated` wasn't written
on the parameter at all).

resolves rdar://109059544
2023-09-18 20:27:46 -07:00
Holly Borla
511add6727 [NFC][Concurrency] Add a test case for non-escaping, synchronous closures
used inside a function with an isolated parameter.
2023-09-03 12:32:50 -07:00
Holly Borla
68a4b314c9 [NFC][Concurrency] Add a test case for inheriting actor context within a
function that uses isolated parameters.
2023-09-03 12:22:42 -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
6a4c7891b8 [Concurrency] Diagnose non-sendable 'self' arguments crossing isolation boundaries. 2023-08-04 09:32:41 -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
3b3bdaf8d1 enforce correctness rules about isolated parameters
This covers function types, closures, and function declarations:

- only one `isolated` parameter
- no global-actor + `isolated` parameter
- no `nonisolated` + `isolated`.

all diagnostics are warnings until Swift 6
2022-11-11 16:00:12 -08:00
Konrad `ktoso` Malawski
e94aa362fa [Concurrency] Ban multiple isolated params, as per SE-0313 2022-11-10 12:18:53 -08:00
Doug Gregor
141ffc2b8d Model isolated parameters in ActorIsolation.
A function can be actor instance-isolated to one of its parameters.
Make sure that this is reflected in ActorIsolation, so such a function
doesn't get a different actor isolation.
2022-08-23 23:19:46 -07:00