Skip to content

Commit 2aa47b8

Browse files
authored
[Sema] Propagate nonisolated attribute from wrapped property to the synthesized projectedValue property (#60421)
* [Sema] Propagate nonisolated attribute from wrapped variable to the synthesized projectedValue variable * [Test] Add some test cases
1 parent 8e235d7 commit 2aa47b8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,13 @@ static VarDecl *synthesizePropertyWrapperProjectionVar(
26822682
var->getAttrs().add(
26832683
new (ctx) ProjectedValuePropertyAttr(name, SourceLoc(), SourceRange(),
26842684
/*Implicit=*/true));
2685+
2686+
// If the wrapped property has a nonisolated attribute, propagate it to
2687+
// the synthesized projectedValue as well.
2688+
if (var->getAttrs().getAttribute<NonisolatedAttr>()) {
2689+
property->getAttrs().add(new (ctx) NonisolatedAttr(/*Implicit=*/true));
2690+
}
2691+
26852692
return property;
26862693
}
26872694

test/Concurrency/global_actor_inference.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,51 @@ func testInferredFromWrapper(x: InferredFromPropertyWrapper) { // expected-note{
457457
_ = x.test() // expected-error{{call to global actor 'SomeGlobalActor'-isolated instance method 'test()' in a synchronous nonisolated context}}
458458
}
459459

460+
// https://github.com/apple/swift/issues/59380
461+
// https://github.com/apple/swift/issues/59494
460462

463+
// Make sure the nonisolated attribute propagates to the synthesized projectedValue
464+
// variable as well.
465+
466+
@propertyWrapper
467+
struct SimplePropertyWrapper {
468+
var wrappedValue: Int { .zero }
469+
var projectedValue: Int { .max }
470+
}
471+
472+
@MainActor
473+
class HasNonIsolatedProperty {
474+
@SimplePropertyWrapper nonisolated var value
475+
476+
deinit {
477+
_ = value
478+
_ = $value // Ok
479+
}
480+
}
481+
482+
@propertyWrapper
483+
struct SimplePropertyWrapper2 {
484+
var wrappedValue: Int
485+
var projectedValue: SimplePropertyWrapper2 { self }
486+
}
487+
488+
@MainActor
489+
class HasNonIsolatedProperty2 {
490+
@SimplePropertyWrapper2 nonisolated var value = 0
491+
nonisolated init() {}
492+
}
493+
494+
let hasNonIsolatedProperty2 = HasNonIsolatedProperty2()
495+
496+
Task { @MainActor in
497+
hasNonIsolatedProperty2.value = 10
498+
_ = hasNonIsolatedProperty2.$value.wrappedValue
499+
}
500+
501+
Task.detached {
502+
hasNonIsolatedProperty2.value = 10
503+
_ = hasNonIsolatedProperty2.$value.wrappedValue // Ok
504+
}
461505

462506
// ----------------------------------------------------------------------
463507
// Unsafe global actors

0 commit comments

Comments
 (0)