Skip to content

Commit

Permalink
[Property Wrappers] Allow property wrappers on local variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
hborla committed Sep 29, 2020
1 parent 9bd3d0b commit d8df621
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5202,8 +5202,6 @@ ERROR(property_wrapper_mutating_get_composed_to_get_only,none,
"property wrapper %0 with a mutating getter cannot be composed inside "
"get-only property wrapper %1", (TypeLoc, TypeLoc))

ERROR(property_wrapper_local,none,
"property wrappers are not yet supported on local properties", ())
ERROR(property_wrapper_top_level,none,
"property wrappers are not yet supported in top-level code", ())
ERROR(property_wrapper_let, none,
Expand Down
6 changes: 0 additions & 6 deletions lib/Sema/TypeCheckPropertyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,6 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
// Check various restrictions on which properties can have wrappers
// attached to them.

// Local properties do not yet support wrappers.
if (var->getDeclContext()->isLocalContext()) {
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_local);
continue;
}

// Nor does top-level code.
if (var->getDeclContext()->isModuleScopeContext()) {
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_top_level);
Expand Down
29 changes: 26 additions & 3 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,39 @@ struct _UppercaseWrapper<T> {
}

// ---------------------------------------------------------------------------
// Limitations on where property wrappers can be used
// Local property wrappers
// ---------------------------------------------------------------------------

func testLocalContext() {
@WrapperWithInitialValue // expected-error{{property wrappers are not yet supported on local properties}}
@WrapperWithInitialValue
var x = 17
x = 42
_ = x
let _: Int = x
let _: WrapperWithInitialValue = _x

@WrapperWithInitialValue(wrappedValue: 17)
var initialValue
let _: Int = initialValue
let _: WrapperWithInitialValue = _initialValue

@Clamping(min: 0, max: 100)
var percent = 50
let _: Int = percent
let _: Clamping = _percent

@WrapperA @WrapperB
var composed = "hello"
let _: WrapperA<WrapperB> = _composed

@WrapperWithStorageRef
var hasProjection = 10
let _: Wrapper = $hasProjection
}

// ---------------------------------------------------------------------------
// Limitations on where property wrappers can be used
// ---------------------------------------------------------------------------

enum SomeEnum {
case foo

Expand Down

0 comments on commit d8df621

Please sign in to comment.