Skip to content

Commit db025ed

Browse files
committed
[Macros] Ensure imported macros work with top-level freestanding macros
1 parent fb91bb6 commit db025ed

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed
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")

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/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)