Skip to content

Commit 548c9a9

Browse files
authored
Merge pull request #1471 from DougGregor/macro-context-add-diags-5.9
2 parents f2e4643 + df6ae3b commit 548c9a9

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
@@ -62,14 +62,8 @@ extension CompilerPluginMessageHandler {
6262
throw MacroExpansionError.unmathedMacroRole
6363
}
6464
} catch {
65-
let diagMessage: DiagnosticMessage
66-
if let message = error as? DiagnosticMessage, message.severity == .error {
67-
diagMessage = message
68-
} else {
69-
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
70-
}
65+
context.addDiagnostics(from: error, node: syntax)
7166
expandedSource = ""
72-
context.diagnose(Diagnostic(node: syntax, message: diagMessage))
7367
}
7468

7569
let diagnostics = context.diagnostics.map {
@@ -221,14 +215,8 @@ extension CompilerPluginMessageHandler {
221215
throw MacroExpansionError.unmathedMacroRole
222216
}
223217
} catch {
224-
let diagMessage: DiagnosticMessage
225-
if let message = error as? DiagnosticMessage, message.severity == .error {
226-
diagMessage = message
227-
} else {
228-
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
229-
}
218+
context.addDiagnostics(from: error, node: attributeNode)
230219
expandedSources = []
231-
context.diagnose(Diagnostic(node: Syntax(attributeNode), message: diagMessage))
232220
}
233221

234222
let diagnostics = context.diagnostics.map {
@@ -239,14 +227,3 @@ extension CompilerPluginMessageHandler {
239227
)
240228
}
241229
}
242-
243-
/// Diagnostic message used for thrown errors.
244-
fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
245-
let message: String
246-
247-
var severity: DiagnosticSeverity { .error }
248-
249-
var diagnosticID: MessageID {
250-
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
251-
}
252-
}

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)