Skip to content

Commit 355a3aa

Browse files
committed
[Concurrency] Diagnose redundant (unsafe) for a static constant Sendable property of an actor.
1 parent 1123bc0 commit 355a3aa

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5690,6 +5690,10 @@ WARNING(nonisolated_unsafe_sendable_struct,none,
56905690
"'nonisolated(unsafe)' is unnecessary for a constant with 'Sendable' type "
56915691
"%0, consider removing it",
56925692
(Type))
5693+
WARNING(unsafe_sendable_actor,none,
5694+
"'(unsafe)' is unnecessary for a constant actor property with 'Sendable' "
5695+
"type %0, consider removing it",
5696+
(Type))
56935697
ERROR(nonisolated_non_sendable,none,
56945698
"'nonisolated' can not be applied to variable with non-'Sendable' "
56955699
"type %0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6917,6 +6917,17 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69176917
if (isa<StructDecl>(nominal))
69186918
diagnoseAndRemoveAttr(
69196919
attr, diag::nonisolated_unsafe_sendable_struct, type);
6920+
6921+
// '(unsafe)' is redundant for static constant 'Sendable' actor
6922+
// properties.
6923+
// TODO: Should this apply to only public actors?
6924+
if (nominal->isActor()) {
6925+
// Get the location where '(unsafe)' starts.
6926+
SourceLoc unsafeStart = Lexer::getLocForEndOfToken(
6927+
Ctx.SourceMgr, attr->getRange().Start);
6928+
diagnose(unsafeStart, diag::unsafe_sendable_actor, type)
6929+
.fixItRemoveChars(unsafeStart, attr->getRange().End);
6930+
}
69206931
}
69216932

69226933
// 'nonisolated' is redundant for the stored properties of a struct.

test/Concurrency/global_variables.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ struct TestStatics {
5454
// expected-note@-1{{isolate 'wrapped' to a global actor, or convert it to a 'let' constant and conform it to 'Sendable'}}
5555
}
5656

57+
actor TestPublicActor {
58+
static nonisolated(unsafe) let immutableNonisolatedUnsafeSendable = TestSendable()
59+
// expected-warning@-1 {{'(unsafe)' is unnecessary for a constant actor property with 'Sendable' type 'TestSendable', consider removing it}}
60+
}
61+
5762
@TestGlobalActor
5863
func f() {
5964
print(TestStatics.immutableExplicitSendable)

0 commit comments

Comments
 (0)