Commit Graph

51 Commits

Author SHA1 Message Date
Michael Gottesman
81885a67d0 [silgen] Change two places we used Builtin.Executor to instead use Optional<any Actor> as an expected executor.
We want SILGen to have a simplified view of its executor and know that whenever
one sees an Actor, it is an actual actor instead of a Builtin.Executor. This
just simplifies code. Also, we should eventually have an invariant that
Builtin.Executor should only be allowed in LoweredSIL after LowerHopToExecutor
has run. But that is a change for another day.
2025-10-16 10:51:13 -07:00
John McCall
5fb314b475 At the SIL level, allow synchronous functions to be nonisolated(nonsending). 2025-09-09 14:26:57 -04:00
Pavel Yaskevich
53ef38e31b [AST] NFC: Rename function type isolation NonisolatedCaller to NonisolatedNonsending
This reduces the number of ways we refer to caller isolated async
functions and matches the name to the attribute spelling.
2025-08-05 17:22:10 -07:00
Allan Shortlidge
2d7884378a SILGen: Stop using AvailabilityInference in SILGenConcurrency.cpp.
NFC.
2025-07-21 17:02:21 -07:00
Michael Gottesman
0da22388cb [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-18 10:08:21 -07:00
John McCall
0f919f1d68 Merge pull request #82867 from rjmccall/dont-capture-defer-isolation
Don't force a capture of an isolated parameter in defer bodies.
2025-07-08 11:58:18 -04:00
John McCall
e30728c5a6 Don't force a capture of an isolated parameter in defer bodies.
SILGen already has an exception for this from -enable-actor-data-race-checks,
so there's no need for it, and it causes problems in actor inits.

Fixes rdar://155239032
2025-07-07 23:29:04 -04:00
Konrad 'ktoso' Malawski
4a5ccf04a7 [Concurrency] Use ASSERT for to also assert in release builds
Small cleanup from code review; we'd crash either way after this line,
so might be better to always ASSERT more nicely.
2025-07-08 10:03:41 +09:00
John McCall
3439e0caab Fix a bunch of bugs with the isolation of local funcs. Since we
use local funcs to implement `defer`, this also fixes several
bugs with that feature, such as it breaking in nonisolated
functions when a default isolation is in effect in the source file.

Change how we compute isolation of local funcs. The rule here is
supposed to be that non-`@Sendable` local funcs are isolated the
same as their enclosing context. Unlike closure expressions, this
is unconditional: in instance-isolated functions, the isolation
does not depend on whether `self` is captured. But the computation
was wrong: it didn't translate global actor isolation between
contexts, it didn't turn parameter isolation into capture isolation,
and it fell through for several other kinds of parent isolation,
causing the compiler to try to apply default isolation instead.
I've extracted the logic from the closure expression path into a
common function and used it for both paths.

The capture computation logic was forcing a capture of the
enclosing isolation in local funcs, but only for async functions.
Presumably this was conditional because async functions need the
isolation for actor hops, but sync functions don't really need it.
However, this was causing crashes with `-enable-actor-data-race-checks`.
(I didn't investigate whether it also failed with the similar
assertion we do with preconcurrency.) For now, I've switched this
to capture the isolated instance unconditionally. If we need to
be more conservative by either only capturing when data-race checks
are enabled or disabling the checks when the isolation isn't captured,
we can look into that.

Fix a bug in capture isolation checking. We were ignoring captures
of nonisolated declarations in order to implement the rule that
permits `nonisolated(unsafe)` variables to be captured in
non-sendable closures. This check needs to only apply to variables!
The isolation of a local func has nothing to do with its sendability
as a capture.

That fix exposed a problem where we were being unnecessarily
restrictive with generic local func declarations because we didn't
consider them to have sendable type. This was true even if the
genericity was purely from being declared in a generic context,
but it doesn't matter, they ought to be sendable regardless.

Finally, fix a handful of bugs where global actor types were not
remapped properly in SILGen.
2025-06-29 01:23:04 -04:00
John McCall
2eee30dfbe [NFC] Encapsulate the parameter index of an ActorIsolation 2025-06-27 19:48:12 -04:00
Anthony
c9b17383c8 Grammatical corrections for compound modifiers 2025-04-24 09:21:32 +02:00
Pavel Yaskevich
bb62e961b8 [SILGen] Concurrency: Fix caller isolated closures to hop to the right actor
Closures that are caller isolated used to still hop to the generic
executor, which is incorrect.
2025-04-16 18:21:44 -07:00
Doug Gregor
731f58443c Address review feedback on AbstractConformance in ProtocolConformanceRef 2025-03-23 20:54:39 -07:00
Erik Eckstein
d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Michael Gottesman
3ef18eaf1d Merge pull request #79300 from gottesmm/pr-12bede947a34719b34b1aa8ea6c126a6126d6dd6
[concurrency] Implement serialization for execution(caller)/execution(concurrent).
2025-02-19 18:29:02 -08:00
Pavel Yaskevich
cdb5396c00 [AST] Declare NonIsolatedCaller as kind of FunctionTypeIsolation
This would make sure that async function types marked as `@execution(caller)`
have correct isolation.

Also defines all of the possible conversions to and from `caller`
isolated function types.
2025-02-13 14:35:54 -08:00
Michael Gottesman
9851305d6e [concurrency] Make sure both with and without the flag, we codegen execution(caller|concurrent) correctly.
The flag is specifically NonIsolatedAsyncInheritsIsolationFromContext.

I noticed that we were not codegening @execution(caller) when the flag was
disabled, so I fixed it and added this test.
2025-02-12 10:38:06 -08:00
Michael Gottesman
7e350bb4ce Revert "[concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated."
This reverts commit 0cb64638d0.
2025-02-06 14:05:06 -08:00
Michael Gottesman
f05f08c2c0 Revert "[concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting."
This reverts commit 543b1e6ca3.
2025-02-06 14:04:30 -08:00
Michael Gottesman
543b1e6ca3 [concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting. 2025-02-03 10:56:08 -08:00
Michael Gottesman
0cb64638d0 [concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated.
This is just the first part of a larger transition.
2025-02-03 10:56:06 -08:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Michael Gottesman
56f38c4172 [concurrency] Add support in SILGen/SIL for emitting and calling implicit leading parameters for CallerInheritingIsolation isolation.
This does not change region isolation yet to recognize these as effectively
nonisolated.
2025-01-02 13:18:54 -08:00
Michael Gottesman
5d4239af57 [concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases
like constructors/destructors where we are pretty sure we want to not support
this.

This is part of my bringup strategy for changing nonisolated/unspecified to be
caller isolation inheriting.
2025-01-02 13:18:30 -08:00
Michael Gottesman
a15f52009e [gardening] Use a higher level method that describes what a piece of code is doing instead of source inlining it. 2025-01-02 13:18:30 -08:00
Slava Pestov
35b78c950b SILGen: Remove usage of weird ProtocolConformanceRef() constructor 2024-11-16 16:16:06 -05: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
John McCall
b603c3c4e4 Emit a null check before asserting isolation in a function with optional isolation.
Fixes rdar://132478429
2024-10-30 14:41:50 -07:00
Allan Shortlidge
7819dd7833 AST/Sema: Remove unnecessary ASTContext parameters from availability APIs.
Many of the methods on `AvailabilityInference` take both a `Decl` and an
`ASTContext`, which is redundant.
2024-10-14 17:46:53 -07:00
John McCall
af8115ffa3 Hop to the current isolation properly in delegating async actor initializers.
This requires two major changes.

The first is that we need to teach SILGen that the isolation of an initializer
is essentially dynamic (as far as SILGen is concerned) --- that it needs to emit
code in order to get the isolation reference.  To make this work, I needed to
refactor how we store the expected executor of a function so that it's not
always a constant value; instead, we'll need to emit code that DI will lower
properly.  Fortunately, I can largely build on top of the work that Doug previously
did to support #isolation in these functions.  The SIL we emit here around delegating
initializer calls is not ideal --- the breadcrumb hop ends up jumping to the
generic executor, and then DI actually emits the hop to the actor.  This is a little
silly, but it's hard to eliminate without special-casing the self-rebinding, which
honestly we should consider rather than the weirdly global handling of that in
SILGen today.  The optimizer should eliminate this hop pretty reliably, at least.

The second is that we need to teach DI to handle the pattern of code we get in
delegating initializers, where the builtin actually has to be passed the self var
rather than a class reference.  This is because we don't *have* a class reference
that's consistently correct in these cases.  This ended up being a fairly
straightforward generalization.

I also taught the hop_to_executor optimizer to skip over the initialization of
the default-actor header; there are a lot of simple cases where we still do emit
the prologue generic-executor hop, but at least the most trivial case is handled.
To do this better, we'd need to teach this bit of the optimizer that the properties
of self can be stored to in an initializer prior to the object having escaped, and
we don't have that information easily at hand, I think.

Fixes rdar://87485045.
2024-10-04 22:23:00 -04:00
Allan Shortlidge
02dbb96b94 AST: Rename AvailabilityContext to AvailabilityRange.
The generality of the `AvailabilityContext` name made it seem like it
encapsulates more than it does. Really it just augments `VersionRange` with
additional set algebra operations that are useful for availability
computations. The `AvailabilityContext` name should be reserved for something
pulls together more than just a single version.
2024-09-13 16:25:18 -07:00
Pavel Yaskevich
080675170b [SILGen] Emit dynamic actor isolation checks for closures
For synchronous isolated closures passed to unsafe APIs
(the ones that have not been fully concurrency checked)
emit an expected executor check in prolog that would make
sure that they are always used in the expected context.

Resolves: rdar://133415157
2024-08-22 09:57:17 -07:00
Slava Pestov
ff308e9510 AST: Remove TypeBase::openAnyExistentialType() 2024-08-20 12:15:27 -04:00
Slava Pestov
0c2f28fd3d AST: Remove GenericSignature parameter from OpenedArchetypeType::get() 2024-08-20 12:15:27 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
977b444eb3 AST: Add a new overload of getContextSubstitutionMap() 2024-07-10 13:28:26 -04:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Konrad `ktoso` Malawski
168bc7b454 [Concurrency] Fix how we obtain DA-as-A conformance for cross module
Resolves rdar://127206143
2024-05-28 13:37:21 +09:00
Doug Gregor
39f4e38027 Diagnose inout uses of 'lets' in constructors in the type checker.
Stored `let` properties of a struct, class, or actor permit
'inout' modification within the constructor body after they have been
initialized. Tentatively remove this rule, only allowing such `let`
properties to be initialized (assigned to) and not treated as `inout`.

Fixes rdar://127258363.
2024-05-14 15:59:50 -07:00
Slava Pestov
05bcc25f7b SIL: Local functions should inherit isolation from context
If the outer context is actor isolated, we need to force a capture the
actor variable, since otherwise it might not appear in the capture list.
2024-04-18 23:26:17 -04:00
John McCall
e7765ad090 Map the isolated variable type into context correctly.
The fix for #72484 didn't properly handle the case of a "generic" context
where all the type parameters had concrete assignments.  In this situation,
F.mapTypeIntoContext does not work because the function has no generic
environment.

Fixes rdar://126085573
2024-04-16 19:31:38 -04:00
Doug Gregor
14fc015d51 Ensure that we map the capture isolation into context
Failing to map the type of an isolation capture into context
caused assertions in type lowering. Fixes the build of the
swift-distributed-actors package.
2024-03-25 17:13:33 -07:00
Pavel Yaskevich
8e93ae9775 [SILGen] Don't emit executor references when '_checkExpectedExecutor' is unavailable
Avoid emitting an unused executor reference for dynamic actor
isolation checking.
2024-03-08 13:04:48 -08:00
Pavel Yaskevich
f977996807 [SILGen] Emit _checkExpectedExecutor only if its available on a deployment target
Don't emit `_checkExpectedExecutor` check if deployment target is
not sufficiently new otherwise it would result in crash.

Resolves: rdar://106827064
2024-03-07 16:35:36 -08:00
John McCall
43b9b28797 Fix a pair of bugs with @isolated(any) closure erasure.
First, and I really should've checked this, but global actors do not
require `shared` to return `Self`; adjust the logic to propagate the
right formal type to the erasure logic.

Second, handle attempts to erase the isolation of something isolated to
a distributed actor using the magic Actor conformance that Doug added.
This only works when the actor is local, but it shouldn't be difficult to
enforce that we only attempt to erase isolation what that's true --- we
need to prevent partial application of distributed actors, and we need to
disallow explicit isolated captures of distributed actors when we're not
currently isolated to it.  Otherwise, it's not possible to get an
@isolated(any) function value with an isolation that isn't the current
function's isolation, which means it always has to be local.

Fixed rdar://123734019
2024-03-04 04:15:21 -05:00
John McCall
7b3415aa23 Properly erase closure isolation to @isolated(any).
We do this by pushing the conversion down to the emission of the
closure expression, then teaching closure emission to apply the isolation
to the closure.  Ideally, we combine the isolation along with the rest of
the conversion peephole, but if necessary, we make sure we emit the
isolation.
2024-02-26 22:50:58 -05:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
John McCall
868fc6ad46 Basic SILGen for @isolated(any).
The main piece that's still missing here is support for closures;
they actually mostly work, but they infer the wrong isolation for
actor-isolated closures (it's not expressed in the type, so obviously
they're non-isolated), so it's not really functional.  We also have
a significant problem where reabstraction thunks collide incorrectly
because we don't mangle (or represent!) formal isolation into
SILFunctionType; that's another follow-up.  Otherwise, I think SILGen
is working.
2024-02-19 21:21:03 -05:00