Skip to content

Commit f0eb312

Browse files
authored
Merge pull request #30116 from owenv/property-wrapper-note
[Diagnostics] Tweak @propertyWrapper diagnostic wording and add an educational note
2 parents e615c1c + c75a363 commit f0eb312

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4797,7 +4797,7 @@ ERROR(property_wrapper_wrong_initial_value_init, none,
47974797
"'wrappedValue' property type (%2) or an @autoclosure thereof",
47984798
(DeclName, Type, Type))
47994799
ERROR(property_wrapper_failable_init, none,
4800-
"%0 cannot be failable", (DeclName))
4800+
"property wrapper initializer %0 cannot be failable", (DeclName))
48014801
ERROR(property_wrapper_type_requirement_not_accessible,none,
48024802
"%select{private|fileprivate|internal|public|open}0 %1 %2 cannot have "
48034803
"more restrictive access than its enclosing property wrapper type %3 "

include/swift/AST/EducationalNotes.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ EDUCATIONAL_NOTES(invalid_dynamic_callable_type,
3434
EDUCATIONAL_NOTES(missing_dynamic_callable_kwargs_method,
3535
"dynamic-callable-requirements.md")
3636

37+
EDUCATIONAL_NOTES(property_wrapper_no_value_property,
38+
"property-wrapper-requirements.md")
39+
EDUCATIONAL_NOTES(property_wrapper_wrong_initial_value_init,
40+
"property-wrapper-requirements.md")
41+
EDUCATIONAL_NOTES(property_wrapper_failable_init,
42+
"property-wrapper-requirements.md")
43+
EDUCATIONAL_NOTES(property_wrapper_type_requirement_not_accessible,
44+
"property-wrapper-requirements.md")
45+
3746
#undef EDUCATIONAL_NOTES

test/decl/var/property_wrappers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct MultipleInitialValues<Value> {
112112
struct InitialValueFailable<Value> {
113113
var wrappedValue: Value
114114

115-
init?(wrappedValue initialValue: Value) { // expected-error{{'init(wrappedValue:)' cannot be failable}}
115+
init?(wrappedValue initialValue: Value) { // expected-error{{property wrapper initializer 'init(wrappedValue:)' cannot be failable}}
116116
return nil
117117
}
118118
}
@@ -121,7 +121,7 @@ struct InitialValueFailable<Value> {
121121
struct InitialValueFailableIUO<Value> {
122122
var wrappedValue: Value
123123

124-
init!(wrappedValue initialValue: Value) { // expected-error{{'init(wrappedValue:)' cannot be failable}}
124+
init!(wrappedValue initialValue: Value) { // expected-error{{property wrapper initializer 'init(wrappedValue:)' cannot be failable}}
125125
return nil
126126
}
127127
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Property Wrapper Implementation Requirements
2+
---
3+
If a type is marked with the `@propertyWrapper` attribute, it must meet certain requirements to be a valid property wrapper.
4+
5+
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.
6+
7+
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.

0 commit comments

Comments
 (0)