Skip to content

Commit d8df621

Browse files
committed
[Property Wrappers] Allow property wrappers on local variables.
1 parent 9bd3d0b commit d8df621

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,8 +5202,6 @@ ERROR(property_wrapper_mutating_get_composed_to_get_only,none,
52025202
"property wrapper %0 with a mutating getter cannot be composed inside "
52035203
"get-only property wrapper %1", (TypeLoc, TypeLoc))
52045204

5205-
ERROR(property_wrapper_local,none,
5206-
"property wrappers are not yet supported on local properties", ())
52075205
ERROR(property_wrapper_top_level,none,
52085206
"property wrappers are not yet supported in top-level code", ())
52095207
ERROR(property_wrapper_let, none,

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,6 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
414414
// Check various restrictions on which properties can have wrappers
415415
// attached to them.
416416

417-
// Local properties do not yet support wrappers.
418-
if (var->getDeclContext()->isLocalContext()) {
419-
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_local);
420-
continue;
421-
}
422-
423417
// Nor does top-level code.
424418
if (var->getDeclContext()->isModuleScopeContext()) {
425419
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_top_level);

test/decl/var/property_wrappers.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,39 @@ struct _UppercaseWrapper<T> {
140140
}
141141

142142
// ---------------------------------------------------------------------------
143-
// Limitations on where property wrappers can be used
143+
// Local property wrappers
144144
// ---------------------------------------------------------------------------
145145

146146
func testLocalContext() {
147-
@WrapperWithInitialValue // expected-error{{property wrappers are not yet supported on local properties}}
147+
@WrapperWithInitialValue
148148
var x = 17
149149
x = 42
150-
_ = x
150+
let _: Int = x
151+
let _: WrapperWithInitialValue = _x
152+
153+
@WrapperWithInitialValue(wrappedValue: 17)
154+
var initialValue
155+
let _: Int = initialValue
156+
let _: WrapperWithInitialValue = _initialValue
157+
158+
@Clamping(min: 0, max: 100)
159+
var percent = 50
160+
let _: Int = percent
161+
let _: Clamping = _percent
162+
163+
@WrapperA @WrapperB
164+
var composed = "hello"
165+
let _: WrapperA<WrapperB> = _composed
166+
167+
@WrapperWithStorageRef
168+
var hasProjection = 10
169+
let _: Wrapper = $hasProjection
151170
}
152171

172+
// ---------------------------------------------------------------------------
173+
// Limitations on where property wrappers can be used
174+
// ---------------------------------------------------------------------------
175+
153176
enum SomeEnum {
154177
case foo
155178

0 commit comments

Comments
 (0)