Skip to content

[Macros] Centralize "unwrapping" of errors into diagnostics #1471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions Sources/SwiftCompilerPluginMessageHandling/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ extension CompilerPluginMessageHandler {
throw MacroExpansionError.unmathedMacroRole
}
} catch {
let diagMessage: DiagnosticMessage
if let message = error as? DiagnosticMessage, message.severity == .error {
diagMessage = message
} else {
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
}
context.addDiagnostics(from: error, node: syntax)
expandedSource = ""
context.diagnose(Diagnostic(node: syntax, message: diagMessage))
}

let diagnostics = context.diagnostics.map {
Expand Down Expand Up @@ -221,14 +215,8 @@ extension CompilerPluginMessageHandler {
throw MacroExpansionError.unmathedMacroRole
}
} catch {
let diagMessage: DiagnosticMessage
if let message = error as? DiagnosticMessage, message.severity == .error {
diagMessage = message
} else {
diagMessage = ThrownErrorDiagnostic(message: String(describing: error))
}
context.addDiagnostics(from: error, node: attributeNode)
expandedSources = []
context.diagnose(Diagnostic(node: Syntax(attributeNode), message: diagMessage))
}

let diagnostics = context.diagnostics.map {
Expand All @@ -239,14 +227,3 @@ extension CompilerPluginMessageHandler {
)
}
}

/// Diagnostic message used for thrown errors.
fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
let message: String

var severity: DiagnosticSeverity { .error }

var diagnosticID: MessageID {
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
}
}
23 changes: 12 additions & 11 deletions Sources/SwiftSyntaxMacros/MacroExpansionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,23 @@ private struct ThrownErrorDiagnostic: DiagnosticMessage {
extension MacroExpansionContext {
/// Add diagnostics from the error thrown during macro expansion.
public func addDiagnostics<S: SyntaxProtocol>(from error: Error, node: S) {
guard let diagnosticsError = error as? DiagnosticsError else {
diagnose(
Diagnostic(
node: Syntax(node),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
return
// Inspect the error to form an appropriate set of diagnostics.
var diagnostics: [Diagnostic]
if let diagnosticsError = error as? DiagnosticsError {
diagnostics = diagnosticsError.diagnostics
} else if let message = error as? DiagnosticMessage {
diagnostics = [Diagnostic(node: Syntax(node), message: message)]
} else {
diagnostics = [Diagnostic(node: Syntax(node), message: ThrownErrorDiagnostic(message: String(describing: error)))]
}

for diagnostic in diagnosticsError.diagnostics {
// Emit the diagnostics.
for diagnostic in diagnostics {
diagnose(diagnostic)
}

// handle possibility that none of the diagnostics was an error
if !diagnosticsError.diagnostics.contains(
// Handle possibility that none of the diagnostics was an error.
if !diagnostics.contains(
where: { $0.diagMessage.severity == .error }
) {
diagnose(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntaxMacros/MacroReplacement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum MacroExpanderError: DiagnosticMessage {
return "macro expansion requires a definition"

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

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