Skip to content

Commit d04fff6

Browse files
committed
A @_distributedActorIndependent declaration is nonisolated.
The `@__distributedActorIndependent` attribute is effectively the same as nonisolated, so start treating it that way by making actor-isolation checking look for it specifically and conclude "nonisolated". Remove various special cases for this attribute that don't need to exist.
1 parent 7e2469f commit d04fff6

File tree

5 files changed

+38
-71
lines changed

5 files changed

+38
-71
lines changed

include/swift/AST/Decl.h

-4
Original file line numberDiff line numberDiff line change
@@ -2391,8 +2391,6 @@ class ValueDecl : public Decl {
23912391
/// Is this declaration marked with 'dynamic'?
23922392
bool isDynamic() const;
23932393

2394-
bool isDistributedActorIndependent() const;
2395-
23962394
private:
23972395
bool isObjCDynamic() const {
23982396
return isObjC() && isDynamic();
@@ -4790,8 +4788,6 @@ class AbstractStorageDecl : public ValueDecl {
47904788

47914789
bool hasAnyNativeDynamicAccessors() const;
47924790

4793-
bool isDistributedActorIndependent() const;
4794-
47954791
// Implement isa/cast/dyncast/etc.
47964792
static bool classof(const Decl *D) {
47974793
return D->getKind() >= DeclKind::First_AbstractStorageDecl &&

lib/AST/Decl.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -2934,10 +2934,6 @@ bool ValueDecl::isDynamic() const {
29342934
getAttrs().hasAttribute<DynamicAttr>());
29352935
}
29362936

2937-
bool ValueDecl::isDistributedActorIndependent() const {
2938-
return getAttrs().hasAttribute<DistributedActorIndependentAttr>();
2939-
}
2940-
29412937
bool ValueDecl::isObjCDynamicInGenericClass() const {
29422938
if (!isObjCDynamic())
29432939
return false;
@@ -5371,10 +5367,6 @@ bool AbstractStorageDecl::hasAnyNativeDynamicAccessors() const {
53715367
return false;
53725368
}
53735369

5374-
bool AbstractStorageDecl::isDistributedActorIndependent() const {
5375-
return getAttrs().hasAttribute<DistributedActorIndependentAttr>();
5376-
}
5377-
53785370
void AbstractStorageDecl::setAccessors(SourceLoc lbraceLoc,
53795371
ArrayRef<AccessorDecl *> accessors,
53805372
SourceLoc rbraceLoc) {

lib/Sema/TypeCheckConcurrency.cpp

+32-46
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ ActorIsolationRestriction ActorIsolationRestriction::forDeclaration(
396396
func->getName());
397397
}
398398
} // TODO: need to handle protocol case here too?
399-
return forDistributedActorSelf(isolation.getActor(),
400-
/*isCrossActor*/ isAccessibleAcrossActors);
401399
}
402400
}
403401

@@ -1586,13 +1584,6 @@ namespace {
15861584
ValueDecl *decl = concDeclRef.getDecl();
15871585
AsyncMarkingResult result = AsyncMarkingResult::NotFound;
15881586

1589-
// if it is explicitly marked distributed actor independent,
1590-
// it is synchronously accessible, no implicit async needed.
1591-
if (decl->getAttrs().hasAttribute<DistributedActorIndependentAttr>() ||
1592-
decl->getAttrs().hasAttribute<NonisolatedAttr>()) {
1593-
return result;
1594-
}
1595-
15961587
// is it an access to a property?
15971588
if (isPropOrSubscript(decl)) {
15981589
if (auto declRef = dyn_cast_or_null<DeclRefExpr>(context)) {
@@ -2247,9 +2238,7 @@ namespace {
22472238
!(isolatedActor.isActorSelf() &&
22482239
member->isInstanceMember() &&
22492240
isActorInitOrDeInitContext(getDeclContext())
2250-
) &&
2251-
!member->getAttrs().hasAttribute<NonisolatedAttr>() &&
2252-
!member->getAttrs().hasAttribute<DistributedActorIndependentAttr>();
2241+
);
22532242

22542243
if (performDistributedChecks) {
22552244
if (auto func = dyn_cast<FuncDecl>(member)) {
@@ -2272,13 +2261,6 @@ namespace {
22722261
} // end FuncDecl
22732262

22742263
if (isPropOrSubscript(member)) {
2275-
DeclAttributes &attrs = member->getAttrs();
2276-
if (attrs.hasAttribute<DistributedActorIndependentAttr>())
2277-
return false;
2278-
2279-
if (attrs.hasAttribute<NonisolatedAttr>())
2280-
return false;
2281-
22822264
ctx.Diags.diagnose(
22832265
memberLoc, diag::distributed_actor_isolated_non_self_reference,
22842266
member->getDescriptiveKind(),
@@ -2329,12 +2311,7 @@ namespace {
23292311
} else {
23302312
// it is some other member and since we only allow distributed
23312313
// funcs this definitely is distributed-actor-isolated
2332-
auto distributedAccessAllowed =
2333-
func->isDistributed() ||
2334-
member->getAttrs()
2335-
.hasAttribute<DistributedActorIndependentAttr>() ||
2336-
member->getAttrs().hasAttribute<NonisolatedAttr>();
2337-
if (!distributedAccessAllowed) {
2314+
if (!func->isDistributed()) {
23382315
ctx.Diags.diagnose(
23392316
memberLoc,
23402317
diag::distributed_actor_isolated_non_self_reference,
@@ -2596,17 +2573,23 @@ static Optional<ActorIsolation> getIsolationFromAttributes(
25962573
// If any of them are present, use that attribute.
25972574
auto nonisolatedAttr = decl->getAttrs().getAttribute<NonisolatedAttr>();
25982575
auto globalActorAttr = decl->getGlobalActorAttr();
2576+
auto distributedActorIndependentAttr =
2577+
decl->getAttrs().getAttribute<DistributedActorIndependentAttr>();
25992578

26002579
// Remove implicit attributes if we only care about explicit ones.
26012580
if (onlyExplicit) {
26022581
if (nonisolatedAttr && nonisolatedAttr->isImplicit())
26032582
nonisolatedAttr = nullptr;
26042583
if (globalActorAttr && globalActorAttr->first->isImplicit())
26052584
globalActorAttr = None;
2585+
if (distributedActorIndependentAttr &&
2586+
distributedActorIndependentAttr->isImplicit())
2587+
distributedActorIndependentAttr = nullptr;
26062588
}
26072589

26082590
unsigned numIsolationAttrs =
2609-
(nonisolatedAttr ? 1 : 0) + (globalActorAttr ? 1 : 0);
2591+
(nonisolatedAttr ? 1 : 0) + (globalActorAttr ? 1 : 0) +
2592+
(distributedActorIndependentAttr ? 1 : 0);
26102593
if (numIsolationAttrs == 0)
26112594
return None;
26122595

@@ -2623,19 +2606,32 @@ static Optional<ActorIsolation> getIsolationFromAttributes(
26232606

26242607
if (globalActorAttr) {
26252608
if (shouldDiagnose) {
2626-
decl->diagnose(
2627-
diag::actor_isolation_multiple_attr, decl->getDescriptiveKind(),
2628-
name, nonisolatedAttr->getAttrName(),
2629-
globalActorAttr->second->getName().str())
2630-
.highlight(nonisolatedAttr->getRangeWithAt())
2631-
.highlight(globalActorAttr->first->getRangeWithAt());
2609+
if (globalActorAttr) {
2610+
const DeclAttribute *otherAttr =
2611+
nonisolatedAttr
2612+
? static_cast<const DeclAttribute *>(nonisolatedAttr)
2613+
: distributedActorIndependentAttr;
2614+
decl->diagnose(
2615+
diag::actor_isolation_multiple_attr, decl->getDescriptiveKind(),
2616+
name, otherAttr->getAttrName(),
2617+
globalActorAttr->second->getName().str())
2618+
.highlight(otherAttr->getRangeWithAt())
2619+
.highlight(globalActorAttr->first->getRangeWithAt());
2620+
} else {
2621+
decl->diagnose(
2622+
diag::actor_isolation_multiple_attr, decl->getDescriptiveKind(),
2623+
name, nonisolatedAttr->getAttrName(),
2624+
distributedActorIndependentAttr->getAttrName())
2625+
.highlight(nonisolatedAttr->getRangeWithAt())
2626+
.highlight(distributedActorIndependentAttr->getRangeWithAt());
2627+
}
26322628
}
26332629
}
26342630
}
26352631

2636-
// If the declaration is explicitly marked 'nonisolated', report it as
2637-
// independent.
2638-
if (nonisolatedAttr) {
2632+
// If the declaration is explicitly marked 'nonisolated' or distributed
2633+
// actor-independent, report it as nonisolated.
2634+
if (nonisolatedAttr || distributedActorIndependentAttr) {
26392635
return ActorIsolation::forIndependent();
26402636
}
26412637

@@ -3089,17 +3085,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
30893085
break;
30903086
}
30913087

3092-
case ActorIsolation::DistributedActorInstance: {
3093-
/// 'distributed actor independent' implies 'nonisolated'
3094-
if (value->isDistributedActorIndependent()) {
3095-
// TODO: rename 'distributed actor independent' to 'distributed(nonisolated)'
3096-
value->getAttrs().add(
3097-
new (ctx) DistributedActorIndependentAttr(/*IsImplicit=*/true));
3098-
value->getAttrs().add(
3099-
new (ctx) NonisolatedAttr(/*IsImplicit=*/true));
3100-
}
3101-
break;
3102-
}
3088+
case ActorIsolation::DistributedActorInstance:
31033089
case ActorIsolation::ActorInstance:
31043090
case ActorIsolation::Unspecified:
31053091
if (onlyGlobal)

lib/Sema/TypeCheckProtocol.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -2839,13 +2839,6 @@ bool ConformanceChecker::checkActorIsolation(
28392839
/// nonisolated or distributed members.
28402840
auto witnessClass = dyn_cast<ClassDecl>(witness->getDeclContext());
28412841
if (witnessClass && witnessClass->isDistributedActor()) {
2842-
// we only have two 'distributed-actor-nonisolated' properties,
2843-
// the address and transport; if we see any such marked property,
2844-
// we're free to automatically assume those are fine and accessible always.
2845-
if (witness->isSynthesized() && witness->isDistributedActorIndependent()) {
2846-
return false;
2847-
}
2848-
28492842
// Maybe we're dealing with a 'distributed func' which is witness to
28502843
// a distributed function requirement, this is ok.
28512844
if (requirementFunc && requirementFunc->isDistributed() &&

test/Distributed/distributed_actor_isolation.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,24 @@ distributed actor DistributedActor_1 {
6767
fatalError()
6868
}
6969

70-
distributed func distReturnGeneric<T: Codable>(item: T) async throws -> T { // ok
70+
distributed func distReturnGeneric<T: Codable & Sendable>(item: T) async throws -> T { // ok
7171
item
7272
}
73-
distributed func distReturnGenericWhere<T>(item: Int) async throws -> T where T: Codable { // ok
73+
distributed func distReturnGenericWhere<T: Sendable>(item: Int) async throws -> T where T: Codable { // ok
7474
fatalError()
7575
}
76-
distributed func distBadReturnGeneric<T>(int: Int) async throws -> T {
76+
distributed func distBadReturnGeneric<T: Sendable>(int: Int) async throws -> T {
7777
// expected-error@-1 {{distributed function result type 'T' does not conform to 'Codable'}}
7878
fatalError()
7979
}
8080

81-
distributed func distGenericParam<T: Codable>(value: T) async throws { // ok
81+
distributed func distGenericParam<T: Codable & Sendable>(value: T) async throws { // ok
8282
fatalError()
8383
}
84-
distributed func distGenericParamWhere<T>(value: T) async throws -> T where T: Codable { // ok
84+
distributed func distGenericParamWhere<T: Sendable>(value: T) async throws -> T where T: Codable { // ok
8585
value
8686
}
87-
distributed func distBadGenericParam<T>(int: T) async throws {
87+
distributed func distBadGenericParam<T: Sendable>(int: T) async throws {
8888
// expected-error@-1 {{distributed function parameter 'int' of type 'T' does not conform to 'Codable'}}
8989
fatalError()
9090
}

0 commit comments

Comments
 (0)