Skip to content

Commit 59c3dd2

Browse files
authored
[Concurrency] Warning and fixig to remove unsafe from nonisolated(unsafe) func (#75663)
1 parent 739c719 commit 59c3dd2

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,6 +5738,10 @@ WARNING(nonisolated_unsafe_sendable_actor_constant,none,
57385738
"'nonisolated(unsafe)' is unnecessary for a constant actor-isolated "
57395739
"property with 'Sendable' type %0, consider removing it",
57405740
(Type))
5741+
WARNING(nonisolated_unsafe_uneffective_on_funcs,none,
5742+
"'nonisolated(unsafe)' has no effect on %0 %1, "
5743+
"consider using 'nonisolated'",
5744+
(DescriptiveDeclKind, const ValueDecl*))
57415745
ERROR(nonisolated_non_sendable,none,
57425746
"'nonisolated' can not be applied to variable with non-'Sendable' "
57435747
"type %0",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7217,6 +7217,17 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
72177217
}
72187218

72197219
if (auto VD = dyn_cast<ValueDecl>(D)) {
7220+
//'nonisolated(unsafe)' is meaningless for computed properties, functions etc.
7221+
auto var = dyn_cast<VarDecl>(VD);
7222+
if (attr->isUnsafe() &&
7223+
(!var || !var->hasStorage())) {
7224+
auto &ctx = VD->getASTContext();
7225+
ctx.Diags.diagnose(attr->getStartLoc(),
7226+
diag::nonisolated_unsafe_uneffective_on_funcs,
7227+
VD->getDescriptiveKind(), VD)
7228+
.fixItReplace(attr->getStartLoc(), "nonisolated");
7229+
}
7230+
72207231
(void)getActorIsolation(VD);
72217232
}
72227233
}

test/Concurrency/actor_isolation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ extension MyActor {
187187
set { }
188188
}
189189

190+
// expected-warning@+1{{'nonisolated(unsafe)' has no effect on instance method 'nonisolatedUnsafe(otherActor:)', consider using 'nonisolated'}}{{3-14=nonisolated}}
191+
nonisolated(unsafe) func nonisolatedUnsafe(otherActor: MyActor) -> Int { }
192+
190193
nonisolated func actorIndependentFunc(otherActor: MyActor) -> Int {
191194
_ = immutable
192195
_ = mutable // expected-error{{actor-isolated property 'mutable' can not be referenced from a nonisolated}}

test/Concurrency/transfernonsendable_nonisolatedunsafe.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ enum NonIsolatedUnsafeComputedEnum: Sendable {
507507
case second
508508

509509
nonisolated(unsafe) var nonIsolatedUnsafeVarObject: NonSendableKlass { NonSendableKlass() }
510+
// expected-warning@-1{{'nonisolated(unsafe)' has no effect on property 'nonIsolatedUnsafeVarObject', consider using 'nonisolated'}}
510511

511512
func test() async {
512513
await transferToMainDirect(nonIsolatedUnsafeVarObject)
@@ -605,6 +606,7 @@ enum NonIsolatedUnsafeComputedEnum: Sendable {
605606
nonisolated(unsafe) let nonIsolatedUnsafeLetObject = NonSendableKlass()
606607
nonisolated(unsafe) var nonIsolatedUnsafeVarObject = NonSendableKlass()
607608
nonisolated(unsafe) var nonIsolatedUnsafeVarComputedObject: NonSendableKlass { NonSendableKlass() }
609+
// expected-warning@-1{{'nonisolated(unsafe)' has no effect on property 'nonIsolatedUnsafeVarComputedObject', consider using 'nonisolated'}}
608610

609611
var t: T? = nil
610612

@@ -646,6 +648,7 @@ enum NonIsolatedUnsafeComputedEnum: Sendable {
646648
case second
647649

648650
nonisolated(unsafe) var nonIsolatedUnsafeVarObject: NonSendableKlass { NonSendableKlass() }
651+
// expected-warning@-1{{'nonisolated(unsafe)' has no effect on property 'nonIsolatedUnsafeVarObject', consider using 'nonisolated'}}
649652

650653
func test() async {
651654
await transferToMainDirect(nonIsolatedUnsafeVarObject)
@@ -668,6 +671,7 @@ struct NonIsolatedUnsafeFieldNonSendableStruct {
668671
nonisolated(unsafe) let nonIsolatedUnsafeLetObject = NonSendableKlass()
669672
nonisolated(unsafe) var nonIsolatedUnsafeVarObject = NonSendableKlass()
670673
nonisolated(unsafe) var nonIsolatedUnsafeVarComputedObject: NonSendableKlass { NonSendableKlass() }
674+
// expected-warning@-1{{'nonisolated(unsafe)' has no effect on property 'nonIsolatedUnsafeVarComputedObject', consider using 'nonisolated'}}
671675

672676
let letObject = NonSendableKlass()
673677
var varObject = NonSendableKlass()

0 commit comments

Comments
 (0)