Skip to content

Commit

Permalink
Merge pull request #26043 from DougGregor/property-wrappers-top-level…
Browse files Browse the repository at this point in the history
…-error

[SE-0258] Diagnose uses of property wrappers in top-level code.
  • Loading branch information
DougGregor committed Jul 10, 2019
2 parents b77499f + 02ff2ee commit 3954a4a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Expand Up @@ -4463,6 +4463,8 @@ NOTE(property_wrapper_declared_here,none,

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,
"property wrapper can only be applied to a 'var'",
())
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckPropertyWrapper.cpp
Expand Up @@ -401,6 +401,12 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
continue;
}

// Nor does top-level code.
if (var->getDeclContext()->isModuleScopeContext()) {
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_top_level);
continue;
}

// Check that the variable is part of a single-variable pattern.
auto binding = var->getParentPatternBinding();
if (!binding || binding->getSingleVar() != var) {
Expand Down
15 changes: 15 additions & 0 deletions test/decl/var/property_wrappers_top_level.swift
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -typecheck -primary-file %s -verify -module-name main

@propertyWrapper
struct Wrapper<T> {
var wrappedValue: T
init(initialValue: T) {
wrappedValue = initialValue
}
}

// expected-error@+1{{property wrappers are not yet supported in top-level code}}
@Wrapper var value: Int = 17

func f() { }
f()

0 comments on commit 3954a4a

Please sign in to comment.