Skip to content

Commit e5178e2

Browse files
authored
Merge pull request #1469 from DougGregor/macro-context-add-diags
2 parents 9ad7a48 + a77e414 commit e5178e2

File tree

3 files changed

+15
-37
lines changed

3 files changed

+15
-37
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,8 @@ extension CompilerPluginMessageHandler {
6969
throw MacroExpansionError.unmathedMacroRole
7070
}
7171
} catch {
72-
let diagMessage: DiagnosticMessage
73-
if let message = error as? DiagnosticMessage, message.severity == .error {
74-
diagMessage = message
75-
} else {
76-
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
77-
}
72+
context.addDiagnostics(from: error, node: syntax)
7873
expandedSource = ""
79-
context.diagnose(Diagnostic(node: syntax, message: diagMessage))
8074
}
8175

8276
let diagnostics = context.diagnostics.map {
@@ -228,14 +222,8 @@ extension CompilerPluginMessageHandler {
228222
throw MacroExpansionError.unmathedMacroRole
229223
}
230224
} catch {
231-
let diagMessage: DiagnosticMessage
232-
if let message = error as? DiagnosticMessage, message.severity == .error {
233-
diagMessage = message
234-
} else {
235-
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
236-
}
225+
context.addDiagnostics(from: error, node: attributeNode)
237226
expandedSources = []
238-
context.diagnose(Diagnostic(node: Syntax(attributeNode), message: diagMessage))
239227
}
240228

241229
let diagnostics = context.diagnostics.map {
@@ -246,14 +234,3 @@ extension CompilerPluginMessageHandler {
246234
)
247235
}
248236
}
249-
250-
/// Diagnostic message used for thrown errors.
251-
fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
252-
let message: String
253-
254-
var severity: DiagnosticSeverity { .error }
255-
256-
var diagnosticID: MessageID {
257-
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
258-
}
259-
}

Sources/SwiftSyntaxMacros/MacroExpansionContext.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,23 @@ private struct ThrownErrorDiagnostic: DiagnosticMessage {
189189
extension MacroExpansionContext {
190190
/// Add diagnostics from the error thrown during macro expansion.
191191
public func addDiagnostics<S: SyntaxProtocol>(from error: Error, node: S) {
192-
guard let diagnosticsError = error as? DiagnosticsError else {
193-
diagnose(
194-
Diagnostic(
195-
node: Syntax(node),
196-
message: ThrownErrorDiagnostic(message: String(describing: error))
197-
)
198-
)
199-
return
192+
// Inspect the error to form an appropriate set of diagnostics.
193+
var diagnostics: [Diagnostic]
194+
if let diagnosticsError = error as? DiagnosticsError {
195+
diagnostics = diagnosticsError.diagnostics
196+
} else if let message = error as? DiagnosticMessage {
197+
diagnostics = [Diagnostic(node: Syntax(node), message: message)]
198+
} else {
199+
diagnostics = [Diagnostic(node: Syntax(node), message: ThrownErrorDiagnostic(message: String(describing: error)))]
200200
}
201201

202-
for diagnostic in diagnosticsError.diagnostics {
202+
// Emit the diagnostics.
203+
for diagnostic in diagnostics {
203204
diagnose(diagnostic)
204205
}
205206

206-
// handle possibility that none of the diagnostics was an error
207-
if !diagnosticsError.diagnostics.contains(
207+
// Handle possibility that none of the diagnostics was an error.
208+
if !diagnostics.contains(
208209
where: { $0.diagMessage.severity == .error }
209210
) {
210211
diagnose(

Sources/SwiftSyntaxMacros/MacroReplacement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum MacroExpanderError: DiagnosticMessage {
1414
return "macro expansion requires a definition"
1515

1616
case .definitionNotMacroExpansion:
17-
return "macro definition must itself by a macro expansion expression (starting with '#')"
17+
return "macro must itself be defined by a macro expansion expression (starting with '#')"
1818

1919
case .nonParameterReference(let name):
2020
return "reference to value '\(name.text)' that is not a macro parameter in expansion"

0 commit comments

Comments
 (0)