Skip to content

Commit 249a369

Browse files
committed
Arguments are weird
1 parent b09a22a commit 249a369

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,13 +2043,36 @@ public struct ClosureCallerMacro: ExpressionMacro {
20432043
}
20442044
}
20452045

2046-
public struct FirstArgumentConcatStringMacro: ExpressionMacro {
2046+
public struct PrependHelloMacro: ExpressionMacro {
20472047
public static func expansion(
20482048
of node: some FreestandingMacroExpansionSyntax,
20492049
in context: some MacroExpansionContext
20502050
) -> ExprSyntax {
20512051
"""
2052-
\(node.arguments.first!.expression) + " world"
2052+
"hello " + \(node.arguments.first!.expression)
2053+
"""
2054+
}
2055+
}
2056+
2057+
public struct AsIsMacro: ExpressionMacro {
2058+
public static func expansion(
2059+
of node: some FreestandingMacroExpansionSyntax,
2060+
in context: some MacroExpansionContext
2061+
) -> ExprSyntax {
2062+
node.arguments.first!.expression
2063+
}
2064+
}
2065+
2066+
public struct IntroduceShadowedMacro: ExpressionMacro {
2067+
public static func expansion(
2068+
of node: some FreestandingMacroExpansionSyntax,
2069+
in context: some MacroExpansionContext
2070+
) -> ExprSyntax {
2071+
"""
2072+
{
2073+
let shadowed = "from shadow"
2074+
return \(node.arguments.first!.expression)
2075+
}()
20532076
"""
20542077
}
20552078
}

test/Macros/Inputs/with_macro_default_arg_interface.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ public macro FileID<T: ExpressibleByStringLiteral>() -> T = #externalMacro(
44
)
55

66
@freestanding(expression)
7-
public macro HasParam(_ param: String) -> String = #externalMacro(
8-
module: "MacroDefinition", type: "FirstArgumentConcatStringMacro"
7+
public macro PrependHello(_ param: String) -> String = #externalMacro(
8+
module: "MacroDefinition", type: "PrependHelloMacro"
9+
)
10+
11+
@freestanding(expression)
12+
public macro AsIs<T>(_ param: T) -> T = #externalMacro(
13+
module: "MacroDefinition", type: "AsIsMacro"
14+
)
15+
16+
@freestanding(expression)
17+
public macro IntroduceShadowed<T>(_ param: T) -> T = #externalMacro(
18+
module: "MacroDefinition", type: "IntroduceShadowedMacro"
919
)
1020

1121
@freestanding(expression)
@@ -31,10 +41,28 @@ public struct ClosureCaller {
3141
}
3242
}
3343

34-
public let shadowed = "hello"
44+
public let shadowed = "world"
3545

3646
public func testParameterUseVariableFromOriginalDeclContext(
37-
param: String = #HasParam(shadowed)
47+
param: String = #PrependHello(shadowed)
48+
) {
49+
print(param)
50+
}
51+
52+
public func testMacroUseMacro(
53+
param: String = #PrependHello(#fileID)
54+
) {
55+
print(param)
56+
}
57+
58+
public func testUseShadowedFromOuterExpansion(
59+
param: String = #IntroduceShadowed(#PrependHello(shadowed))
60+
) {
61+
print(param)
62+
}
63+
64+
public func testNestedStillInOriginalDeclContext(
65+
param: String = #AsIs(#PrependHello(shadowed))
3866
) {
3967
print(param)
4068
}

test/Macros/macro_default_argument_enabled.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ let printWithFileLine = buildPrinter { }
112112
testParameterUseVariableFromOriginalDeclContext()
113113
}
114114

115+
do {
116+
let shadowed: String = "not this either"
117+
// CHECK: hello world
118+
testNestedStillInOriginalDeclContext()
119+
}
120+
121+
// CHECK: hello from shadow
122+
testUseShadowedFromOuterExpansion()
123+
// CHECK: hello MacroUser/macro_default_argument_enabled.swift
124+
testMacroUseMacro()
125+
115126
// CHECK: [[# @LINE + 1]]
116127
asDefaultArgument()
117128
// CHECK: [[# @LINE + 1]]

0 commit comments

Comments
 (0)