Skip to content

Commit 12d89a9

Browse files
committed
[Module-scope lookup] Only add macro-expanded names that match.
When we look into expanded macros to find declarations for module-scope name lookup, make sure that we only return declarations whose name matches the requested name. We were effectively spamming the results with unrelated names, and most clients trust the results of this lookup (vs. filtering them further), so they would get confused. The particular failure for this test is that type reconstruction would fail because we were looking for one unique name, but we found many non-matching names, and type reconstruction bails out rather than try to filter further. (cherry picked from commit 8e63bf1)
1 parent bc9f9e1 commit 12d89a9

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

lib/AST/Module.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,12 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
444444
return;
445445

446446
for (auto *unexpandedDecl : auxDecls->second) {
447-
// Add expanded peers to the result.
447+
// Add expanded peers and freestanding declarations to the result.
448448
unexpandedDecl->forEachMacroExpandedDecl(
449449
[&](ValueDecl *decl) {
450-
Result.push_back(decl);
450+
if (decl->getName().matchesRef(Name)) {
451+
Result.push_back(decl);
452+
}
451453
});
452454
}
453455
}

test/Macros/macro_expand_peers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
1414
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
1515

16-
// RUN: %target-build-swift -swift-version 5 -Xfrontend -disable-availability-checking -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -o %t/main -module-name MacroUser
16+
// RUN: %target-build-swift -swift-version 5 -Xfrontend -disable-availability-checking -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -o %t/main -module-name MacroUser -g
1717
// RUN: %target-codesign %t/main
1818
// RUN: %target-run %t/main | %FileCheck %s -check-prefix=CHECK-EXEC
1919

0 commit comments

Comments
 (0)