Skip to content

Commit 74d2127

Browse files
Merge pull request #71067 from sophiapoirier/nonisolated-mutable-stored-property-diagnostic
[Concurrency] clarify diagnostic for 'nonisolated' stored properties being due to mutability
2 parents eaf6c36 + 27b57c6 commit 74d2127

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5659,8 +5659,12 @@ ERROR(nonisolated_let,none,
56595659
"they are immutable",
56605660
())
56615661
ERROR(nonisolated_mutable_storage,none,
5662-
"'nonisolated' can not be applied to stored properties",
5662+
"'nonisolated' cannot be applied to mutable stored properties",
56635663
())
5664+
NOTE(nonisolated_mutable_storage_note,none,
5665+
"convert %0 to a 'let' constant or consider declaring it "
5666+
"'nonisolated(unsafe)' if manually managing concurrency safety",
5667+
(const VarDecl *))
56645668
ERROR(nonisolated_local_var,none,
56655669
"'nonisolated' can not be applied to local variables",
56665670
())

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6871,7 +6871,9 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
68716871
// 'nonisolated' can not be applied to mutable stored properties unless
68726872
// qualified as 'unsafe'.
68736873
if (var->supportsMutation() && !attr->isUnsafe()) {
6874-
diagnoseAndRemoveAttr(attr, diag::nonisolated_mutable_storage);
6874+
diagnoseAndRemoveAttr(attr, diag::nonisolated_mutable_storage)
6875+
.fixItInsertAfter(attr->getRange().End, "(unsafe)");
6876+
var->diagnose(diag::nonisolated_mutable_storage_note, var);
68756877
return;
68766878
}
68776879

test/Concurrency/actor_isolation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ struct InferredFromContext {
139139
get { [] }
140140
}
141141

142-
nonisolated var status: Bool = true // expected-error {{'nonisolated' can not be applied to stored properties}}{{3-15=}}
142+
nonisolated var status: Bool = true // expected-error {{'nonisolated' cannot be applied to mutable stored properties}}{{3-15=}}{{3-15=}}{{14-14=(unsafe)}}
143+
// expected-note@-1{{convert 'status' to a 'let' constant or consider declaring it 'nonisolated(unsafe)' if manually managing concurrency safety}}
143144

144145
nonisolated let flag: Bool = false // expected-warning {{'nonisolated' is redundant on struct's stored properties; this is an error in Swift 6}}{{3-15=}}
145146

0 commit comments

Comments
 (0)