diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 1bf01e02f425b..3b813f0d05dec 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -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'", ()) diff --git a/lib/Sema/TypeCheckPropertyWrapper.cpp b/lib/Sema/TypeCheckPropertyWrapper.cpp index 1a59c2899d29b..0f8a0a250fa87 100644 --- a/lib/Sema/TypeCheckPropertyWrapper.cpp +++ b/lib/Sema/TypeCheckPropertyWrapper.cpp @@ -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) { diff --git a/test/decl/var/property_wrappers_top_level.swift b/test/decl/var/property_wrappers_top_level.swift new file mode 100644 index 0000000000000..35ed97d70020d --- /dev/null +++ b/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 { + 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()