Skip to content

Commit 4e96cef

Browse files
committed
Centralize logic for the actor isolation of a context
1 parent 581e666 commit 4e96cef

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

lib/AST/Decl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8023,6 +8023,25 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
80238023
return getActorIsolation(var);
80248024
}
80258025

8026+
if (auto *closure = dyn_cast<AbstractClosureExpr>(dc)) {
8027+
switch (auto isolation = closure->getActorIsolation()) {
8028+
case ClosureActorIsolation::Independent:
8029+
return ActorIsolation::forIndependent(ActorIndependentKind::Safe);
8030+
8031+
case ClosureActorIsolation::GlobalActor: {
8032+
return ActorIsolation::forGlobalActor(isolation.getGlobalActor());
8033+
}
8034+
8035+
case ClosureActorIsolation::ActorInstance: {
8036+
auto selfDecl = isolation.getActorInstance();
8037+
auto actorClass = selfDecl->getType()->getRValueType()
8038+
->getClassOrBoundGenericClass();
8039+
assert(actorClass && "Bad closure actor isolation?");
8040+
return ActorIsolation::forActorInstance(actorClass);
8041+
}
8042+
}
8043+
}
8044+
80268045
return ActorIsolation::forUnspecified();
80278046
}
80288047

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,63 +1385,18 @@ namespace {
13851385
return ClosureActorIsolation::forIndependent();
13861386

13871387
// A non-escaping closure gets its isolation from its context.
1388-
Optional<ActorIsolation> parentIsolation;
1389-
auto parentDC = closure->getParent();
1390-
switch (parentDC->getContextKind()) {
1391-
case DeclContextKind::AbstractClosureExpr: {
1392-
auto parentClosureIsolation = cast<AbstractClosureExpr>(parentDC)
1393-
->getActorIsolation();
1394-
switch (parentClosureIsolation) {
1395-
case ClosureActorIsolation::Independent:
1396-
parentIsolation = ActorIsolation::forIndependent(
1397-
ActorIndependentKind::Safe);
1398-
break;
1399-
1400-
case ClosureActorIsolation::ActorInstance: {
1401-
auto selfDecl = parentClosureIsolation.getActorInstance();
1402-
auto actorClass = selfDecl->getType()->getRValueType()
1403-
->getClassOrBoundGenericClass();
1404-
assert(actorClass && "Bad closure actor isolation?");
1405-
parentIsolation = ActorIsolation::forActorInstance(actorClass);
1406-
break;
1407-
}
1408-
1409-
case ClosureActorIsolation::GlobalActor:
1410-
parentIsolation = ActorIsolation::forGlobalActor(
1411-
parentClosureIsolation.getGlobalActor());
1412-
break;
1413-
}
1414-
break;
1415-
}
1416-
1417-
case DeclContextKind::AbstractFunctionDecl:
1418-
case DeclContextKind::SubscriptDecl:
1419-
parentIsolation = getActorIsolation(
1420-
cast<ValueDecl>(parentDC->getAsDecl()));
1421-
break;
1422-
1423-
case DeclContextKind::EnumElementDecl:
1424-
case DeclContextKind::ExtensionDecl:
1425-
case DeclContextKind::FileUnit:
1426-
case DeclContextKind::GenericTypeDecl:
1427-
case DeclContextKind::Initializer:
1428-
case DeclContextKind::Module:
1429-
case DeclContextKind::SerializedLocal:
1430-
case DeclContextKind::TopLevelCodeDecl:
1431-
return ClosureActorIsolation::forIndependent();
1432-
}
1388+
auto parentIsolation = getActorIsolationOfContext(closure->getParent());
14331389

14341390
// We must have parent isolation determined to get here.
1435-
assert(parentIsolation && "Missing parent isolation?");
1436-
switch (*parentIsolation) {
1391+
switch (parentIsolation) {
14371392
case ActorIsolation::Independent:
14381393
case ActorIsolation::IndependentUnsafe:
14391394
case ActorIsolation::Unspecified:
14401395
return ClosureActorIsolation::forIndependent();
14411396

14421397
case ActorIsolation::GlobalActor: {
14431398
Type globalActorType = closure->mapTypeIntoContext(
1444-
parentIsolation->getGlobalActor()->mapTypeOutOfContext());
1399+
parentIsolation.getGlobalActor()->mapTypeOutOfContext());
14451400
return ClosureActorIsolation::forGlobalActor(globalActorType);
14461401
}
14471402

0 commit comments

Comments
 (0)