Skip to content

[SE-0258] Diagnose uses of property wrappers in top-level code. #26043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()