Skip to content

Commit 9360e90

Browse files
committed
[Concurrency] Diagnose redundant nonisolated(unsafe) for an actor-isolated Sendable immutable storage.
1 parent 904f542 commit 9360e90

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
@@ -5737,6 +5737,10 @@ WARNING(unsafe_sendable_actor_constant,none,
57375737
"'(unsafe)' is unnecessary for a constant public actor property with "
57385738
"'Sendable' type %0, consider removing it",
57395739
(Type))
5740+
WARNING(nonisolated_unsafe_sendable_actor_constant,none,
5741+
"'nonisolated(unsafe)' is unnecessary for a constant actor-isolated "
5742+
"property with 'Sendable' type %0, consider removing it",
5743+
(Type))
57405744
ERROR(nonisolated_non_sendable,none,
57415745
"'nonisolated' can not be applied to variable with non-'Sendable' "
57425746
"type %0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6990,6 +6990,12 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69906990
diagnose(unsafeStart, diag::unsafe_sendable_actor_constant, type)
69916991
.fixItRemoveChars(unsafeStart,
69926992
attr->getRange().End.getAdvancedLoc(1));
6993+
} else {
6994+
// This actor is not public, so suggest to remove
6995+
// 'nonisolated(unsafe)'.
6996+
diagnose(attr->getLocation(),
6997+
diag::nonisolated_unsafe_sendable_actor_constant, type)
6998+
.fixItRemove(attr->getRange());
69936999
}
69947000

69957001
} else {

test/Concurrency/global_variables.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public struct TestWrapper {
3737
}
3838
}
3939

40+
// https://github.com/apple/swift/issues/71546
41+
actor TestActor {
42+
nonisolated(unsafe) let immutableActorIsolated = TestSendable()
43+
// expected-warning@-1 {{'nonisolated(unsafe)' is unnecessary for a constant actor-isolated property with 'Sendable' type 'TestSendable', consider removing it}} {{3-23=}}
44+
}
45+
4046
struct TestStatics {
4147
static let immutableExplicitSendable = TestSendable()
4248
static let immutableNonsendable = TestNonsendable() // expected-error{{static property 'immutableNonsendable' is not concurrency-safe because non-'Sendable' type 'TestNonsendable' may have shared mutable state}}

0 commit comments

Comments
 (0)