Skip to content

[Diagnostics] Tweak @propertyWrapper diags and add an educational note #30116

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 1 commit into from
Feb 29, 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
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4795,7 +4795,7 @@ ERROR(property_wrapper_wrong_initial_value_init, none,
"'wrappedValue' property type (%2) or an @autoclosure thereof",
(DeclName, Type, Type))
ERROR(property_wrapper_failable_init, none,
"%0 cannot be failable", (DeclName))
"property wrapper initializer %0 cannot be failable", (DeclName))
ERROR(property_wrapper_type_requirement_not_accessible,none,
"%select{private|fileprivate|internal|public|open}0 %1 %2 cannot have "
"more restrictive access than its enclosing property wrapper type %3 "
Expand Down
9 changes: 9 additions & 0 deletions include/swift/AST/EducationalNotes.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ EDUCATIONAL_NOTES(invalid_dynamic_callable_type,
EDUCATIONAL_NOTES(missing_dynamic_callable_kwargs_method,
"dynamic-callable-requirements.md")

EDUCATIONAL_NOTES(property_wrapper_no_value_property,
"property-wrapper-requirements.md")
EDUCATIONAL_NOTES(property_wrapper_wrong_initial_value_init,
"property-wrapper-requirements.md")
EDUCATIONAL_NOTES(property_wrapper_failable_init,
"property-wrapper-requirements.md")
EDUCATIONAL_NOTES(property_wrapper_type_requirement_not_accessible,
"property-wrapper-requirements.md")

#undef EDUCATIONAL_NOTES
4 changes: 2 additions & 2 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct MultipleInitialValues<Value> {
struct InitialValueFailable<Value> {
var wrappedValue: Value

init?(wrappedValue initialValue: Value) { // expected-error{{'init(wrappedValue:)' cannot be failable}}
init?(wrappedValue initialValue: Value) { // expected-error{{property wrapper initializer 'init(wrappedValue:)' cannot be failable}}
return nil
}
}
Expand All @@ -121,7 +121,7 @@ struct InitialValueFailable<Value> {
struct InitialValueFailableIUO<Value> {
var wrappedValue: Value

init!(wrappedValue initialValue: Value) { // expected-error{{'init(wrappedValue:)' cannot be failable}}
init!(wrappedValue initialValue: Value) { // expected-error{{property wrapper initializer 'init(wrappedValue:)' cannot be failable}}
return nil
}
}
Expand Down
7 changes: 7 additions & 0 deletions userdocs/diagnostics/property-wrapper-requirements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Property Wrapper Implementation Requirements
---
If a type is marked with the `@propertyWrapper` attribute, it must meet certain requirements to be a valid property wrapper.

First, all property wrapper types must have a property named `wrappedValue`. This property cannot be static and must have the same access level as the property wrapper type. If the property wrapper provides a `projectedValue` property, it is subject to the same requirements.

Second, none of a property wrapper's initializers may be failable. Additionally, if a property wrapper initializer has a `wrappedValue` parameter, the type of that parameter must either be the same as the type of the `wrappedValue` property or an `@autoclosure` of that type.