Skip to content

Commit e00ba43

Browse files
committed
[Concurrency] Implement basic actor isolation rules.
Implement a basic set of isolation rules for actors, which includes: * Asynchronous actor methods can be invoked on any actor instance. * All mutable instance properties and synchronous instance methods are only accessible via "self" or "super" within the context of the actor. * Closures defined within an actor method are considered to be a context distinct from the actor itself, are therefore are subject to the same restrictions as above. Together with a scheduler that ensures that asynchronous invocations for a particular actor are serialized (at least up until they hit a suspension point or return), this serves to isolate the actor's instance state in a shallow manner: references to shared mutable state can still be passed among actors, code can access global variables, and one can still form and pass around unsafe pointers. Pair this with some basic checking within actor contexts that identifies a few obvious potential kinds of race conditions: * References to captured mutable local variables from within closures. * References to mutable global and static/class variables. The latter are warnings, for which we currently don't have a great suppression mechanism. That, and a lot of tuning, are still to come.
1 parent 22a350b commit e00ba43

File tree

6 files changed

+554
-5
lines changed

6 files changed

+554
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,6 +4137,23 @@ ERROR(actor_without_concurrency,none,
41374137
ERROR(actor_with_nonactor_superclass,none,
41384138
"actor class cannot inherit from non-actor class %0", (DeclName))
41394139

4140+
ERROR(actor_isolated_non_self_reference,none,
4141+
"actor-isolated %0 %1 can only be referenced "
4142+
"%select{inside the actor|on 'self'}2",
4143+
(DescriptiveDeclKind, DeclName, bool))
4144+
WARNING(concurrent_access,none,
4145+
"%select{local|actor-isolated}0 %1 %2 is unsafe to reference in code "
4146+
"that may execute concurrently",
4147+
(bool, DescriptiveDeclKind, DeclName))
4148+
NOTE(actor_isolated_method,none,
4149+
"only asynchronous methods can be used outside the actor instance; "
4150+
"do you want to add 'async'?", ())
4151+
NOTE(actor_mutable_state,none,
4152+
"mutable state is only available within the actor instance", ())
4153+
WARNING(shared_mutable_state_access,none,
4154+
"reference to %0 %1 is not concurrency-safe because it involves "
4155+
"shared mutable state", (DescriptiveDeclKind, DeclName))
4156+
41404157
//------------------------------------------------------------------------------
41414158
// MARK: Type Check Types
41424159
//------------------------------------------------------------------------------

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,6 +4461,7 @@ void swift::performSyntacticExprDiagnostics(const Expr *E,
44614461
if (ctx.LangOpts.EnableObjCInterop)
44624462
diagDeprecatedObjCSelectors(DC, E);
44634463
diagnoseConstantArgumentRequirement(E, DC);
4464+
checkActorIsolation(E, DC);
44644465
}
44654466

44664467
void swift::performStmtDiagnostics(ASTContext &ctx, const Stmt *S) {

0 commit comments

Comments
 (0)