Skip to content

Commit 1453e76

Browse files
committed
[Macros] Pass MacroExpansionContext as inout through macro expansion.
This matches the current proposal, and makes it possible for mutations to the context in the future.
1 parent 29f0e2f commit 1453e76

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

Sources/_SwiftSyntaxMacros/ExpressionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public protocol ExpressionMacro: Macro {
2222
/// Evaluate a macro described by the given macro expansion expression
2323
/// within the given context to produce a replacement expression.
2424
static func apply(
25-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
25+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
2626
) -> MacroResult<ExprSyntax>
2727
}
2828

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public struct FunctionMacro: ExpressionMacro {
116116
}
117117

118118
public static func apply(
119-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
119+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
120120
) -> MacroResult<ExprSyntax> {
121121
let name = findEnclosingName(macro) ?? context.moduleName
122122
let literal: ExprSyntax = "\(literal: name)"
@@ -143,7 +143,7 @@ private func replaceFirstLabel(
143143

144144
public struct ColorLiteralMacro: ExpressionMacro {
145145
public static func apply(
146-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
146+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
147147
) -> MacroResult<ExprSyntax> {
148148
let argList = replaceFirstLabel(
149149
of: macro.argumentList, with: "_colorLiteralRed"
@@ -158,7 +158,7 @@ public struct ColorLiteralMacro: ExpressionMacro {
158158

159159
public struct FileLiteralMacro: ExpressionMacro {
160160
public static func apply(
161-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
161+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
162162
) -> MacroResult<ExprSyntax> {
163163
let argList = replaceFirstLabel(
164164
of: macro.argumentList, with: "fileReferenceLiteralResourceName"
@@ -173,7 +173,7 @@ public struct FileLiteralMacro: ExpressionMacro {
173173

174174
public struct ImageLiteralMacro: ExpressionMacro {
175175
public static func apply(
176-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
176+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
177177
) -> MacroResult<ExprSyntax> {
178178
let argList = replaceFirstLabel(
179179
of: macro.argumentList, with: "imageLiteralResourceName"
@@ -188,7 +188,7 @@ public struct ImageLiteralMacro: ExpressionMacro {
188188

189189
public struct FileIDMacro: ExpressionMacro {
190190
public static func apply(
191-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
191+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
192192
) -> MacroResult<ExprSyntax> {
193193
// FIXME: Compiler has more sophisticated file ID computation
194194
let fileID = "\(context.moduleName)/\(context.fileName)"

Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616
// Macros used for testing purposes
1717
public struct StringifyMacro: ExpressionMacro {
1818
public static func apply(
19-
_ macro: MacroExpansionExprSyntax, in context: MacroExpansionContext
19+
_ macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
2020
) -> MacroResult<ExprSyntax> {
2121
guard let argument = macro.argumentList.first?.expression else {
2222
// FIXME: Create a diagnostic for the missing argument?

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public struct MacroSystem {
6262
/// Syntax rewriter that evaluates any macros encountered along the way.
6363
class MacroApplication : SyntaxRewriter {
6464
let macroSystem: MacroSystem
65-
let context: MacroExpansionContext
65+
var context: MacroExpansionContext
6666
let errorHandler: (MacroSystemError) -> Void
6767

6868
init(
@@ -81,7 +81,7 @@ class MacroApplication : SyntaxRewriter {
8181
}
8282

8383
return node.evaluateMacro(
84-
with: macroSystem, context: context, errorHandler: errorHandler
84+
with: macroSystem, context: &context, errorHandler: errorHandler
8585
)
8686
}
8787

@@ -113,7 +113,7 @@ extension MacroSystem {
113113
/// - Returns: the syntax tree with all macros evaluated.
114114
public func evaluateMacros<Node: SyntaxProtocol>(
115115
node: Node,
116-
in context: MacroExpansionContext,
116+
in context: inout MacroExpansionContext,
117117
errorHandler: (MacroSystemError) -> Void
118118
) -> Syntax {
119119
return withoutActuallyEscaping(errorHandler) { errorHandler in

Sources/_SwiftSyntaxMacros/Syntax+MacroEvaluation.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension MacroExpansionExprSyntax {
1818
/// result and (possibly) some diagnostics.
1919
func evaluateMacro(
2020
_ macro: Macro.Type,
21-
in context: MacroExpansionContext,
21+
in context: inout MacroExpansionContext,
2222
errorHandler: (MacroSystemError) -> Void
2323
) -> ExprSyntax {
2424
guard let exprMacro = macro as? ExpressionMacro.Type else {
@@ -27,7 +27,7 @@ extension MacroExpansionExprSyntax {
2727
}
2828

2929
// Handle the rewrite.
30-
let result = exprMacro.apply(self, in: context)
30+
let result = exprMacro.apply(self, in: &context)
3131

3232
// Report diagnostics, if there were any.
3333
if !result.diagnostics.isEmpty {
@@ -65,15 +65,15 @@ extension MacroExpansionDeclSyntax {
6565
/// result and (possibly) some diagnostics.
6666
func evaluateMacro(
6767
_ macro: Macro.Type,
68-
in context: MacroExpansionContext,
68+
in context: inout MacroExpansionContext,
6969
errorHandler: (MacroSystemError) -> Void
7070
) -> Syntax {
7171
// TODO: declaration/statement macros
7272

7373
// Fall back to evaluating as an expression macro.
7474
return Syntax(
7575
asMacroExpansionExpr().evaluateMacro(
76-
macro, in: context, errorHandler: errorHandler
76+
macro, in: &context, errorHandler: errorHandler
7777
)
7878
)
7979
}
@@ -104,7 +104,7 @@ extension Syntax {
104104
/// some kind.
105105
public func evaluateMacro(
106106
with macroSystem: MacroSystem,
107-
context: MacroExpansionContext,
107+
context: inout MacroExpansionContext,
108108
errorHandler: (MacroSystemError) -> Void
109109
) -> Syntax {
110110
// If this isn't a macro evaluation node, do nothing.
@@ -121,13 +121,13 @@ extension Syntax {
121121
switch self.as(SyntaxEnum.self) {
122122
case .macroExpansionDecl(let expansion):
123123
return expansion.evaluateMacro(
124-
macro, in: context, errorHandler: errorHandler
124+
macro, in: &context, errorHandler: errorHandler
125125
)
126126

127127
case .macroExpansionExpr(let expansion):
128128
return Syntax(
129129
expansion.evaluateMacro(
130-
macro, in: context, errorHandler: errorHandler
130+
macro, in: &context, errorHandler: errorHandler
131131
)
132132
)
133133

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ class ExpandMacros: ParsableCommand {
242242
resultTree = Syntax(tree)
243243
}
244244

245-
let context = MacroExpansionContext(
245+
var context = MacroExpansionContext(
246246
moduleName: "MyModule", fileName: self.sourceFile.withoutPath()
247247
)
248248
var diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
249249
let transformedTree = MacroSystem.exampleSystem.evaluateMacros(
250-
node: resultTree, in: context
250+
node: resultTree, in: &context
251251
) { error in
252252
if case let .evaluationDiagnostics(_, diagnostics) = error {
253253
diags.append(contentsOf: diagnostics)

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ final class MacroSystemTests: XCTestCase {
2323
let b = #stringify(x + y)
2424
#colorLiteral(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
2525
"""
26-
let context = MacroExpansionContext(
26+
var context = MacroExpansionContext(
2727
moduleName: "MyModule", fileName: "test.swift"
2828
)
2929
let transformedSF = MacroSystem.exampleSystem.evaluateMacros(
30-
node: sf, in: context, errorHandler: { error in }
30+
node: sf, in: &context, errorHandler: { error in }
3131
)
3232
AssertStringsEqualWithDiff(
3333
transformedSF.description,
@@ -46,11 +46,11 @@ final class MacroSystemTests: XCTestCase {
4646
return true
4747
})
4848
"""
49-
let context = MacroExpansionContext(
49+
var context = MacroExpansionContext(
5050
moduleName: "MyModule", fileName: "test.swift"
5151
)
5252
let transformedSF = MacroSystem.exampleSystem.evaluateMacros(
53-
node: sf, in: context, errorHandler: { error in }
53+
node: sf, in: &context, errorHandler: { error in }
5454
)
5555
AssertStringsEqualWithDiff(
5656
transformedSF.description,
@@ -94,11 +94,11 @@ final class MacroSystemTests: XCTestCase {
9494
static var staticProp: String = #function
9595
}
9696
"""
97-
let context = MacroExpansionContext(
97+
var context = MacroExpansionContext(
9898
moduleName: "MyModule", fileName: "test.swift"
9999
)
100100
let transformedSF = MacroSystem.exampleSystem.evaluateMacros(
101-
node: sf, in: context, errorHandler: { error in }
101+
node: sf, in: &context, errorHandler: { error in }
102102
)
103103
AssertStringsEqualWithDiff(
104104
transformedSF.description,
@@ -139,11 +139,11 @@ final class MacroSystemTests: XCTestCase {
139139
"""
140140
let b = #fileID
141141
"""
142-
let context = MacroExpansionContext(
142+
var context = MacroExpansionContext(
143143
moduleName: "MyModule", fileName: "taylor.swift"
144144
)
145145
let transformedSF = MacroSystem.exampleSystem.evaluateMacros(
146-
node: sf, in: context, errorHandler: { error in }
146+
node: sf, in: &context, errorHandler: { error in }
147147
)
148148
AssertStringsEqualWithDiff(
149149
transformedSF.description,

0 commit comments

Comments
 (0)