Skip to content

Commit 13092e0

Browse files
committed
[Concurrency] Diagnose a redundant nonisolated(unsafe)
1 parent 6fbc06e commit 13092e0

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5686,6 +5686,10 @@ NOTE(nonisolated_mutable_storage_note,none,
56865686
"convert %0 to a 'let' constant or consider declaring it "
56875687
"'nonisolated(unsafe)' if manually managing concurrency safety",
56885688
(const VarDecl *))
5689+
WARNING(nonisolated_unsafe_sendable,none,
5690+
"'nonisolated(unsafe)' is unnecessary for a constant with 'Sendable' type "
5691+
"%0, consider removing it",
5692+
(Type))
56895693
ERROR(nonisolated_non_sendable,none,
56905694
"'nonisolated' can not be applied to variable with non-'Sendable' "
56915695
"type %0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6910,6 +6910,16 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69106910
return;
69116911
}
69126912

6913+
// 'nonisolated(unsafe)' is redundant for constant 'Sendable'
6914+
// properties.
6915+
// TODO: remove only the (unsafe) part for a public actor
6916+
if (attr->isUnsafe() && type->isSendableType() && var->isStatic() &&
6917+
var->isLet()) {
6918+
if (isa<StructDecl>(nominal))
6919+
diagnoseAndRemoveAttr(attr, diag::nonisolated_unsafe_sendable,
6920+
type);
6921+
}
6922+
69136923
// 'nonisolated' is redundant for the stored properties of a struct.
69146924
if (isa<StructDecl>(nominal) &&
69156925
!var->isStatic() &&

test/Concurrency/global_variables.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct TestStatics {
4141
static let immutableExplicitSendable = TestSendable()
4242
static let immutableNonsendable = TestNonsendable() // expected-error{{static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor}}
4343
static nonisolated(unsafe) let immutableNonisolatedUnsafe = TestNonsendable()
44+
static nonisolated(unsafe) let immutableNonisolatedUnsafeSendable = TestSendable()
45+
// expected-warning@-1 {{'nonisolated(unsafe)' is unnecessary for a constant with 'Sendable' type 'TestSendable', consider removing it}}
4446
static nonisolated let immutableNonisolated = TestNonsendable() // expected-error{{static property 'immutableNonisolated' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor}}
4547
// expected-error@-1 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'TestNonsendable'}}
4648
static let immutableInferredSendable = 0

0 commit comments

Comments
 (0)