Skip to content

Commit 5bcd5a8

Browse files
committed
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.
1 parent 58f4969 commit 5bcd5a8

File tree

10 files changed

+470
-233
lines changed

10 files changed

+470
-233
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,31 +4349,27 @@ ERROR(override_implicit_unowned_executor,none,
43494349
"explicitly defined", ())
43504350

43514351
ERROR(actor_isolated_non_self_reference,none,
4352-
"actor-isolated %0 %1 can only be %select{referenced|mutated|used 'inout'}3 "
4353-
"%select{from inside the actor|on 'self'}2",
4354-
(DescriptiveDeclKind, DeclName, bool, unsigned))
4352+
"actor-isolated %0 %1 can not be "
4353+
"%select{referenced|mutated|used 'inout'}2 "
4354+
"%select{on a non-isolated actor instance|"
4355+
"from a Sendable function|from a Sendable closure|"
4356+
"from an 'async let' initializer|from global actor %4|"
4357+
"from the main actor|from a non-isolated context}3",
4358+
(DescriptiveDeclKind, DeclName, unsigned, unsigned, Type))
43554359
ERROR(distributed_actor_isolated_non_self_reference,none,
43564360
"distributed actor-isolated %0 %1 can only be referenced "
43574361
"inside the distributed actor",
43584362
(DescriptiveDeclKind, DeclName))
43594363
ERROR(distributed_actor_needs_explicit_distributed_import,none,
43604364
"'_Distributed' module not imported, required for 'distributed actor'",
43614365
())
4362-
ERROR(actor_isolated_self_independent_context,none,
4363-
"actor-isolated %0 %1 can not be %select{referenced|mutated|used 'inout'}2 from a "
4364-
"non-isolated context",
4365-
(DescriptiveDeclKind, DeclName, unsigned))
43664366
ERROR(actor_isolated_inout_state,none,
43674367
"actor-isolated %0 %1 cannot be passed 'inout' to"
43684368
"%select{| implicitly}2 'async' function call",
43694369
(DescriptiveDeclKind, DeclName, bool))
43704370
ERROR(actor_isolated_mutating_func,none,
43714371
"cannot call mutating async function %0 on actor-isolated %1 %2",
43724372
(DeclName, DescriptiveDeclKind, DeclName))
4373-
ERROR(actor_isolated_global_actor_context,none,
4374-
"actor-isolated %0 %1 can not be %select{referenced|mutated|used 'inout'}3 "
4375-
"from%select{| synchronous}4 context of global actor %2",
4376-
(DescriptiveDeclKind, DeclName, Type, unsigned, bool))
43774373
ERROR(global_actor_from_instance_actor_context,none,
43784374
"%0 %1 isolated to global actor %2 can not be %select{referenced|mutated|used 'inout'}4"
43794375
" from actor %3 %select{|in a synchronous context}5",
@@ -4398,15 +4394,6 @@ ERROR(actor_isolated_partial_apply,none,
43984394
ERROR(concurrent_access_local,none,
43994395
"use of local %0 %1 in concurrently-executing code",
44004396
(DescriptiveDeclKind, DeclName))
4401-
ERROR(actor_isolated_from_concurrent_closure,none,
4402-
"actor-isolated %0 %1 cannot be %select{referenced|mutated|used 'inout'}2 from a concurrent closure",
4403-
(DescriptiveDeclKind, DeclName, unsigned))
4404-
ERROR(actor_isolated_from_concurrent_function,none,
4405-
"actor-isolated %0 %1 cannot be %select{referenced|mutated|used 'inout'}2 from a concurrent function",
4406-
(DescriptiveDeclKind, DeclName, unsigned))
4407-
ERROR(actor_isolated_from_async_let,none,
4408-
"actor-isolated %0 %1 cannot be %select{referenced|mutated|used 'inout'}2 from 'async let' initializer",
4409-
(DescriptiveDeclKind, DeclName, unsigned))
44104397
ERROR(actor_isolated_keypath_component,none,
44114398
"cannot form key path to%select{| distributed}0 actor-isolated %1 %2",
44124399
(bool, DescriptiveDeclKind, DeclName))

lib/AST/Decl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,15 @@ ParamDecl *PatternBindingInitializer::getImplicitSelfDecl() const {
14201420
LazySelfParam->setImplicit();
14211421
LazySelfParam->setSpecifier(specifier);
14221422
LazySelfParam->setInterfaceType(DC->getSelfInterfaceType());
1423+
1424+
// Lazy members of actors have an isolated 'self', assuming there is
1425+
// no "nonisolated" attribute.
1426+
if (auto nominal = DC->getSelfNominalTypeDecl()) {
1427+
if (nominal->isActor() &&
1428+
!singleVar->getAttrs().hasAttribute<NonisolatedAttr>())
1429+
LazySelfParam->setIsolated();
1430+
}
1431+
14231432
mutableThis->SelfParam = LazySelfParam;
14241433
}
14251434
}
@@ -8312,6 +8321,7 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
83128321
auto selfDecl = isolation.getActorInstance();
83138322
auto actorClass = selfDecl->getType()->getRValueType()
83148323
->getClassOrBoundGenericClass();
8324+
// FIXME: Doesn't work properly with generics
83158325
assert(actorClass && "Bad closure actor isolation?");
83168326
return ActorIsolation::forActorInstance(actorClass);
83178327
}

0 commit comments

Comments
 (0)