Skip to content

Commit 4651af1

Browse files
committed
[Concurrency] Disallow global actor annotations on stored properties of structs.
They don't make sense because stored properties of structs have the isolation of their enclosure values. Fixes rdar://70881253
1 parent 0861f7e commit 4651af1

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,6 +4267,8 @@ ERROR(global_actor_on_actor_class,none,
42674267
"actor class %0 cannot have a global actor", (Identifier))
42684268
ERROR(global_actor_on_local_variable,none,
42694269
"local variable %0 cannot have a global actor", (DeclName))
4270+
ERROR(global_actor_on_struct_property,none,
4271+
"stored property %0 of a struct cannot have a global actor", (DeclName))
42704272

42714273
ERROR(actor_isolation_multiple_attr,none,
42724274
"%0 %1 has multiple actor-isolation attributes ('%2' and '%3')",

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ GlobalActorAttributeRequest::evaluate(
406406
.highlight(globalActorAttr->getRangeWithAt());
407407
return None;
408408
}
409+
410+
// Global actors don't make sense on a stored property of a struct.
411+
if (var->hasStorage() && var->getDeclContext()->getSelfStructDecl() &&
412+
var->isInstanceMember()) {
413+
var->diagnose(diag::global_actor_on_struct_property, var->getName())
414+
.highlight(globalActorAttr->getRangeWithAt());
415+
return None;
416+
}
417+
409418
}
410419
} else if (isa<ExtensionDecl>(decl)) {
411420
// Extensions are okay.

test/attr/global_actor.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ struct OtherGlobalActor {
6464
_ = x
6565
}
6666

67-
@GA1 struct X { }
67+
@GA1 struct X {
68+
@GA1 var member: Int // expected-error{{stored property 'member' of a struct cannot have a global actor}}
69+
}
6870

6971
struct Y {
7072
@GA1 subscript(i: Int) -> Int { i }

0 commit comments

Comments
 (0)