Skip to content

Commit af8d931

Browse files
committed
[Concurrency] Loosen constraints on @actorIndependent placement.
@actorIndependent is needed on declarations outside of actors to (e.g.) disable inference of a global actor. It is also effectively the default, so allow it to be specified explicitly.
1 parent 5e9c24e commit af8d931

File tree

4 files changed

+14
-31
lines changed

4 files changed

+14
-31
lines changed

include/swift/AST/Attr.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,10 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(actor, Actor,
572572
102)
573573

574574
DECL_ATTR(actorIndependent, ActorIndependent,
575-
OnFunc | OnVar | OnSubscript | ConcurrencyOnly |
575+
OnClass | OnStruct | OnEnum | OnExtension | OnFunc | OnConstructor |
576+
OnVar | OnSubscript | ConcurrencyOnly |
576577
ABIStableToAdd | ABIStableToRemove |
577-
APIStableToAdd | APIBreakingToRemove,
578+
APIBreakingToAdd | APIBreakingToRemove,
578579
103)
579580

580581
SIMPLE_DECL_ATTR(globalActor, GlobalActor,

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,13 +4281,6 @@ ERROR(actorindependent_mutable_storage,none,
42814281
ERROR(actorindependent_local_var,none,
42824282
"'@actorIndependent' can not be applied to local variables",
42834283
())
4284-
ERROR(actorindependent_not_actor_member,none,
4285-
"'@actorIndependent' can only be applied to actor members and "
4286-
"global/static variables",
4287-
())
4288-
ERROR(actorindependent_not_actor_instance_member,none,
4289-
"'@actorIndependent' can only be applied to instance members of actors",
4290-
())
42914284

42924285
ERROR(concurrency_lib_missing,none,
42934286
"missing '%0' declaration, probably because the '_Concurrency' "

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
298298
case ActorIndependentKind::Safe:
299299
diagnoseAndRemoveAttr(attr, diag::actorindependent_mutable_storage);
300300
return;
301-
301+
302302
case ActorIndependentKind::Unsafe:
303303
break;
304304
}
@@ -315,26 +315,11 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
315315
(dc->isTypeContext() && var->isStatic())) {
316316
return;
317317
}
318-
319-
// Otherwise, fall through to make sure we're in an appropriate
320-
// context.
321318
}
322319

323-
// @actorIndependent only makes sense on an actor instance member.
324-
if (!dc->getSelfClassDecl() ||
325-
!dc->getSelfClassDecl()->isActor()) {
326-
diagnoseAndRemoveAttr(attr, diag::actorindependent_not_actor_member);
327-
return;
320+
if (auto VD = dyn_cast<ValueDecl>(D)) {
321+
(void)getActorIsolation(VD);
328322
}
329-
330-
auto VD = cast<ValueDecl>(D);
331-
if (!VD->isInstanceMember()) {
332-
diagnoseAndRemoveAttr(
333-
attr, diag::actorindependent_not_actor_instance_member);
334-
return;
335-
}
336-
337-
(void)getActorIsolation(VD);
338323
}
339324

340325
void visitGlobalActorAttr(GlobalActorAttr *attr) {

test/attr/actorindependent.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
// REQUIRES: concurrency
44

5-
// expected-error@+1{{'@actorIndependent' can only be applied to actor members and global/static variables}}
65
@actorIndependent func globalFunction() { }
76

87
@actorIndependent var globalComputedProperty1: Int { 17 }
@@ -33,9 +32,11 @@ struct X {
3332
}
3433

3534
class C {
36-
// expected-error@+1{{'@actorIndependent' can only be applied to actor members and global/static variables}}
3735
@actorIndependent
3836
var property3: Int { 5 }
37+
38+
@actorIndependent
39+
func f() { }
3940
}
4041

4142
actor class A {
@@ -65,6 +66,8 @@ actor class A {
6566
set { }
6667
}
6768

69+
@actorIndependent init() { }
70+
6871
@actorIndependent
6972
func synchronousFunc() { }
7073

@@ -74,7 +77,6 @@ actor class A {
7477
@actorIndependent
7578
subscript(index: Int) -> String { "\(index)" }
7679

77-
// expected-error@+1{{'@actorIndependent' can only be applied to instance members of actors}}
7880
@actorIndependent static func staticFunc() { }
7981
}
8082

@@ -95,4 +97,6 @@ actor class FromProperty {
9597
// expected-error@+1{{actor-isolated property 'counter' can not be referenced from an '@actorIndependent' context}}
9698
set { counter = newValue }
9799
}
98-
}
100+
}
101+
102+
@actorIndependent extension FromProperty { }

0 commit comments

Comments
 (0)