Skip to content

[5.2] [PropertyWrappers] Fix a bug with class property wrapper access control #29193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/Sema/TypeCheckPropertyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ enum class PropertyWrapperInitKind {
Default
};

static bool isDeclNotAsAccessibleAsParent(ValueDecl *decl,
NominalTypeDecl *parent) {
return decl->getFormalAccess() <
std::min(parent->getFormalAccess(), AccessLevel::Public);
}

/// Find the named property in a property wrapper to which access will
/// be delegated.
static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
Expand Down Expand Up @@ -78,7 +84,7 @@ static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,

// The property must be as accessible as the nominal type.
VarDecl *var = vars.front();
if (var->getFormalAccess() < nominal->getFormalAccess()) {
if (isDeclNotAsAccessibleAsParent(var, nominal)) {
var->diagnose(diag::property_wrapper_type_requirement_not_accessible,
var->getFormalAccess(), var->getDescriptiveKind(),
var->getFullName(), nominal->getDeclaredType(),
Expand Down Expand Up @@ -156,7 +162,7 @@ findSuitableWrapperInit(ASTContext &ctx, NominalTypeDecl *nominal,
}

// Check accessibility.
if (init->getFormalAccess() < nominal->getFormalAccess()) {
if (isDeclNotAsAccessibleAsParent(init, nominal)) {
nonviable.push_back(
std::make_tuple(init, NonViableReason::Inaccessible, Type()));
continue;
Expand Down Expand Up @@ -272,7 +278,7 @@ static SubscriptDecl *findEnclosingSelfSubscript(ASTContext &ctx,

auto subscript = subscripts.front();
// the subscript must be as accessible as the nominal type.
if (subscript->getFormalAccess() < nominal->getFormalAccess()) {
if (isDeclNotAsAccessibleAsParent(subscript, nominal)) {
subscript->diagnose(diag::property_wrapper_type_requirement_not_accessible,
subscript->getFormalAccess(),
subscript->getDescriptiveKind(),
Expand Down
10 changes: 10 additions & 0 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1870,3 +1870,13 @@ struct Rdar57411331 {

var other: Int
}

// SR-11994
@propertyWrapper
open class OpenPropertyWrapperWithPublicInit {
public init(wrappedValue: String) { // Okay
self.wrappedValue = wrappedValue
}

open var wrappedValue: String = "Hello, world"
}