Skip to content

Commit 29268e2

Browse files
authored
[PropertyWrappers] Fix a bug with class property wrapper access control (#29023)
* [PropertyWrappers] Class property wrapper cannot have an open init, so make sure to consider that when looking for suitable wrapper inits * [PropertyWrappers] Extract the access control check into a separate static method
1 parent 9b4906d commit 29268e2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ enum class PropertyWrapperInitKind {
3636
Default
3737
};
3838

39+
static bool isDeclNotAsAccessibleAsParent(ValueDecl *decl,
40+
NominalTypeDecl *parent) {
41+
return decl->getFormalAccess() <
42+
std::min(parent->getFormalAccess(), AccessLevel::Public);
43+
}
44+
3945
/// Find the named property in a property wrapper to which access will
4046
/// be delegated.
4147
static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
@@ -79,7 +85,7 @@ static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
7985

8086
// The property must be as accessible as the nominal type.
8187
VarDecl *var = vars.front();
82-
if (var->getFormalAccess() < nominal->getFormalAccess()) {
88+
if (isDeclNotAsAccessibleAsParent(var, nominal)) {
8389
var->diagnose(diag::property_wrapper_type_requirement_not_accessible,
8490
var->getFormalAccess(), var->getDescriptiveKind(),
8591
var->getFullName(), nominal->getDeclaredType(),
@@ -157,7 +163,7 @@ findSuitableWrapperInit(ASTContext &ctx, NominalTypeDecl *nominal,
157163
}
158164

159165
// Check accessibility.
160-
if (init->getFormalAccess() < nominal->getFormalAccess()) {
166+
if (isDeclNotAsAccessibleAsParent(init, nominal)) {
161167
nonviable.push_back(
162168
std::make_tuple(init, NonViableReason::Inaccessible, Type()));
163169
continue;
@@ -273,7 +279,7 @@ static SubscriptDecl *findEnclosingSelfSubscript(ASTContext &ctx,
273279

274280
auto subscript = subscripts.front();
275281
// the subscript must be as accessible as the nominal type.
276-
if (subscript->getFormalAccess() < nominal->getFormalAccess()) {
282+
if (isDeclNotAsAccessibleAsParent(subscript, nominal)) {
277283
subscript->diagnose(diag::property_wrapper_type_requirement_not_accessible,
278284
subscript->getFormalAccess(),
279285
subscript->getDescriptiveKind(),

test/decl/var/property_wrappers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,3 +1870,13 @@ struct Rdar57411331 {
18701870

18711871
var other: Int
18721872
}
1873+
1874+
// SR-11994
1875+
@propertyWrapper
1876+
open class OpenPropertyWrapperWithPublicInit {
1877+
public init(wrappedValue: String) { // Okay
1878+
self.wrappedValue = wrappedValue
1879+
}
1880+
1881+
open var wrappedValue: String = "Hello, world"
1882+
}

0 commit comments

Comments
 (0)