Skip to content

Commit 638c2cd

Browse files
committed
[Walker] Don't visit VarDecls encountered in freestanding macro expansions
VarDecls are always walked via their pattern bindings, so we end up with double-visitation if we also visit them here. Fixes rdar://109376102.
1 parent 7386a7e commit 638c2cd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
460460
if (shouldWalkExpansion) {
461461
MED->visitAuxiliaryDecls([&](Decl *decl) {
462462
if (alreadyFailed) return;
463-
alreadyFailed = inherited::visit(decl);
463+
if (!isa<VarDecl>(decl))
464+
alreadyFailed = inherited::visit(decl);
464465
});
465466
MED->forEachExpandedExprOrStmt([&](ASTNode expandedNode) {
466467
if (alreadyFailed) return;

test/Macros/macro_expand.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(mo
5252
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
5353

5454
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String, blah: Bool) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
55+
56+
@freestanding(declaration, names: named(value)) macro varValue() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
57+
5558
#endif
5659

5760
#if TEST_DIAGNOSTICS
@@ -409,3 +412,8 @@ func testFreestandingClosureNesting() {
409412
#coerceToInt(2)
410413
})
411414
}
415+
416+
// Freestanding declaration macros that produce local variables
417+
func testLocalVarsFromDeclarationMacros() {
418+
#varValue
419+
}

0 commit comments

Comments
 (0)