Skip to content

Commit f6613e3

Browse files
committed
Make expansion(of:in:) throwing to match the latest proposal
1 parent 51d06bf commit f6613e3

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Sources/_SwiftSyntaxMacros/ExpressionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ public protocol ExpressionMacro: Macro {
2121
static func expansion(
2222
of node: MacroExpansionExprSyntax,
2323
in context: inout MacroExpansionContext
24-
) -> ExprSyntax
24+
) throws -> ExprSyntax
2525
}

Sources/_SwiftSyntaxMacros/Syntax+MacroEvaluation.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
import SwiftDiagnostics
1414
import SwiftSyntax
1515

16+
/// Diagnostic message used for thrown errors.
17+
struct ThrownErrorDiagnostic: DiagnosticMessage {
18+
let message: String
19+
20+
var severity: DiagnosticSeverity { .error }
21+
22+
var diagnosticID: MessageID {
23+
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
24+
}
25+
}
26+
1627
extension MacroExpansionExprSyntax {
1728
private func disconnectedCopy() -> MacroExpansionExprSyntax {
1829
MacroExpansionExprSyntax(
@@ -41,7 +52,19 @@ extension MacroExpansionExprSyntax {
4152
}
4253

4354
// Handle the rewrite.
44-
return exprMacro.expansion(of: disconnectedCopy(), in: &context)
55+
do {
56+
return try exprMacro.expansion(of: disconnectedCopy(), in: &context)
57+
} catch {
58+
// Record the error
59+
context.diagnose(
60+
Diagnostic(
61+
node: Syntax(self),
62+
message: ThrownErrorDiagnostic(message: String(describing: error))
63+
)
64+
)
65+
66+
return ExprSyntax(self)
67+
}
4568
}
4669
}
4770

0 commit comments

Comments
 (0)