Skip to content

Commit 1203074

Browse files
authored
Merge pull request #34527 from DougGregor/global-actor-struct-property
[Concurrency] Disallow global actor annotations on stored properties of structs
2 parents c6bb359 + 4651af1 commit 1203074

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
@@ -4270,6 +4270,8 @@ ERROR(global_actor_on_actor_class,none,
42704270
"actor class %0 cannot have a global actor", (Identifier))
42714271
ERROR(global_actor_on_local_variable,none,
42724272
"local variable %0 cannot have a global actor", (DeclName))
4273+
ERROR(global_actor_on_struct_property,none,
4274+
"stored property %0 of a struct cannot have a global actor", (DeclName))
42734275

42744276
ERROR(actor_isolation_multiple_attr,none,
42754277
"%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
@@ -416,6 +416,15 @@ GlobalActorAttributeRequest::evaluate(
416416
.highlight(globalActorAttr->getRangeWithAt());
417417
return None;
418418
}
419+
420+
// Global actors don't make sense on a stored property of a struct.
421+
if (var->hasStorage() && var->getDeclContext()->getSelfStructDecl() &&
422+
var->isInstanceMember()) {
423+
var->diagnose(diag::global_actor_on_struct_property, var->getName())
424+
.highlight(globalActorAttr->getRangeWithAt());
425+
return None;
426+
}
427+
419428
}
420429
} else if (isa<ExtensionDecl>(decl)) {
421430
// 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)