Skip to content

Commit ddf6cf9

Browse files
authored
Merge pull request #68088 from DougGregor/accessor-macro-single-binding-restriction-5.9
[5.9] Ban the use of accessor macros on bindings with multiple variables in a single binding
2 parents c9e8cd0 + 79e3589 commit ddf6cf9

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
@@ -7236,6 +7236,9 @@ ERROR(macro_expand_circular_reference_unnamed, none,
72367236
NOTE(macro_expand_circular_reference_unnamed_through, none,
72377237
"circular reference expanding %0 macros", (StringRef))
72387238

7239+
ERROR(accessor_macro_not_single_var, none,
7240+
"accessor macro %0 can only apply to a single variable", (DeclName))
7241+
72397242
//------------------------------------------------------------------------------
72407243
// MARK: Noncopyable Types Diagnostics
72417244
//------------------------------------------------------------------------------

lib/Sema/TypeCheckMacros.cpp

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