13
13
import SwiftDiagnostics
14
14
import SwiftSyntax
15
15
16
- extension MacroExpansionExprSyntax {
17
- private func disconnectedCopy( ) -> MacroExpansionExprSyntax {
18
- MacroExpansionExprSyntax (
19
- unexpectedBeforePoundToken, poundToken: poundToken,
20
- unexpectedBetweenPoundTokenAndMacro, macro: macro,
21
- genericArguments: genericArguments,
22
- unexpectedBetweenGenericArgumentsAndLeftParen, leftParen: leftParen,
23
- unexpectedBetweenLeftParenAndArgumentList, argumentList: argumentList,
24
- unexpectedBetweenArgumentListAndRightParen, rightParen: rightParen,
25
- unexpectedBetweenRightParenAndTrailingClosure,
26
- trailingClosure: trailingClosure,
27
- unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures,
28
- additionalTrailingClosures: additionalTrailingClosures,
29
- unexpectedAfterAdditionalTrailingClosures
30
- )
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 " )
31
24
}
25
+ }
32
26
27
+ extension MacroExpansionExprSyntax {
33
28
/// Evaluate the given macro for this syntax node, producing the expanded
34
29
/// result and (possibly) some diagnostics.
35
30
func evaluateMacro(
@@ -41,7 +36,19 @@ extension MacroExpansionExprSyntax {
41
36
}
42
37
43
38
// Handle the rewrite.
44
- return exprMacro. expansion ( of: disconnectedCopy ( ) , in: & context)
39
+ do {
40
+ return try exprMacro. expansion ( of: detach ( ) , in: & context)
41
+ } catch {
42
+ // Record the error
43
+ context. diagnose (
44
+ Diagnostic (
45
+ node: Syntax ( self ) ,
46
+ message: ThrownErrorDiagnostic ( message: String ( describing: error) )
47
+ )
48
+ )
49
+
50
+ return ExprSyntax ( self )
51
+ }
45
52
}
46
53
}
47
54
0 commit comments