Skip to content

Replace DiagnosticsProviding protocol with concrete type #1327

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 2 commits into from
Feb 8, 2023
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
17 changes: 13 additions & 4 deletions Sources/SwiftDiagnostics/Diagnostic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ public struct Diagnostic: CustomDebugStringConvertible {
}
}

public protocol DiagnosticsProviding: Error {
/// The diagnostics provided by this error.
/// At least one diagnostic should have `severity == .error`.
var diagnostics: [Diagnostic] { get }
public struct DiagnosticsError: Error {
public var diagnostics: [Diagnostic]

/// The diagnostics must contain at least one with severity == `.error`.
/// Asserts if this condition is not satisfied.
public init(diagnostics: [Diagnostic]) {
self.diagnostics = diagnostics

assert(
diagnostics.contains(where: { $0.diagMessage.severity == .error }),
"at least one diagnostic must have severity == .error"
)
}
}
7 changes: 3 additions & 4 deletions Sources/SwiftSyntaxMacros/MacroExpansionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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 diagnosticsProvider = error as? DiagnosticsProviding else {
guard let diagnosticsError = error as? DiagnosticsError else {
diagnose(
Diagnostic(
node: Syntax(node),
Expand All @@ -89,13 +89,12 @@ extension MacroExpansionContext {
return
}

let providedDiagnostics = diagnosticsProvider.diagnostics
for diagnostic in providedDiagnostics {
for diagnostic in diagnosticsError.diagnostics {
diagnose(diagnostic)
}

// handle possibility that none of the diagnostics was an error
if !providedDiagnostics.contains(
if !diagnosticsError.diagnostics.contains(
where: { $0.diagMessage.severity == .error }
) {
diagnose(
Expand Down