@@ -20,25 +20,14 @@ import SwiftSyntaxMacroExpansion
2020import SwiftSyntaxMacrosTestSupport
2121import XCTest
2222
23- enum CustomError : Error , CustomStringConvertible {
24- case message( String )
25-
26- var description : String {
27- switch self {
28- case . message( let text) :
29- return text
30- }
31- }
32- }
33-
3423// MARK: Example macros
3524public struct StringifyMacro : ExpressionMacro {
3625 public static func expansion(
3726 of macro: some FreestandingMacroExpansionSyntax ,
3827 in context: some MacroExpansionContext
3928 ) throws -> ExprSyntax {
4029 guard let argument = macro. argumentList. first? . expression else {
41- throw CustomError . message ( " missing argument " )
30+ throw MacroExpansionErrorMessage ( " missing argument " )
4231 }
4332
4433 return " ( \( argument) , \( StringLiteralExprSyntax ( content: argument. description) ) ) "
@@ -110,7 +99,7 @@ public struct ColumnMacro: ExpressionMacro {
11099 ) throws -> ExprSyntax {
111100 guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro)
112101 else {
113- throw CustomError . message ( " can't find location for macro " )
102+ throw MacroExpansionErrorMessage ( " can't find location for macro " )
114103 }
115104 return sourceLoc. column. with ( \. leadingTrivia, macro. leadingTrivia)
116105 }
@@ -123,7 +112,7 @@ public struct FileIDMacro: ExpressionMacro {
123112 ) throws -> ExprSyntax {
124113 guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro)
125114 else {
126- throw CustomError . message ( " can't find location for macro " )
115+ throw MacroExpansionErrorMessage ( " can't find location for macro " )
127116 }
128117 return sourceLoc. file. with ( \. leadingTrivia, macro. leadingTrivia)
129118 }
@@ -147,16 +136,6 @@ struct CheckContextIndependenceMacro: ExpressionMacro {
147136 }
148137}
149138
150- struct SimpleDiagnosticMessage : DiagnosticMessage {
151- let message : String
152- let diagnosticID : MessageID
153- let severity : DiagnosticSeverity
154- }
155-
156- extension SimpleDiagnosticMessage : FixItMessage {
157- var fixItID : MessageID { diagnosticID }
158- }
159-
160139public struct ErrorMacro : DeclarationMacro {
161140 public static func expansion(
162141 of node: some FreestandingMacroExpansionSyntax ,
@@ -168,17 +147,13 @@ public struct ErrorMacro: DeclarationMacro {
168147 stringLiteral. segments. count == 1 ,
169148 case let . stringSegment( messageString) = stringLiteral. segments [ 0 ]
170149 else {
171- throw CustomError . message ( " #error macro requires a string literal " )
150+ throw MacroExpansionErrorMessage ( " #error macro requires a string literal " )
172151 }
173152
174153 context. diagnose (
175154 Diagnostic (
176155 node: Syntax ( node) ,
177- message: SimpleDiagnosticMessage (
178- message: messageString. content. description,
179- diagnosticID: MessageID ( domain: " test " , id: " error " ) ,
180- severity: . error
181- )
156+ message: MacroExpansionErrorMessage ( messageString. content. description)
182157 )
183158 )
184159
@@ -197,7 +172,7 @@ struct DefineBitwidthNumberedStructsMacro: DeclarationMacro {
197172 stringLiteral. segments. count == 1 ,
198173 case let . stringSegment( prefix) = stringLiteral. segments [ 0 ]
199174 else {
200- throw CustomError . message (
175+ throw MacroExpansionErrorMessage (
201176 " #bitwidthNumberedStructs macro requires a string literal "
202177 )
203178 }
@@ -304,12 +279,12 @@ public struct AddCompletionHandler: PeerMacro {
304279 // Only on functions at the moment. We could handle initializers as well
305280 // with a bit of work.
306281 guard let funcDecl = declaration. as ( FunctionDeclSyntax . self) else {
307- throw CustomError . message ( " @addCompletionHandler only works on functions " )
282+ throw MacroExpansionErrorMessage ( " @addCompletionHandler only works on functions " )
308283 }
309284
310285 // This only makes sense for async functions.
311286 if funcDecl. signature. effectSpecifiers? . asyncSpecifier == nil {
312- throw CustomError . message (
287+ throw MacroExpansionErrorMessage (
313288 " @addCompletionHandler requires an async function "
314289 )
315290 }
@@ -559,14 +534,14 @@ public struct UnwrapMacro: CodeItemMacro {
559534 in context: some MacroExpansionContext
560535 ) throws -> [ CodeBlockItemSyntax ] {
561536 guard !node. argumentList. isEmpty else {
562- throw CustomError . message ( " '#unwrap' requires arguments " )
537+ throw MacroExpansionErrorMessage ( " '#unwrap' requires arguments " )
563538 }
564539 let errorThrower = node. trailingClosure
565540 let identifiers = try node. argumentList. map { argument in
566541 guard let tupleElement = argument. as ( LabeledExprSyntax . self) ,
567542 let declReferenceExpr = tupleElement. expression. as ( DeclReferenceExprSyntax . self)
568543 else {
569- throw CustomError . message ( " Arguments must be identifiers " )
544+ throw MacroExpansionErrorMessage ( " Arguments must be identifiers " )
570545 }
571546 return declReferenceExpr. baseName
572547 }
0 commit comments