Skip to content

Ban the use of accessor macros on bindings with multiple variables in a single binding #68087

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
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7290,6 +7290,9 @@ ERROR(macro_expand_circular_reference_unnamed, none,
NOTE(macro_expand_circular_reference_unnamed_through, none,
"circular reference expanding %0 macros", (StringRef))

ERROR(accessor_macro_not_single_var, none,
"accessor macro %0 can only apply to a single variable", (DeclName))

//------------------------------------------------------------------------------
// MARK: Noncopyable Types Diagnostics
//------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,15 @@ bool swift::accessorMacroIntroducesInitAccessor(
llvm::Optional<unsigned> swift::expandAccessors(AbstractStorageDecl *storage,
CustomAttr *attr,
MacroDecl *macro) {
if (auto var = dyn_cast<VarDecl>(storage)) {
// Check that the variable is part of a single-variable pattern.
auto binding = var->getParentPatternBinding();
if (binding && binding->getSingleVar() != var) {
var->diagnose(diag::accessor_macro_not_single_var, macro->getName());
return llvm::None;
}
}

// Evaluate the macro.
auto macroSourceFile =
::evaluateAttachedMacro(macro, storage, attr,
Expand Down
8 changes: 8 additions & 0 deletions test/Macros/accessor_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ struct HasStoredTests {
// expected-note@-3 2{{'z' declared here}}
#endif
}


#if TEST_DIAGNOSTICS
struct MultipleVars {
@AddWillSet var (x, y): (Int, Int) = (0, 0)
// expected-error@-1 2{{accessor macro 'AddWillSet()' can only apply to a single variable}}
}
#endif