Skip to content

Commit c6f4be2

Browse files
committed
[Concurrency] Eliminate "privileged" forms of ActorIsolation.
The "privileged" cases for actor instance and global actor isolation covered the case where the entity itself is within the particular actor but can be freely used from outside the actor, e.g., because it is asynchronous or an asynchronous handler. This is the wrong modeling for the problem, because it's only the first part of that---what the isolation of the particular entity is---that is needed for most clients. There will be a different abstraction for that.
1 parent 7758492 commit c6f4be2

File tree

5 files changed

+28
-52
lines changed

5 files changed

+28
-52
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,13 @@ class ActorIsolation {
4242
/// For example, a mutable stored property or synchronous function within
4343
/// the actor is isolated to the instance of that actor.
4444
ActorInstance,
45-
/// The declaration can refer to actor-isolated state, but can also be
46-
/// referenced from outside the actor.
47-
ActorPrivileged,
4845
/// The declaration is explicitly specified to be independent of any actor,
4946
/// meaning that it can be used from any actor but is also unable to
5047
/// refer to the isolated state of any given actor.
5148
Independent,
5249
/// The declaration is isolated to a global actor. It can refer to other
5350
/// entities with the same global actor.
5451
GlobalActor,
55-
/// The declaration can refer to state isolated by the given global actor,
56-
/// and can be used from anywhere.
57-
GlobalActorPrivileged,
5852
};
5953

6054
private:
@@ -78,18 +72,10 @@ class ActorIsolation {
7872
return ActorIsolation(Independent, nullptr);
7973
}
8074

81-
static ActorIsolation forActorPrivileged(ClassDecl *actor) {
82-
return ActorIsolation(ActorPrivileged, actor);
83-
}
84-
8575
static ActorIsolation forActorInstance(ClassDecl *actor) {
8676
return ActorIsolation(ActorInstance, actor);
8777
}
8878

89-
static ActorIsolation forGlobalActorPrivileged(Type globalActor) {
90-
return ActorIsolation(GlobalActorPrivileged, globalActor);
91-
}
92-
9379
static ActorIsolation forGlobalActor(Type globalActor) {
9480
return ActorIsolation(GlobalActor, globalActor);
9581
}
@@ -99,12 +85,12 @@ class ActorIsolation {
9985
operator Kind() const { return getKind(); }
10086

10187
ClassDecl *getActor() const {
102-
assert(getKind() == ActorInstance || getKind() == ActorPrivileged);
88+
assert(getKind() == ActorInstance);
10389
return actor;
10490
}
10591

10692
Type getGlobalActor() const {
107-
assert(getKind() == GlobalActor || getKind() == GlobalActorPrivileged);
93+
assert(getKind() == GlobalActor);
10894
return globalActor;
10995
}
11096

@@ -119,11 +105,9 @@ class ActorIsolation {
119105
return true;
120106

121107
case ActorInstance:
122-
case ActorPrivileged:
123108
return lhs.actor == rhs.actor;
124109

125110
case GlobalActor:
126-
case GlobalActorPrivileged:
127111
return areTypesEqual(lhs.globalActor, rhs.globalActor);
128112
}
129113
}

lib/AST/TypeCheckRequests.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,10 +1455,6 @@ void swift::simple_display(
14551455
out << "actor-isolated to instance of " << state.getActor()->getName();
14561456
break;
14571457

1458-
case ActorIsolation::ActorPrivileged:
1459-
out << "actor-privileged to instance of " << state.getActor()->getName();
1460-
break;
1461-
14621458
case ActorIsolation::Independent:
14631459
out << "actor-independent";
14641460
break;
@@ -1471,11 +1467,6 @@ void swift::simple_display(
14711467
out << "actor-isolated to global actor "
14721468
<< state.getGlobalActor().getString();
14731469
break;
1474-
1475-
case ActorIsolation::GlobalActorPrivileged:
1476-
out << "actor-privileged to global actor "
1477-
<< state.getGlobalActor().getString();
1478-
break;
14791470
}
14801471
}
14811472

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ class IsolationRestriction {
550550
case DeclKind::Accessor:
551551
case DeclKind::Func:
552552
case DeclKind::Subscript:
553+
// A function that provides an asynchronous context has no restrictions
554+
// on its access.
555+
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
556+
if (func->isAsyncContext())
557+
return forUnrestricted();
558+
}
559+
553560
// Local captures can only be referenced in their local context or a
554561
// context that is guaranteed not to run concurrently with it.
555562
if (cast<ValueDecl>(decl)->isLocalCapture())
@@ -565,10 +572,7 @@ class IsolationRestriction {
565572
return forGlobalActor(isolation.getGlobalActor());
566573

567574
case ActorIsolation::Independent:
568-
case ActorIsolation::ActorPrivileged:
569-
case ActorIsolation::GlobalActorPrivileged:
570-
// Actor-independent and actor-privileged declarations have no
571-
// restrictions on their access.
575+
// Actor-independent have no restrictions on their access.
572576
return forUnrestricted();
573577

574578
case ActorIsolation::Unspecified:
@@ -869,7 +873,6 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
869873
switch (auto contextIsolation =
870874
getInnermostIsolatedContext(getDeclContext())) {
871875
case ActorIsolation::ActorInstance:
872-
case ActorIsolation::ActorPrivileged:
873876
ctx.Diags.diagnose(
874877
loc, diag::global_actor_from_instance_actor_context,
875878
value->getDescriptiveKind(), value->getName(), globalActor,
@@ -878,7 +881,6 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
878881
return true;
879882

880883
case ActorIsolation::GlobalActor:
881-
case ActorIsolation::GlobalActorPrivileged:
882884
// If the global actor types are the same, we're done.
883885
if (contextIsolation.getGlobalActor()->isEqual(globalActor))
884886
return false;
@@ -973,7 +975,6 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
973975
switch (auto contextIsolation = getActorIsolation(
974976
cast<ValueDecl>(selfVar->getDeclContext()->getAsDecl()))) {
975977
case ActorIsolation::ActorInstance:
976-
case ActorIsolation::ActorPrivileged:
977978
// An escaping partial application of something that is part of
978979
// the actor's isolated state is never permitted.
979980
if (isEscapingPartialApply) {
@@ -1001,7 +1002,6 @@ void swift::checkActorIsolation(const Expr *expr, const DeclContext *dc) {
10011002
return true;
10021003

10031004
case ActorIsolation::GlobalActor:
1004-
case ActorIsolation::GlobalActorPrivileged:
10051005
// The 'self' is for a member that's part of a global actor, which
10061006
// means we cannot refer to actor-isolated state.
10071007
ctx.Diags.diagnose(
@@ -1081,12 +1081,6 @@ ActorIsolation ActorIsolationRequest::evaluate(
10811081
if (!globalActorType || globalActorType->hasError())
10821082
return ActorIsolation::forUnspecified();
10831083

1084-
// A function that is an asynchronous context is actor-privileged.
1085-
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
1086-
if (func->isAsyncContext())
1087-
return ActorIsolation::forGlobalActorPrivileged(globalActorType);
1088-
}
1089-
10901084
return ActorIsolation::forGlobalActor(globalActorType);
10911085
}
10921086

@@ -1104,13 +1098,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
11041098
// actor-isolated state.
11051099
auto classDecl = value->getDeclContext()->getSelfClassDecl();
11061100
if (classDecl && classDecl->isActor() && value->isInstanceMember()) {
1107-
// A function that is an asynchronous context is actor-privileged.
1108-
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
1109-
if (func->isAsyncContext())
1110-
return ActorIsolation::forActorPrivileged(classDecl);
1111-
}
1112-
1113-
// Everything else is part of the actor's isolated state.
1101+
// Part of the actor's isolated state.
11141102
return ActorIsolation::forActorInstance(classDecl);
11151103
}
11161104

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ static bool checkObjCActorIsolation(const ValueDecl *VD,
375375
// Check actor isolation.
376376
bool Diagnose = shouldDiagnoseObjCReason(Reason, VD->getASTContext());
377377

378+
// Asynchronous contexts can be @objc.
379+
// FIXME: Feels duplicative of TypeCheckConcurrency.
380+
if (auto func = dyn_cast<AbstractFunctionDecl>(VD)) {
381+
if (func->isAsyncContext())
382+
return false;
383+
}
384+
378385
switch (getActorIsolation(const_cast<ValueDecl *>(VD))) {
379386
case ActorIsolation::ActorInstance:
380387
// Actor-isolated functions cannot be @objc.
@@ -388,9 +395,9 @@ static bool checkObjCActorIsolation(const ValueDecl *VD,
388395
}
389396
return true;
390397

391-
case ActorIsolation::ActorPrivileged:
392398
case ActorIsolation::GlobalActor:
393-
case ActorIsolation::GlobalActorPrivileged:
399+
// FIXME: Consider whether to limit @objc on global-actor-qualified
400+
// declarations.
394401
case ActorIsolation::Independent:
395402
case ActorIsolation::Unspecified:
396403
return false;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,14 @@ void ConformanceChecker::resolveValueWitnesses() {
43404340
// Check for actor-isolation consistency.
43414341
switch (getActorIsolation(witness)) {
43424342
case ActorIsolation::ActorInstance: {
4343+
// Asynchronous contexts can be used to conform to protocol
4344+
// requirements.
4345+
// FIXME: Feels duplicative.
4346+
if (auto func = dyn_cast<AbstractFunctionDecl>(witness)) {
4347+
if (func->isAsyncContext())
4348+
break;
4349+
}
4350+
43434351
// Actor-isolated witnesses cannot conform to protocol requirements.
43444352
bool canBeAsyncHandler = false;
43454353
if (auto witnessFunc = dyn_cast<FuncDecl>(witness)) {
@@ -4364,8 +4372,6 @@ void ConformanceChecker::resolveValueWitnesses() {
43644372
break;
43654373
}
43664374

4367-
case ActorIsolation::ActorPrivileged:
4368-
case ActorIsolation::GlobalActorPrivileged:
43694375
case ActorIsolation::Independent:
43704376
case ActorIsolation::Unspecified:
43714377
break;

0 commit comments

Comments
 (0)