Skip to content

Commit

Permalink
[Concurrency] Diagnose redundant nonisolated(unsafe) for an actor-i…
Browse files Browse the repository at this point in the history
…solated `Sendable` immutable storage.
  • Loading branch information
simanerush committed Mar 13, 2024
1 parent cf05d97 commit 9aa13e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5694,6 +5694,10 @@ WARNING(unsafe_sendable_actor_constant,none,
"'(unsafe)' is unnecessary for a constant public actor property with "
"'Sendable' type %0, consider removing it",
(Type))
WARNING(nonisolated_unsafe_sendable_actor_constant,none,
"'nonisolated(unsafe)' is unnecessary for a constant actor-isolated "
"property with 'Sendable' type %0, consider removing it",
(Type))
ERROR(nonisolated_non_sendable,none,
"'nonisolated' can not be applied to variable with non-'Sendable' "
"type %0",
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6941,6 +6941,12 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
diagnose(unsafeStart, diag::unsafe_sendable_actor_constant, type)
.fixItRemoveChars(unsafeStart,
attr->getRange().End.getAdvancedLoc(1));
} else {
// This actor is not public, so suggest to remove
// 'nonisolated(unsafe)'.
diagnose(attr->getLocation(),
diag::nonisolated_unsafe_sendable_actor_constant, type)
.fixItRemove(attr->getRange());
}

} else {
Expand Down
6 changes: 6 additions & 0 deletions test/Concurrency/global_variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public struct TestWrapper {
}
}

// https://github.com/apple/swift/issues/71546
actor TestActor {
nonisolated(unsafe) let immutableActorIsolated = TestSendable()
// expected-warning@-1 {{'nonisolated(unsafe)' is unnecessary for a constant actor-isolated property with 'Sendable' type 'TestSendable', consider removing it}} {{3-23=}}
}

struct TestStatics {
static let immutableExplicitSendable = TestSendable()
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}}
Expand Down

0 comments on commit 9aa13e0

Please sign in to comment.