Skip to content
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
Ban the use of accessor macros on bindings with multiple variables
As with property wrappers, we can't properly desugar the application of
accessor macros to bindings with multiple variables. Prohibit them up
front.

Fixes rdar://112783811.
  • Loading branch information
DougGregor committed Aug 23, 2023
commit 8ed1853b61123714f20b023f2a0b5edbba379603
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