Skip to content

Commit cadc891

Browse files
committed
[Macros] Make sure to avoid full macro resolution in name lookup.
When populating the name lookup tables with macro-expanded declarations, we were careful to avoid full macro resolution for attached macros, but freestanding macros were will doing full resolution. Switch freestanding macros over to the same partial resolution mechanism to avoid reference cycles. (cherry picked from commit e436553)
1 parent 54b4b7d commit cadc891

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,13 +1639,9 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
16391639
// that weren't introduced by the macro.
16401640
MacroIntroducedNameTracker nameTracker;
16411641
if (auto *med = dyn_cast<MacroExpansionDecl>(member)) {
1642-
auto declRef = evaluateOrDefault(
1643-
ctx.evaluator, ResolveMacroRequest{med, dc},
1644-
nullptr);
1645-
if (!declRef)
1646-
continue;
1647-
auto *macro = dyn_cast<MacroDecl>(declRef.getDecl());
1648-
nameTracker(macro, macro->getMacroRoleAttr(MacroRole::Declaration));
1642+
forEachPotentialResolvedMacro(
1643+
member->getModuleContext(), med->getMacroName(),
1644+
MacroRole::Declaration, nameTracker);
16491645
} else if (auto *vd = dyn_cast<ValueDecl>(member)) {
16501646
nameTracker.attachedTo = dyn_cast<ValueDecl>(member);
16511647
forEachPotentialAttachedMacro(member, MacroRole::Peer, nameTracker);

test/Macros/macro_expand.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func testFreestandingMacroExpansion() {
335335
}
336336
testFreestandingMacroExpansion()
337337

338+
// Explicit structs to force macros to be parsed as decl.
339+
struct ContainerOfNumberedStructs {
340+
#bitwidthNumberedStructs("MyIntOne")
341+
#bitwidthNumberedStructs("MyIntTwo")
342+
}
343+
338344
// Avoid re-type-checking declaration macro arguments.
339345
@freestanding(declaration)
340346
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")

test/Macros/top_level_freestanding.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(mo
5050
let x = $0
5151
return x
5252
}
53+
54+
struct HasInnerClosure {
55+
#freestandingWithClosure(0) { x in x }
56+
#freestandingWithClosure(1) { x in x }
57+
}

0 commit comments

Comments
 (0)