Skip to content

Commit 1b5fe6b

Browse files
authored
Merge pull request #1327 from elizablock/DiagnosticsError
Replace DiagnosticsProviding protocol with concrete type
2 parents f6ba4aa + e21e0b4 commit 1b5fe6b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ public struct Diagnostic: CustomDebugStringConvertible {
7272
}
7373
}
7474

75-
public protocol DiagnosticsProviding: Error {
76-
/// The diagnostics provided by this error.
77-
/// At least one diagnostic should have `severity == .error`.
78-
var diagnostics: [Diagnostic] { get }
75+
public struct DiagnosticsError: Error {
76+
public var diagnostics: [Diagnostic]
77+
78+
/// The diagnostics must contain at least one with severity == `.error`.
79+
/// Asserts if this condition is not satisfied.
80+
public init(diagnostics: [Diagnostic]) {
81+
self.diagnostics = diagnostics
82+
83+
assert(
84+
diagnostics.contains(where: { $0.diagMessage.severity == .error }),
85+
"at least one diagnostic must have severity == .error"
86+
)
87+
}
7988
}

Sources/SwiftSyntaxMacros/MacroExpansionContext.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private struct ThrownErrorDiagnostic: DiagnosticMessage {
7979
extension MacroExpansionContext {
8080
/// Add diagnostics from the error thrown during macro expansion.
8181
public func addDiagnostics<S: SyntaxProtocol>(from error: Error, node: S) {
82-
guard let diagnosticsProvider = error as? DiagnosticsProviding else {
82+
guard let diagnosticsError = error as? DiagnosticsError else {
8383
diagnose(
8484
Diagnostic(
8585
node: Syntax(node),
@@ -89,13 +89,12 @@ extension MacroExpansionContext {
8989
return
9090
}
9191

92-
let providedDiagnostics = diagnosticsProvider.diagnostics
93-
for diagnostic in providedDiagnostics {
92+
for diagnostic in diagnosticsError.diagnostics {
9493
diagnose(diagnostic)
9594
}
9695

9796
// handle possibility that none of the diagnostics was an error
98-
if !providedDiagnostics.contains(
97+
if !diagnosticsError.diagnostics.contains(
9998
where: { $0.diagMessage.severity == .error }
10099
) {
101100
diagnose(

0 commit comments

Comments
 (0)