Skip to content

Commit 9aa13e0

Browse files
committed
[Concurrency] Diagnose redundant nonisolated(unsafe) for an actor-isolated Sendable immutable storage.
1 parent cf05d97 commit 9aa13e0

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
@@ -5694,6 +5694,10 @@ WARNING(unsafe_sendable_actor_constant,none,
56945694
"'(unsafe)' is unnecessary for a constant public actor property with "
56955695
"'Sendable' type %0, consider removing it",
56965696
(Type))
5697+
WARNING(nonisolated_unsafe_sendable_actor_constant,none,
5698+
"'nonisolated(unsafe)' is unnecessary for a constant actor-isolated "
5699+
"property with 'Sendable' type %0, consider removing it",
5700+
(Type))
56975701
ERROR(nonisolated_non_sendable,none,
56985702
"'nonisolated' can not be applied to variable with non-'Sendable' "
56995703
"type %0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6941,6 +6941,12 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69416941
diagnose(unsafeStart, diag::unsafe_sendable_actor_constant, type)
69426942
.fixItRemoveChars(unsafeStart,
69436943
attr->getRange().End.getAdvancedLoc(1));
6944+
} else {
6945+
// This actor is not public, so suggest to remove
6946+
// 'nonisolated(unsafe)'.
6947+
diagnose(attr->getLocation(),
6948+
diag::nonisolated_unsafe_sendable_actor_constant, type)
6949+
.fixItRemove(attr->getRange());
69446950
}
69456951

69466952
} 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 it is not either conforming to 'Sendable' or isolated to a global actor}}

0 commit comments

Comments
 (0)