Skip to content

Commit b450fea

Browse files
committed
[SE-0258] Escalate access of wrapperValue-produced $x to internal.
(cherry picked from commit 1160017)
1 parent 339d3c2 commit b450fea

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,9 +1523,15 @@ static VarDecl *synthesizePropertyWrapperStorageWrapperProperty(
15231523
addMemberToContextIfNeeded(pbd, dc, var);
15241524
pbd->setStatic(var->isStatic());
15251525

1526-
// The property is always private.
1527-
property->overwriteAccess(AccessLevel::Private);
1528-
property->overwriteSetterAccess(AccessLevel::Private);
1526+
// Determine the access level for the property.
1527+
AccessLevel access =
1528+
std::min(AccessLevel::Internal, var->getFormalAccess());
1529+
property->overwriteAccess(access);
1530+
1531+
// Determine setter access.
1532+
AccessLevel setterAccess =
1533+
std::min(AccessLevel::Internal, var->getSetterFormalAccess());
1534+
property->overwriteSetterAccess(setterAccess);
15291535

15301536
// Add the accessors we need.
15311537
bool hasSetter = wrapperVar->isSettable(nullptr) &&

test/SILGen/property_wrappers.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,10 @@ struct WrapperWithStorageValue<T> {
191191

192192
struct UseWrapperWithStorageValue {
193193
// UseWrapperWithStorageValue.$x.getter
194-
// CHECK-LABEL: sil private [ossa] @$s17property_wrappers26UseWrapperWithStorageValueV2$x33_{{.*}}SiGvg : $@convention(method) (UseWrapperWithStorageValue) -> Wrapper<Int>
194+
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s17property_wrappers26UseWrapperWithStorageValueV2$xAA0D0VySiGvg : $@convention(method) (UseWrapperWithStorageValue) -> Wrapper<Int>
195195
// CHECK-NOT: return
196196
// CHECK: function_ref @$s17property_wrappers23WrapperWithStorageValueV07wrapperF0AA0C0VyxGvg
197197
@WrapperWithStorageValue(value: 17) var x: Int
198-
199-
func foo() {
200-
_ = $x
201-
}
202198
}
203199

204200
@propertyWrapper

test/decl/var/property_wrappers.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,6 @@ extension Wrapper {
695695

696696
struct TestStorageRef {
697697
@WrapperWithStorageRef var x: Int // expected-note{{'$$x' declared here}}
698-
// expected-note@-1{{'$x' declared here}}
699698

700699
init(x: Int) {
701700
self.$$x = WrapperWithStorageRef(wrappedValue: x)
@@ -713,10 +712,25 @@ struct TestStorageRef {
713712
}
714713

715714
func testStorageRef(tsr: TestStorageRef) {
716-
let _: Wrapper = tsr.$x // expected-error{{'$x' is inaccessible due to 'private' protection level}}
715+
let _: Wrapper = tsr.$x
717716
_ = tsr.$$x // expected-error{{'$$x' is inaccessible due to 'private' protection level}}
718717
}
719718

719+
struct TestStorageRefPrivate {
720+
@WrapperWithStorageRef private(set) var x: Int
721+
722+
init() {
723+
self.$$x = WrapperWithStorageRef(wrappedValue: 5)
724+
}
725+
}
726+
727+
func testStorageRefPrivate() {
728+
var tsr = TestStorageRefPrivate()
729+
let a = tsr.$x // okay, getter is internal
730+
tsr.$x = a // expected-error{{cannot assign to property: '$x' is immutable}}
731+
}
732+
733+
720734
// rdar://problem/50873275 - crash when using wrapper with wrapperValue in
721735
// generic type.
722736
@propertyWrapper

0 commit comments

Comments
 (0)