Skip to content

Commit 6ab3bc2

Browse files
committed
Ensure that we lazily find macro-unique names in nominal type context
1 parent 3c779e7 commit 6ab3bc2

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/AST/NameLookup.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,7 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12551255
}
12561256

12571257
bool isLazilyCompleteForMacroExpansion(DeclName name) const {
1258+
assert(!MacroDecl::isUniqueMacroName(name.getBaseName()));
12581259
// If we've already expanded macros for a simple name, we must have expanded
12591260
// all macros that produce names with the same base identifier.
12601261
bool isBaseNameComplete = name.isCompoundName() &&
@@ -1264,6 +1265,7 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12641265
}
12651266

12661267
void markLazilyCompleteForMacroExpansion(DeclName name) {
1268+
assert(!MacroDecl::isUniqueMacroName(name.getBaseName()));
12671269
LazilyCompleteNamesForMacroExpansion.insert(name);
12681270
}
12691271

@@ -1521,6 +1523,17 @@ populateLookupTableEntryFromExtensions(ASTContext &ctx,
15211523
}
15221524
}
15231525

1526+
/// Adjust the given name to make it a proper key for the lazy macro expansion
1527+
/// cache, which maps all uniquely-generated names down to a single placeholder
1528+
/// key.
1529+
static DeclName adjustLazyMacroExpansionNameKey(
1530+
ASTContext &ctx, DeclName name) {
1531+
if (MacroDecl::isUniqueMacroName(name.getBaseName()))
1532+
return MacroDecl::getUniqueNamePlaceholder(ctx);
1533+
1534+
return name;
1535+
}
1536+
15241537
static void
15251538
populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
15261539
MemberLookupTable &table,
@@ -1756,9 +1769,11 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
17561769
Table.markLazilyComplete(baseName);
17571770
}
17581771

1759-
if (!Table.isLazilyCompleteForMacroExpansion(name)) {
1760-
populateLookupTableEntryFromMacroExpansions(ctx, Table, name, decl);
1761-
Table.markLazilyCompleteForMacroExpansion(name);
1772+
DeclName macroExpansionKey = adjustLazyMacroExpansionNameKey(ctx, name);
1773+
if (!Table.isLazilyCompleteForMacroExpansion(macroExpansionKey)) {
1774+
populateLookupTableEntryFromMacroExpansions(
1775+
ctx, Table, macroExpansionKey, decl);
1776+
Table.markLazilyCompleteForMacroExpansion(macroExpansionKey);
17621777
}
17631778

17641779
// Look for a declaration with this name.

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ public struct AddMembers: MemberMacro {
456456
providingMembersOf decl: some DeclGroupSyntax,
457457
in context: some MacroExpansionContext
458458
) throws -> [DeclSyntax] {
459+
let uniqueClassName = context.createUniqueName("uniqueClass")
460+
459461
let storageStruct: DeclSyntax =
460462
"""
461463
struct Storage {}
@@ -481,7 +483,12 @@ public struct AddMembers: MemberMacro {
481483

482484
let initDecl: DeclSyntax =
483485
"""
484-
init() {}
486+
init() { _ = \(uniqueClassName)() }
487+
"""
488+
489+
let classDecl: DeclSyntax =
490+
"""
491+
class \(uniqueClassName) { }
485492
"""
486493

487494
return [
@@ -490,6 +497,7 @@ public struct AddMembers: MemberMacro {
490497
instanceMethod,
491498
staticMethod,
492499
initDecl,
500+
classDecl,
493501
]
494502
}
495503
}

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
33
// RUNx: %target-swift-frontend -dump-ast -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser 2>&1 | %FileCheck --check-prefix CHECK-AST %s
44
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5
5-
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser -swift-version 5
5+
// RUN: %target-build-swift -g -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser -swift-version 5
66
// RUN: %target-run %t/main | %FileCheck %s
77
// REQUIRES: executable_test
88

0 commit comments

Comments
 (0)