@@ -16,21 +16,25 @@ import SwiftSyntax
16
16
/// Errors in macro handing.
17
17
enum MacroExpansionError : String {
18
18
case macroTypeNotFound = " macro expanding type not found "
19
+ case unmathedMacroRole = " macro doesn't conform to required macro role "
19
20
case freestandingMacroSyntaxIsNotMacro = " macro syntax couldn't be parsed "
21
+ case invalidExpansionMessage = " internal message error; please file a bug report "
20
22
}
21
23
22
24
extension MacroExpansionError : DiagnosticMessage {
23
25
var message : String {
24
26
self . rawValue
25
27
}
26
28
var diagnosticID : SwiftDiagnostics . MessageID {
27
- . init( domain: " \( type ( of: self ) ) " , id : " \( self ) " )
29
+ . init( domain: " SwiftCompilerPlugin " , id : " \( type ( of: self ) ) . \( self ) " )
28
30
}
29
31
var severity : SwiftDiagnostics . DiagnosticSeverity {
30
32
. error
31
33
}
32
34
}
33
35
36
+ extension MacroExpansionError : Error { }
37
+
34
38
extension PluginMessage . Diagnostic . Severity {
35
39
init ( from syntaxDiagSeverity: SwiftDiagnostics . DiagnosticSeverity ) {
36
40
switch syntaxDiagSeverity {
@@ -43,22 +47,21 @@ extension PluginMessage.Diagnostic.Severity {
43
47
44
48
extension PluginMessage . Diagnostic {
45
49
init ( from syntaxDiag: SwiftDiagnostics . Diagnostic , in sourceManager: SourceManager ) {
46
- guard
47
- let position = sourceManager . position (
48
- of : syntaxDiag . node ,
49
- at : . afterLeadingTrivia
50
- )
51
- else {
52
- fatalError ( " unknown diagnostic node " )
50
+ if let position = sourceManager . position (
51
+ of : syntaxDiag . node ,
52
+ at : . afterLeadingTrivia
53
+ ) {
54
+ self . position = . init ( fileName : position . fileName , offset : position . utf8Offset )
55
+ } else {
56
+ self . position = . invalid
53
57
}
54
58
55
- self . position = . init( fileName: position. fileName, offset: position. utf8Offset)
56
59
self . severity = . init( from: syntaxDiag. diagMessage. severity)
57
60
self . message = syntaxDiag. message
58
61
59
- self . highlights = syntaxDiag. highlights. map {
62
+ self . highlights = syntaxDiag. highlights. compactMap {
60
63
guard let range = sourceManager. range ( of: $0) else {
61
- fatalError ( " highlight node is not known " )
64
+ return nil
62
65
}
63
66
return . init(
64
67
fileName: range. fileName,
@@ -67,9 +70,9 @@ extension PluginMessage.Diagnostic {
67
70
)
68
71
}
69
72
70
- self . notes = syntaxDiag. notes. map {
73
+ self . notes = syntaxDiag. notes. compactMap {
71
74
guard let pos = sourceManager. position ( of: $0. node, at: . afterLeadingTrivia) else {
72
- fatalError ( " note node is not known " )
75
+ return nil
73
76
}
74
77
let position = PluginMessage . Diagnostic. Position (
75
78
fileName: pos. fileName,
@@ -78,7 +81,7 @@ extension PluginMessage.Diagnostic {
78
81
return . init( position: position, message: $0. message)
79
82
}
80
83
81
- self . fixIts = syntaxDiag. fixIts. map {
84
+ self . fixIts = syntaxDiag. fixIts. compactMap {
82
85
PluginMessage . Diagnostic. FixIt (
83
86
message: $0. message. message,
84
87
changes: $0. changes. changes. map {
@@ -108,7 +111,7 @@ extension PluginMessage.Diagnostic {
108
111
text = newTrivia. description
109
112
}
110
113
guard let range = range else {
111
- fatalError ( " unknown " )
114
+ return nil
112
115
}
113
116
return . init(
114
117
range: PositionRange (
0 commit comments