Skip to content

Commit c3ee467

Browse files
committed
[Isolation] Factor out the "not a value" check from "okay across actors".
1 parent e45a29c commit c3ee467

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,9 +4886,8 @@ bool swift::isThrowsDecl(ConcreteDeclRef declRef) {
48864886
return false;
48874887
}
48884888

4889-
bool swift::isAccessibleAcrossActors(
4890-
ValueDecl *value, const ActorIsolation &isolation,
4891-
const DeclContext *fromDC, Optional<ReferencedActor> actorInstance) {
4889+
/// Determine whether a reference to this value isn't actually a value.
4890+
static bool isNonValueReference(const ValueDecl *value) {
48924891
switch (value->getKind()) {
48934892
case DeclKind::AssociatedType:
48944893
case DeclKind::Class:
@@ -4899,13 +4898,7 @@ bool swift::isAccessibleAcrossActors(
48994898
case DeclKind::Protocol:
49004899
case DeclKind::Struct:
49014900
case DeclKind::TypeAlias:
4902-
return true;
4903-
49044901
case DeclKind::EnumCase:
4905-
case DeclKind::EnumElement:
4906-
// Type-level entities are always accessible across actors.
4907-
return true;
4908-
49094902
case DeclKind::IfConfig:
49104903
case DeclKind::Import:
49114904
case DeclKind::InfixOperator:
@@ -4917,16 +4910,26 @@ bool swift::isAccessibleAcrossActors(
49174910
case DeclKind::PrecedenceGroup:
49184911
case DeclKind::PrefixOperator:
49194912
case DeclKind::TopLevelCode:
4920-
// Non-value entities are always accessible across actors.
4921-
return true;
4922-
49234913
case DeclKind::Destructor:
4924-
// Destructors are always accessible across actors.
49254914
return true;
49264915

4916+
case DeclKind::EnumElement:
49274917
case DeclKind::Constructor:
4928-
// Initializers are accessible across actors unless they are global-actor
4929-
// qualified.
4918+
case DeclKind::Param:
4919+
case DeclKind::Var:
4920+
case DeclKind::Accessor:
4921+
case DeclKind::Func:
4922+
case DeclKind::Subscript:
4923+
return false;
4924+
}
4925+
}
4926+
4927+
bool swift::isAccessibleAcrossActors(
4928+
ValueDecl *value, const ActorIsolation &isolation,
4929+
const DeclContext *fromDC, Optional<ReferencedActor> actorInstance) {
4930+
// Initializers and enum elements are accessible across actors unless they
4931+
// are global-actor qualified.
4932+
if (isa<ConstructorDecl>(value) || isa<EnumElementDecl>(value)) {
49304933
switch (isolation) {
49314934
case ActorIsolation::ActorInstance:
49324935
case ActorIsolation::Independent:
@@ -4937,19 +4940,15 @@ bool swift::isAccessibleAcrossActors(
49374940
case ActorIsolation::GlobalActor:
49384941
return false;
49394942
}
4943+
}
49404944

4941-
case DeclKind::Param:
4942-
case DeclKind::Var:
4943-
// 'let' declarations are immutable, so some of them can be accessed across
4944-
// actors.
4945-
return varIsSafeAcrossActors(
4946-
fromDC->getParentModule(), cast<VarDecl>(value), isolation);
4947-
4948-
case DeclKind::Accessor:
4949-
case DeclKind::Func:
4950-
case DeclKind::Subscript:
4951-
return false;
4945+
// 'let' declarations are immutable, so some of them can be accessed across
4946+
// actors.
4947+
if (auto var = dyn_cast<VarDecl>(value)) {
4948+
return varIsSafeAcrossActors(fromDC->getParentModule(), var, isolation);
49524949
}
4950+
4951+
return false;
49534952
}
49544953

49554954
ActorReferenceResult ActorReferenceResult::forSameConcurrencyDomain(
@@ -4998,6 +4997,11 @@ ActorReferenceResult ActorReferenceResult::forReference(
49984997
declIsolation = declIsolation.subst(declRef.getSubstitutions());
49994998
}
50004999

5000+
// If the entity we are referencing is not a value, we're in thesame
5001+
// concurrency domain.
5002+
if (isNonValueReference(declRef.getDecl()))
5003+
return forSameConcurrencyDomain(declIsolation);
5004+
50015005
// Compute the isolation of the context, if not provided.
50025006
ActorIsolation contextIsolation = ActorIsolation::forUnspecified();
50035007
if (knownContextIsolation) {

0 commit comments

Comments
 (0)