Skip to content

Commit 37e4542

Browse files
committed
[Property Wrappers] For a property with an attached property wrapper,
consider its didSet mutability when computing setter mutability.
1 parent 149a1f4 commit 37e4542

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,13 @@ IsSetterMutatingRequest::evaluate(Evaluator &evaluator,
342342
if (auto var = dyn_cast<VarDecl>(storage)) {
343343
if (auto mut = var->getPropertyWrapperMutability()) {
344344
bool isMutating = mut->Setter == PropertyWrapperMutability::Mutating;
345-
if (var->getParsedAccessor(AccessorKind::DidSet)) {
345+
if (auto *accessor = var->getParsedAccessor(AccessorKind::DidSet)) {
346346
// If there's a didSet, we call the getter for the 'oldValue', and so
347347
// should consider the getter's mutatingness as well
348-
isMutating |= (mut->Getter == PropertyWrapperMutability::Mutating);
348+
if (!accessor->isSimpleDidSet()) {
349+
isMutating |= (mut->Getter == PropertyWrapperMutability::Mutating);
350+
}
351+
isMutating |= accessor->getAttrs().hasAttribute<MutatingAttr>();
349352
}
350353
return isMutating && result;
351354
}

test/SILGen/property_wrapper_observers.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,28 @@ class Bar {
6868
// CHECK-NEXT: destroy_addr [[ALLOC_STACK]] : $*Array<Int>
6969
// CHECK-NEXT: dealloc_stack [[ALLOC_STACK]] : $*Array<Int>
7070
// CHECK-NEXT: unwind
71-
// CHECK-END: }
71+
// CHECK-END: }
72+
73+
74+
@propertyWrapper
75+
struct State {
76+
var wrappedValue: Int {
77+
get { 0 }
78+
nonmutating set {}
79+
}
80+
}
81+
82+
struct MutatingDidSet {
83+
@State private var value: Int {
84+
mutating didSet {}
85+
}
86+
87+
mutating func test() {
88+
value = 10
89+
}
90+
}
91+
92+
// MutatingDidSet.value.setter
93+
// CHECK-LABEL: sil private [ossa] @$s26property_wrapper_observers14MutatingDidSetV5value33_{{.*}} : $@convention(method) (Int, @inout MutatingDidSet) -> () {
94+
// CHECK: function_ref @$s26property_wrapper_observers5StateV12wrappedValueSivs : $@convention(method) (Int, State) -> ()
95+
// CHECK: function_ref @$s26property_wrapper_observers14MutatingDidSetV5value33_{{.*}} : $@convention(method) (@inout MutatingDidSet) -> ()

0 commit comments

Comments
 (0)