Skip to content

Commit acc8a08

Browse files
authored
Merge pull request #65670 from DougGregor/freestanding-member-lookup
[Macros] Fix lazy expansion of freestanding macros within types/extensions
2 parents 4ddc1f4 + a690fc4 commit acc8a08

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,6 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
16201620
MemberLookupTable &table,
16211621
DeclName name,
16221622
TypeOrExtensionDecl container) {
1623-
16241623
// Trigger the expansion of member macros on the container, if any of the
16251624
// names match.
16261625
{
@@ -1645,7 +1644,7 @@ populateLookupTableEntryFromMacroExpansions(ASTContext &ctx,
16451644
MacroIntroducedNameTracker nameTracker;
16461645
if (auto *med = dyn_cast<MacroExpansionDecl>(member)) {
16471646
forEachPotentialResolvedMacro(
1648-
member->getModuleContext(), med->getMacroName(),
1647+
dc->getModuleScopeContext(), med->getMacroName(),
16491648
MacroRole::Declaration, nameTracker);
16501649
} else if (auto *vd = dyn_cast<ValueDecl>(member)) {
16511650
nameTracker.attachedTo = dyn_cast<ValueDecl>(member);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
2+
public macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
3+
4+
@freestanding(declaration)
5+
public macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
6+
7+
@freestanding(declaration)
8+
public macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
9+
10+
@freestanding(declaration, names: arbitrary)
11+
public macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
12+
13+
@freestanding(expression)
14+
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import freestanding_macro_library
2+
3+
@freestanding(declaration, names: arbitrary)
4+
public macro bitwidthNumberedStructs(_ baseName: String, blah: Bool) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")

test/Macros/Inputs/top_level_freestanding_other.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#if IMPORT_MACRO_LIBRARY
2+
import freestanding_macro_library
3+
#endif
4+
15
#anonymousTypes { "hello2" }
26

37
var globalVar = #stringify(1 + 1)

test/Macros/macro_expand.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
// Diagnostics testing
77
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS
88

9+
// Diagnostics testing by importing macros from a module
10+
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library.swiftmodule %S/Inputs/freestanding_macro_library.swift -module-name freestanding_macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
11+
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library_2.swiftmodule %S/Inputs/freestanding_macro_library_2.swift -module-name freestanding_macro_library_2 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %t
12+
13+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -I %t -DIMPORT_MACRO_LIBRARY
14+
915
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics > %t/macro-printing.txt
1016
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s
1117

@@ -28,6 +34,26 @@
2834

2935
// CHECK-MODULE-TRACE: {{libMacroDefinition.dylib|libMacroDefinition.so|MacroDefinition.dll}}
3036

37+
#if IMPORT_MACRO_LIBRARY
38+
import freestanding_macro_library
39+
import freestanding_macro_library_2
40+
#else
41+
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
42+
macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
43+
44+
@freestanding(declaration)
45+
macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
46+
47+
@freestanding(declaration)
48+
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
49+
50+
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
51+
52+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
53+
54+
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String, blah: Bool) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
55+
#endif
56+
3157
#if TEST_DIAGNOSTICS
3258
@freestanding(declaration)
3359
macro NotCovered() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro")
@@ -93,7 +119,6 @@ struct Bad {}
93119
#endif
94120

95121
@freestanding(expression) macro customFileID() -> String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
96-
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
97122
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>() -> T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
98123
@freestanding(expression) macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
99124
@freestanding(expression) macro assert(_: Bool) = #externalMacro(module: "MacroDefinition", type: "AssertMacro")
@@ -267,9 +292,6 @@ func testNestedDeclInExpr() {
267292
let _: () -> Void = #nestedDeclInExpr
268293
}
269294

270-
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
271-
// Test overload
272-
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String, blah: Bool) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
273295
// Test non-arbitrary names
274296
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne))
275297
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro")
@@ -288,12 +310,6 @@ let blah = false
288310
#endif
289311

290312
// Test unqualified lookup from within a macro expansion
291-
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
292-
macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
293-
294-
@freestanding(declaration)
295-
macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
296-
297313
// FIXME: Global freestanding macros not yet supported in script mode.
298314
#if false
299315
let world = 3 // to be used by the macro expansion below
@@ -376,9 +392,6 @@ struct ContainerOfNumberedStructs {
376392
}
377393

378394
// Avoid re-type-checking declaration macro arguments.
379-
@freestanding(declaration)
380-
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
381-
382395
func testFreestandingWithClosure(i: Int) {
383396
#freestandingWithClosure(i) { x in x }
384397

test/Macros/top_level_freestanding.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
// Type check testing
88
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 %S/Inputs/top_level_freestanding_other.swift
99

10+
// Type check testing with imported macro declarations
11+
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/freestanding_macro_library.swiftmodule %S/Inputs/freestanding_macro_library.swift -module-name freestanding_macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
12+
13+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -DIMPORT_MACRO_LIBRARY -swift-version 5 %S/Inputs/top_level_freestanding_other.swift -I %t
14+
1015
// Check diagnostic buffer names
1116
// RUN: %target-swift-frontend -typecheck -swift-version 5 -enable-experimental-feature FreestandingMacros -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 %s %S/Inputs/top_level_freestanding_other.swift 2> %t.diags
1217
// RUN: %FileCheck -check-prefix DIAG_BUFFERS %s < %t.diags
@@ -16,9 +21,20 @@
1621
// RUN: %target-codesign %t/main
1722
// RUN: %target-run %t/main | %FileCheck %s
1823

19-
// Test unqualified lookup from within a macro expansion
24+
#if IMPORT_MACRO_LIBRARY
25+
import freestanding_macro_library
26+
#else
2027
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
2128
macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
29+
@freestanding(declaration)
30+
macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
31+
@freestanding(declaration)
32+
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
33+
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
34+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
35+
#endif
36+
37+
// Test unqualified lookup from within a macro expansion
2238

2339
let world = 3 // to be used by the macro expansion below
2440

@@ -29,9 +45,6 @@ func lookupGlobalFreestandingExpansion() {
2945
print(StructWithUnqualifiedLookup().foo())
3046
}
3147

32-
@freestanding(declaration)
33-
macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
34-
3548
#anonymousTypes(public: true) { "hello" }
3649

3750
// CHECK-SIL: sil @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF
@@ -43,9 +56,6 @@ struct Main {
4356
}
4457
}
4558

46-
@freestanding(declaration)
47-
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
48-
4959
// Unqualified lookup for names defined within macro arguments.
5060
#freestandingWithClosure(0) { x in x }
5161

@@ -61,15 +71,11 @@ struct HasInnerClosure {
6171

6272
// Arbitrary names at global scope
6373

64-
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
65-
6674
#bitwidthNumberedStructs("MyIntGlobal")
6775

6876
func testArbitraryAtGlobal() {
6977
_ = MyIntGlobal16()
7078
}
7179

72-
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
73-
7480
// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf1_{{.*}}warning: 'deprecated()' is deprecated
7581
// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf2_{{.*}}warning: 'deprecated()' is deprecated

0 commit comments

Comments
 (0)