Skip to content

Commit 8ed1853

Browse files
committed
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.
1 parent c48836c commit 8ed1853

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7290,6 +7290,9 @@ ERROR(macro_expand_circular_reference_unnamed, none,
72907290
NOTE(macro_expand_circular_reference_unnamed_through, none,
72917291
"circular reference expanding %0 macros", (StringRef))
72927292

7293+
ERROR(accessor_macro_not_single_var, none,
7294+
"accessor macro %0 can only apply to a single variable", (DeclName))
7295+
72937296
//------------------------------------------------------------------------------
72947297
// MARK: Noncopyable Types Diagnostics
72957298
//------------------------------------------------------------------------------

lib/Sema/TypeCheckMacros.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,15 @@ bool swift::accessorMacroIntroducesInitAccessor(
13981398
llvm::Optional<unsigned> swift::expandAccessors(AbstractStorageDecl *storage,
13991399
CustomAttr *attr,
14001400
MacroDecl *macro) {
1401+
if (auto var = dyn_cast<VarDecl>(storage)) {
1402+
// Check that the variable is part of a single-variable pattern.
1403+
auto binding = var->getParentPatternBinding();
1404+
if (binding && binding->getSingleVar() != var) {
1405+
var->diagnose(diag::accessor_macro_not_single_var, macro->getName());
1406+
return llvm::None;
1407+
}
1408+
}
1409+
14011410
// Evaluate the macro.
14021411
auto macroSourceFile =
14031412
::evaluateAttachedMacro(macro, storage, attr,

test/Macros/accessor_macros.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ struct HasStoredTests {
155155
// expected-note@-3 2{{'z' declared here}}
156156
#endif
157157
}
158+
159+
160+
#if TEST_DIAGNOSTICS
161+
struct MultipleVars {
162+
@AddWillSet var (x, y): (Int, Int) = (0, 0)
163+
// expected-error@-1 2{{accessor macro 'AddWillSet()' can only apply to a single variable}}
164+
}
165+
#endif

0 commit comments

Comments
 (0)