Skip to content

Commit 69230a2

Browse files
committed
Update for review
1 parent 306eb51 commit 69230a2

File tree

6 files changed

+230
-199
lines changed

6 files changed

+230
-199
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ extension CompilerPlugin {
152152
expandingSyntax: expandingSyntax
153153
)
154154

155-
case .expandAttachedMacro(let macro, let macroRole, let discriminator, let customAttributeSyntax, let declSyntax, let parentDeclSyntax):
155+
case .expandAttachedMacro(let macro, let macroRole, let discriminator, let attributeSyntax, let declSyntax, let parentDeclSyntax):
156156
try expandAttachedMacro(
157157
macro: macro,
158158
macroRole: macroRole,
159159
discriminator: discriminator,
160-
customAttributeSyntax: customAttributeSyntax,
160+
attributeSyntax: attributeSyntax,
161161
declSyntax: declSyntax,
162162
parentDeclSyntax: parentDeclSyntax
163163
)

Sources/SwiftCompilerPlugin/Diagnostics.swift

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ import SwiftSyntax
1616
/// Errors in macro handing.
1717
enum MacroExpansionError: String {
1818
case macroTypeNotFound = "macro expanding type not found"
19+
case unmathedMacroRole = "macro doesn't conform to required macro role"
1920
case freestandingMacroSyntaxIsNotMacro = "macro syntax couldn't be parsed"
21+
case invalidExpansionMessage = "internal message error; please file a bug report"
2022
}
2123

2224
extension MacroExpansionError: DiagnosticMessage {
2325
var message: String {
2426
self.rawValue
2527
}
2628
var diagnosticID: SwiftDiagnostics.MessageID {
27-
.init(domain: "\(type(of: self))", id: "\(self)")
29+
.init(domain: "SwiftCompilerPlugin", id: "\(type(of: self)).\(self)")
2830
}
2931
var severity: SwiftDiagnostics.DiagnosticSeverity {
3032
.error
3133
}
3234
}
3335

36+
extension MacroExpansionError: Error {}
37+
3438
extension PluginMessage.Diagnostic.Severity {
3539
init(from syntaxDiagSeverity: SwiftDiagnostics.DiagnosticSeverity) {
3640
switch syntaxDiagSeverity {
@@ -43,22 +47,21 @@ extension PluginMessage.Diagnostic.Severity {
4347

4448
extension PluginMessage.Diagnostic {
4549
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
5357
}
5458

55-
self.position = .init(fileName: position.fileName, offset: position.utf8Offset)
5659
self.severity = .init(from: syntaxDiag.diagMessage.severity)
5760
self.message = syntaxDiag.message
5861

59-
self.highlights = syntaxDiag.highlights.map {
62+
self.highlights = syntaxDiag.highlights.compactMap {
6063
guard let range = sourceManager.range(of: $0) else {
61-
fatalError("highlight node is not known")
64+
return nil
6265
}
6366
return .init(
6467
fileName: range.fileName,
@@ -67,9 +70,9 @@ extension PluginMessage.Diagnostic {
6770
)
6871
}
6972

70-
self.notes = syntaxDiag.notes.map {
73+
self.notes = syntaxDiag.notes.compactMap {
7174
guard let pos = sourceManager.position(of: $0.node, at: .afterLeadingTrivia) else {
72-
fatalError("note node is not known")
75+
return nil
7376
}
7477
let position = PluginMessage.Diagnostic.Position(
7578
fileName: pos.fileName,
@@ -78,7 +81,7 @@ extension PluginMessage.Diagnostic {
7881
return .init(position: position, message: $0.message)
7982
}
8083

81-
self.fixIts = syntaxDiag.fixIts.map {
84+
self.fixIts = syntaxDiag.fixIts.compactMap {
8285
PluginMessage.Diagnostic.FixIt(
8386
message: $0.message.message,
8487
changes: $0.changes.changes.map {
@@ -108,7 +111,7 @@ extension PluginMessage.Diagnostic {
108111
text = newTrivia.description
109112
}
110113
guard let range = range else {
111-
fatalError("unknown")
114+
return nil
112115
}
113116
return .init(
114117
range: PositionRange(

0 commit comments

Comments
 (0)