Skip to content

[Property Wrappers] Don't allow wrappedValue and projectedValue to have dynamic Self type. #31344

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
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4904,6 +4904,9 @@ ERROR(property_wrapper_type_requirement_not_accessible,none,
ERROR(property_wrapper_ambiguous_enclosing_self_subscript, none,
"property wrapper type %0 has multiple enclosing-self subscripts %1",
(Type, DeclName))
ERROR(property_wrapper_dynamic_self_type, none,
"property wrapper %select{wrapped value|projected value}0 cannot have "
"dynamic Self type", (bool))

ERROR(property_wrapper_attribute_not_on_property, none,
"property wrapper attribute %0 can only be applied to a property",
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {

// If the nominal type is a property wrapper type, we can be delegating
// through a property.
if (nominal->getPropertyWrapperTypeInfo()) {
if (nominal->getAttrs().hasAttribute<PropertyWrapperAttr>()) {
// property wrappers can only be applied to variables
if (!isa<VarDecl>(D) || isa<ParamDecl>(D)) {
diagnose(attr->getLocation(),
Expand Down
21 changes: 21 additions & 0 deletions lib/Sema/TypeCheckPropertyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,27 @@ PropertyWrapperTypeInfoRequest::evaluate(
}
}

auto diagnoseInvalidDynamicSelf = [&]() -> bool {
bool invalidDynamicSelf = false;
if (result.projectedValueVar &&
result.projectedValueVar->getValueInterfaceType()->is<DynamicSelfType>()) {
result.projectedValueVar->diagnose(
diag::property_wrapper_dynamic_self_type, /*projectedValue=*/true);
invalidDynamicSelf = true;
}

if (result.valueVar->getValueInterfaceType()->is<DynamicSelfType>()) {
result.valueVar->diagnose(
diag::property_wrapper_dynamic_self_type, /*projectedValue=*/false);
invalidDynamicSelf = true;
}

return invalidDynamicSelf;
};

if (diagnoseInvalidDynamicSelf())
return PropertyWrapperTypeInfo();

return result;
}

Expand Down
19 changes: 17 additions & 2 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,28 @@ struct TestProjectionValuePropertyAttr {
@propertyWrapper
struct BrokenLazy { }
// expected-error@-1{{property wrapper type 'BrokenLazy' does not contain a non-static property named 'wrappedValue'}}
// expected-note@-2{{'BrokenLazy' declared here}}

struct S {
@BrokenLazy // expected-error{{struct 'BrokenLazy' cannot be used as an attribute}}
@BrokenLazy
var wrappedValue: Int
}

@propertyWrapper
struct DynamicSelfStruct {
var wrappedValue: Self { self } // okay
var projectedValue: Self { self } // okay
}

@propertyWrapper
class DynamicSelf {
var wrappedValue: Self { self } // expected-error {{property wrapper wrapped value cannot have dynamic Self type}}
var projectedValue: Self { self } // expected-error {{property wrapper projected value cannot have dynamic Self type}}
}

struct UseDynamicSelfWrapper {
@DynamicSelf() var value
}

// ---------------------------------------------------------------------------
// Invalid redeclaration
// ---------------------------------------------------------------------------
Expand Down