Skip to content

[Macros] generate diagnostics from thrown error #1318

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
6 changes: 6 additions & 0 deletions Sources/SwiftDiagnostics/Diagnostic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ public struct Diagnostic: CustomDebugStringConvertible {
return "\(location): \(message)"
}
}

public protocol DiagnosticsProviding: Error {
/// The diagnostics provided by this error.
/// At least one diagnostic should have `severity == .error`.
var diagnostics: [Diagnostic] { get }
}
45 changes: 45 additions & 0 deletions Sources/SwiftSyntaxMacros/MacroExpansionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,51 @@ extension MacroExpansionContext {
}
}

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

var severity: DiagnosticSeverity { .error }

var diagnosticID: MessageID {
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
}
}

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 {
diagnose(
Diagnostic(
node: Syntax(node),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
return
}

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

// handle possibility that none of the diagnostics was an error
if !providedDiagnostics.contains(
where: { $0.diagMessage.severity == .error }
) {
diagnose(
Diagnostic(
node: Syntax(node),
message: ThrownErrorDiagnostic(
message: "macro expansion failed without generating an error"
)
)
)
}
}
}

/// Describe the position within a syntax node that can be used to compute
/// source locations.
public enum PositionInSyntaxNode {
Expand Down
40 changes: 5 additions & 35 deletions Sources/SwiftSyntaxMacros/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))
}
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(node),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
context.addDiagnostics(from: error, node: node)
}

continue
Expand Down Expand Up @@ -200,13 +194,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
}
)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(node),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
context.addDiagnostics(from: error, node: node)
}

continue
Expand Down Expand Up @@ -376,13 +364,7 @@ extension MacroApplication {
let newPeers = try peerMacro.expansion(of: attribute, providingPeersOf: decl, in: context)
peers.append(contentsOf: newPeers)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(attribute),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
context.addDiagnostics(from: error, node: attribute)
}
}

Expand All @@ -406,13 +388,7 @@ extension MacroApplication {
)
)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(attribute),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
context.addDiagnostics(from: error, node: attribute)
}
}

Expand Down Expand Up @@ -473,13 +449,7 @@ extension MacroApplication {
contentsOf: try _openExistential(typedDecl, do: expand)
)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(attribute),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
context.addDiagnostics(from: error, node: attribute)
}
}

Expand Down
20 changes: 1 addition & 19 deletions Sources/SwiftSyntaxMacros/Syntax+MacroEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
import SwiftDiagnostics
import SwiftSyntax

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

var severity: DiagnosticSeverity { .error }

var diagnosticID: MessageID {
.init(domain: "SwiftSyntaxMacros", id: "ThrownErrorDiagnostic")
}
}

extension SyntaxProtocol {
/// Detach the current node and inform the macro expansion context,
/// if it needs to know.
Expand Down Expand Up @@ -51,14 +40,7 @@ extension MacroExpansionExprSyntax {
do {
return try exprMacro.expansion(of: detach(in: context), in: context)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(self),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)

context.addDiagnostics(from: error, node: self)
return ExprSyntax(self)
}
}
Expand Down