Commit Graph

2898 Commits

Author SHA1 Message Date
Michael Gottesman
99a2db3392 Merge pull request #83410 from gottesmm/release/6.2-156919493
[6.2][concurrency] Emit nonisolated(nonsending) async throw initializers correctly
2025-07-31 09:41:12 -07:00
Pavel Yaskevich
c04eb7c482 [Concurrency] Downgrade errors when isolated member is referenced from a preconcurrency context
Such references used to be downgraded until Swift 6 but since the
context is `@preconcurrency` it should be possible for API authors
to introduce concurrency annotations such as `@Sendable` without
breaking clients even when they are compiled in Swift 6 mode.

Resolves: rdar://157061896
(cherry picked from commit 5ee673f358c9dd7dbbfe58f02bc78af60e0377b8)
2025-07-30 11:03:30 -07:00
Michael Gottesman
dcb3e5c48e [concurrency] Emit nonisolated(nonsending) async throw initializers correctly.
Specifically, we were not inserting the implicit isolated parameter and were not
setting up the actor prologue. To keep this specific to nonisolated(nonsending)
code, I only setup the actor prologue if we know that we have something that is
nonisolated(nonsending).

I also ported some async initializer tests to run with/without
nonisolated(nonsending) just to increase code coverage.

rdar://156919493
(cherry picked from commit 3871d22257)
2025-07-29 20:10:44 -07:00
John McCall
787cca9e37 Rewrite the type of nonisolated(nonsending) closures.
The constraint solver does not reliably give closures a function type
that includes `nonisolated(noncaller)`, even when the immediate context
requires a conversion to such a type. We were trying to work around this
in SILGen, but the peephole only kicked in if the types matched exactly,
so a contextual conversion that e.g. added `throws` was still emitting
the closure as `@concurrent`, which is of course the wrong semantics.
It's relatively easy to avoid all this by just rewriting the closure's
type to include `nonisolated(nonsending)` at a point where we can reliably
decide that, and then SILGen doesn't have to peephole anything for
correctness.

Fixes rdar://155313349
2025-07-28 22:22:54 -04:00
Max Desiatov
ab7cd238fd [6.2] test/CMakeLists.txt: run Embedded Swift for Wasm tests (#83287)
Cherry-pick of #83128, #82399, and #82878, merged as ea6ca2b5db, 0c4e56174b, and e34eb3331f respectively.

**Explanation**: Currently `test/CMakeLists.txt` can only set `SWIFT_LIT_ARGS` for all tests uniformly. This means that we can't have tests for Embedded Swift with a different set of `lit.py` arguments.

Also, create new `check-swift-embedded-wasi` target from `test/CMakeLists.txt`, tweak `lit.cfg` to support WASI Clang resource dir, exclude unsupported tests based on `CPU=wasm32` instead of `OS=wasi`.

**Scope**: Limited to Embedded Swift test suite.
**Risk**: Low, due to limited scope.
**Testing**: #82878 was incubated on `main` for 2 weeks, #82399 for 3 weeks with no disruption, #83128 merged this week, but enables all these tests on CI, which are consistently passing.
**Issue**: rdar://156585717
**Reviewer**: @bnbarham
2025-07-26 09:48:47 +01:00
Doug Gregor
20a10c618e [SE-0466] Treat explicit "nonisolated" like implicit "nonisolated" on protocols
Make explicit "nonisolated" also not special on protocols, so a
nonisolated protocol does not suppress default isolation.
SendableMetatype is the proper way to suppress default isolation for a
protocol.

Unfortunately, these rules made it appear like issue #82168
was fixed, when in fact it was not. Keep the test case, but as a
failing test, and we'll investigate separately.
2025-07-25 12:21:38 -07:00
Doug Gregor
5abbf2e4c9 [SE-0466] Under main actor default isolation, explicit nonisolated is not special
Given an explicitly-nonisolated type such as

    nonisolated struct S { }

all extensions of S were also being treated as nonisolated. This meant
that being implicitly nonisolated (i.e., when you're using nonisolated
default isolation) was different from explicitly-writing nonisolated,
which is unfortunate and confusing. Align the rules, such that an
extension of S will get default isolation:

    extension S {
      func f() { } // @MainActor if we're in main actor default isolation
    }
2025-07-25 12:21:35 -07:00
Doug Gregor
4ffe06ed7c [SE-0466] Extensions and members thereof can apply default isolation
Extensions of nonisolated types can get default isolation from context
(e.g., @MainActor) independently. This was previously true for members,
but not conformances, and recently regressed for members. Fix the logic
to consistently allow such extensions to be default main-actor
isolated, which affects both their members and conformances.

Fixes rdar://156644976.
2025-07-25 12:21:08 -07:00
Doug Gregor
2db51c9159 Merge pull request #82923 from DougGregor/sendable-prohibits-mainactor-by-default-6.2
[6.2] [SE-0466] Enable SendableProhibitsMainActorInference by default
2025-07-23 20:04:56 -07:00
Holly Borla
4518bc9dcf Merge pull request #83254 from hborla/6.2-local-decl-isolation-alt
[6.2][Concurrency] Only apply default main actor to local and nested decls that are in a main actor isolated context.
2025-07-23 11:12:34 -07:00
Holly Borla
276e5de462 [Concurrency] Local storage is always default nonisolated.
(cherry picked from commit 7b0ba7b5ed)
2025-07-22 21:36:59 -07:00
Holly Borla
2697773f21 [Concurrency] Only apply default main actor to local and nested
decls that are in a main actor isolated context.

This prevents `@MainActor` from being inferred in a context where
it cannot be used, while still allowing main actor code to be used
in local contexts that are also main actor isolated.

(cherry picked from commit 858e145069)
2025-07-22 21:15:26 -07:00
Konrad 'ktoso' Malawski
835d55f45a [test] startImmediately was missing explicit plugins path 2025-07-22 17:52:02 +09:00
Pavel Yaskevich
61157cdd36 Merge pull request #83165 from xedin/nonisolated-nonseding-with-special-decls-6.2
[6.2][Concurrency] NonisolatedNonsendingByDefault: Infer `nonisolated(nonsending)` on declaration with special special semantics
2025-07-18 09:40:08 -07:00
Michael Gottesman
64a53b483e Merge pull request #83084 from gottesmm/release/6.2-rdar155905383
[6.2][concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
2025-07-17 22:04:17 -07:00
Pavel Yaskevich
fb0daaa78a [Concurrency] NonisolatedNonsendingByDefault: Extend nonisolated(nonsending) to _openExistential
`_openExistential` is type-checked in a special way which
means that we need to explicitly inject `nonisolated(nonsending)`
isolation when forming a reference to this builtin.

(cherry picked from commit 358869ff54)
2025-07-17 18:08:30 -07:00
Pavel Yaskevich
18817b823f [Concurrency] NonisolatedNonsendingByDefault: Extend nonisolated(nonsending) to withoutActuallyEscaping
`withoutActuallyEscaping` is type-checked in a special way which
means that we need to explicitly inject `nonisolated(nonsending)`
isolation when forming a reference to this builtin.

(cherry picked from commit 48f4d7b688)
2025-07-17 18:08:30 -07:00
Michael Gottesman
82b0ec3de0 [silgen] Make sure that thunks that convert to/from nonisolated(nonsending) handle hopping correctly.
Specifically:

1. When we convert a function to nonisolated(nonsending), we need to
make sure that in the thunk we hop upon return since nonisolated(nonsending)
functions are assumed to preserve the caller's isolation.

2. When we convert a function from nonisolated(nonsending), we need to
make sure that in the thunk we hop onto the actor that we are passing in as the
isolated parameter of the nonisolated(nonsending) function. This ensures that
the nonisolated(nonsending) function can assume that it is already on its
isolated parameter's actor at function entry.

rdar://155905383
2025-07-17 10:55:59 -07:00
Holly Borla
94df8da676 [Concurrency] Downgrade isolated conformances to SendableMetatype
protocols to a warning for implicitly `@preconcurrency` protocols.

(cherry picked from commit 4b50238ebf)
2025-07-17 10:28:27 -07:00
Anthony Latsis
ffa408d9b8 Merge pull request #83110 from swiftlang/jepa-release
[6.2] Sema: Make `@concurrent` not imply `async` on closures
2025-07-17 16:44:44 +01:00
Yuta Saito
0a5a694b46 Merge pull request #83120 from kateinoigakukun/yt/cherry-pick-6.2-gh83086
[6.2][Concurrency] Fix runtime isolated-conformance checks with static stdlib
2025-07-17 22:00:15 +09:00
Michael Gottesman
7f9eb4ccb1 Merge pull request #83102 from gottesmm/release/6.2-revert
[6.2] Revert nonisolated(unsafe) closure inference
2025-07-16 22:03:39 -07:00
Yuta Saito
6b2ade5d99 [6.2] Skip a part of isolated_conformance.swift on WASI
Custom global executor is not landing in 6.2, so we skip the test
by just gating it behind the platform check.
2025-07-17 04:20:13 +00:00
Yuta Saito
500e25ced1 [Concurrency] Unskip isolated-conformance runtime test on WebAssembly 2025-07-16 23:50:35 +00:00
Anthony Latsis
dc1458762e Sema: Make @concurrent not imply async on closures
The present approach is not prudent because `@concurrent` synchronous
functions, a natural extension, are a likely-to-happen future direction,
whereas the current inference rule is entirely grounded on `@concurrent`
being exclusive to async functions.

If we were to ship this rule, we would have to keep the promise for
backwards compatibility when implementing the aforementioned future
direction, replacing one inconsistency with another, and possibly
introducing new bug-prone expression checking code.

```swift
func foo(_: () -> Void) {}
func foo(_: () async -> Void) {}

// In a future without this change and  `@concurrent` synchronous
// functions accepted, the first call resolves to the first overload,
// and the second call resolves to the second, despite `@concurrent` no
// longer implying `async`.
foo { }
foo { @concurrent in }
```

This change also drops the fix-it for removing `@concurrent` when used
on a synchronous closure. With the inference rule gone, and the
diagnosis delayed until after solution application, this error raises a
fairly balanced choice between removing the attribute and being
explicit about the effect, where a unilateral suggestion is quite
possibly more harmful than useful.

(cherry picked from commit 58d5059617)
2025-07-16 20:49:37 +01:00
Michael Gottesman
348720b830 Revert "[rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe)."
This reverts commit 6ce93c8a37.
2025-07-16 09:58:09 -07:00
Michael Gottesman
e81838f672 Revert "[region-isolation] Add a test showing that we do not infer nonisolated(unsafe) onto named closures."
This reverts commit 4ccb39e943.
2025-07-16 09:58:08 -07:00
Michael Gottesman
2e06769dbc Revert "Fix an error message due to a difference in Task.detached from 6.2 and main."
This reverts commit b81f6bbcda.
2025-07-16 09:58:06 -07:00
Doug Gregor
73530bf957 Merge pull request #83088 from DougGregor/se-0466-nested-no-inference-6.2
[6.2] [SE-0466] Nested types of nonisolated types don't infer main-actor isolation
2025-07-16 09:44:00 -07:00
Konrad `ktoso` Malawski
3fbb1958ef CI: Temporarily disable taskExecutor test with Task.immediate
rdar://155596073 Task executors execution may not always hop as expected

I'm investigating a fix here as we speak, but disabling the specific piece of the test while we work on it.
2025-07-16 21:16:33 +09:00
Doug Gregor
a4a39f2a98 [SE-0466] Nested types of nonisolated types don't infer main-actor isolation
Under discussion as part of an amendment to SE-0466, limit default main
actor inference so it doesn't apply to a nested type within a
nonisolated type.
2025-07-15 20:15:35 -07:00
Pavel Yaskevich
d7808f7804 Merge pull request #83064 from xedin/rdar-155847011-6.2
[6.2][Concurrency] Replace `nonisolated` with `nonisolated(nonsending)` when isolation is inferred
2025-07-15 17:41:06 -07:00
Michael Gottesman
182149978e [concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
Specifically for 6.2, we are making optimize hop to executor more conservative
around caller isolation inheriting functions. This means that we are:

1. No longer treating calls to caller isolation inheriting functions as having a
hop in their prologue. In terms of this pass, it means that when determining
dead hop to executors, we no longer think that a caller isolation inheriting
function means that an earlier hop to executor is not required.

2. Treating returns from caller isolation inheriting callees as requiring a
hop. The reason why we are doing this is that we can no longer assume that our
caller will hop after we return.

Post 6.2, there are three main changes we are going to make:

* Forward Dataflow

Caller isolation inheriting functions will no longer be treated as suspension
points meaning that we will be able to propagate hops over them and can assume
that we know the actor that we are on when we enter the function. Practically
this means that trees of calls that involve just nonisolated(nonsending) async
functions will avoid /all/ hop to executor calls since we will be able to
eliminate all of them since the dataflow will just propagate forward from the
entrance that we are already on the actor.

* Backwards Dataflow

A caller isolation inheriting call site will still cause preceding
hop_to_executor functions to be live. This is because we need to ensure that we
are on the caller isolation inheriting actor before we hit the call site. If we
are already on that actor, the hop will be eliminated by the forward pass. But
if the hop has not been eliminated, then the hop must be needed to return us to
the appropriate actor.

We will also keep the behavior that returns from a caller isolation inheriting
function are considered to keep hop to executors alive. If we were able to
propagate to a hop to executor before the return inst with the forward dataflow,
then we know that we are guaranteed to still be on the relevant actor. If the
hop to executor is still there, then we need it to ensure that our caller can
treat the caller isolation inheriting function as a non-suspension point.

rdar://155905383
(cherry picked from commit b3942424c8)
2025-07-15 17:32:19 -07:00
Pavel Yaskevich
a7e44d422f Merge pull request #83048 from xedin/rdar-153487603-6.2
[6.2][Serialization] Move `nonisolated(nonsending)` isolation kind above g…
2025-07-15 11:20:22 -07:00
Pavel Yaskevich
84f70f3792 [Concurrency] Replace nonisolated with nonisolated(nonsending) when isolation is inferred
With `NonisolatedNonsendingByDefault` an explicit `nonisolated`
attribute in declaration context is inferred to mean `nonisolated(nonsending)`
and it should be printed as such in interface files and other places.

The inference logic that did didn't remove the original attribute
which meant that it would be printed twice i.e.
`nonisolated nonisolated(nonsending) func test() async` which is
incorrect and would fail swift interface validation.

Resolves: rdar://155847011
(cherry picked from commit b519c07428)
2025-07-15 09:20:33 -07:00
Doug Gregor
e5658e0087 Merge pull request #83020 from DougGregor/prohibit-isolated-conformance-capture-check-6.2 2025-07-14 15:37:31 -07:00
Pavel Yaskevich
64c8c3d5f6 [Serialization] Move nonisolated(nonsending) isolation kind above global actor one
Global actor kind also appends type offset that indicates what
global actor to use with the type. All of the isolation kinds
should be placed above it to make sure that there is never a
clash when i.e. `MainActor` is serialized as id `1`.

Resolves: rdar://153487603
(cherry picked from commit ace6f738ba)
2025-07-14 14:33:28 -07:00
Michael Gottesman
187b1eedf7 Merge pull request #82776 from gottesmm/release/6.2-154139237
[6.2] [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
2025-07-14 13:19:55 -07:00
Alastair Houghton
eefdd69c8d Merge pull request #82915 from al45tair/eng/PR-151147606-take2
[Concurrency] Remove custom global executors from 6.2.
2025-07-14 16:20:57 +01:00
Pavel Yaskevich
c4a3d31ad0 Merge pull request #82998 from xedin/rdar-155589753-6.2
[6.2][Concurrency] Don't downgrade explicit isolation attribute clashes
2025-07-13 08:22:58 -07:00
Doug Gregor
74b54fea5c Use the "prohibits isolated conformances" check for capture metatypes
When checking whether a capture of a metatype is Sendable, we had an
incomplete and incorrect check for SendableMetatype conformance.
Replace that with the proper "prohibits isolated conformances" check
on the generic signature, which matches what we do on the caller side.

Fixes issue #82905 / rdar://155399531.
2025-07-12 08:47:14 -07:00
Pavel Yaskevich
4e93e74898 [Concurrency] Don't downgrade explicit isolation attribute clashes
The compiler shouldn't accept mismatch in explicit isolation attributes
because it could lead to incorrect isolation selection.

Resolves: rdar://155589753

(cherry picked from commit a9373c0f3f)
2025-07-10 22:29:20 -07:00
Konrad `ktoso` Malawski
ce0d16652c add newline at end of startImmediately.swift file 2025-07-11 09:52:42 +09:00
Michael Gottesman
1f9e30276a [rbi] Teach SendNonSendable how to more aggressively suppress sending errors around obfuscated Sendable functions
Specifically the type checker to work around interface types not having
isolation introduces casts into the AST that enrich the AST with isolation
information. Part of that information is Sendable. This means that we can
sometimes lose due to conversions that a function is actually Sendable. To work
around this, we today suppress those errors when they are emitted (post 6.2, we
should just change their classification as being Sendable... but I don't want to
make that change now).

This change just makes the pattern matching for these conversions handle more
cases so that transfernonsendable_closureliterals_isolationinference.swift now
passes.
2025-07-10 17:25:01 -07:00
Michael Gottesman
35c3b9e12c [concurrency] Perform some fixes so that transfernonsendable_closureliterals_isolationinference.swift now passes.
The reason why this failed is that concurrently to @xedin landing
79af04ccc4, I enabled
NonisolatedNonsendingByDefault on a bunch of other tests. That change broke the
test and so we needed to fix it.

This commit fixes a few issues that were exposed:

1. We do not propagate nonisolated(nonsending) into a closure if its inferred
context isolation is global actor isolated or if the closure captures an
isolated parameter. We previously just always inferred
nonisolated(nonsending). Unfortunately since we do not yet have capture
information in CSApply, this required us to put the isolation change into
TypeCheckConcurrency.cpp and basically have function conversions of the form:

```
(function_conversion_expr type="nonisolated(nonsending) () async -> Void"
   (closure_expr type="() async -> ()" isolated_to_caller_isolation))
```

Notice how we have a function conversion to nonisolated(nonsending) from a
closure expr that has an isolation that is isolated_to_caller.

2. With this in hand, we found that this pattern caused us to first thunk a
nonisolated(nonsending) function to an @concurrent function and then thunk that
back to nonisolated(nonsending), causing the final function to always be
concurrent. I put into SILGen a peephole that recognizes this pattern and emits
the correct code.

3. With that in hand, we found that we were emitting nonisolated(nonsending)
parameters for inheritActorContext functions. This was then fixed by @xedin in

With all this in hand, closure literal isolation and all of the other RBI tests
with nonisolated(nonsending) enabled pass.

rdar://154969621
(cherry picked from commit 648bb8fe30)
2025-07-10 17:24:48 -07:00
Konrad 'ktoso' Malawski
fba13f8eda [Concurrency] Add missing Task.immediateDetached, which drops task locals 2025-07-11 08:45:08 +09:00
Konrad 'ktoso' Malawski
6f3099b564 [Concurrency] a few missing overloads for immediate tasks 2025-07-11 08:45:03 +09:00
Michael Gottesman
85fafa5b1e [rbi] Begin tracking if a function argument is from a nonisolated(nonsending) parameter and adjust printing as appropriate.
Specifically in terms of printing, if NonisolatedNonsendingByDefault is enabled,
we print out things as nonisolated/task-isolated and @concurrent/@concurrent
task-isolated. If said feature is disabled, we print out things as
nonisolated(nonsending)/nonisolated(nonsending) task-isolated and
nonisolated/task-isolated. This ensures in the default case, diagnostics do not
change and we always print out things to match the expected meaning of
nonisolated depending on the mode.

I also updated the tests as appropriate/added some more tests/added to the
SendNonSendable education notes information about this.

(cherry picked from commit 14634b6847)
2025-07-09 12:25:03 -07:00
Michael Gottesman
065ffa3b72 [rbi] Convert all rbi tests to run also in NonisolatedNonsendingByDefault.
Going to update the tests in the next commit. This just makes it easier to
review.

(cherry picked from commit a6edf4fb90)
2025-07-09 12:24:17 -07:00
Michael Gottesman
f24ff98f9a [nonisolated-nonsending] Make the AST not consider nonisolated(nonsending) to be an actor isolation crossing point.
We were effectively working around this previously at the SIL level. This caused
us not to obey the semantics of the actual evolution proposal. As an example of
this, in the following, x should not be considered main actor isolated:

```swift
nonisolated(nonsending) func useValue<T>(_ t: T) async {}
@MainActor func test() async {
  let x = NS()
  await useValue(x)
  print(x)
}
```

we should just consider this to be a merge and since useValue does not have any
MainActor isolated parameters, x should not be main actor isolated and we should
not emit an error here.

I also fixed a separate issue where we were allowing for parameters of
nonisolated(nonsending) functions to be passed to @concurrent functions. We
cannot allow for this to happen since the nonisolated(nonsending) parameters
/could/ be actor isolated. Of course, we have lost that static information at
this point so we cannot allow for it. Given that we have the actual dynamic
actor isolation information, we could dynamically allow for the parameters to be
passed... but that is something that is speculative and is definitely outside of
the scope of this patch.

rdar://154139237
(cherry picked from commit c12c99fb73)
2025-07-09 12:24:17 -07:00