Skip to main content

bynk_syntax/
error.rs

1//! Compiler diagnostics.
2//!
3//! Every error has a category (a dotted namespace string like
4//! `bynk.parse.expected_token`), a primary span, a primary message, and
5//! optionally some secondary labels and notes. Rendering goes through
6//! [`ariadne`] for source-pointing colour output.
7
8use ariadne::{Color, Config, Label, Report, ReportKind};
9
10use crate::span::Span;
11
12/// A compile error.
13#[derive(Debug, Clone)]
14pub struct CompileError {
15    pub category: &'static str,
16    pub span: Span,
17    pub message: String,
18    pub labels: Vec<(Span, String)>,
19    pub notes: Vec<String>,
20    /// v0.26 (ADR 0054): machine-applicable fixes, authored at the diagnosis
21    /// site — the only place the exact spans and replacement are known.
22    /// Consumed by the LSP (`codeAction`) and, later, a CLI `--fix`.
23    pub suggestions: Vec<Suggestion>,
24}
25
26/// A structured fix for the error it is attached to (v0.26, ADR 0054).
27///
28/// `edits` are span → replacement: an empty replacement deletes the span; an
29/// empty span inserts at its position. Spans are offsets into the same source
30/// text as the error's own span.
31#[derive(Debug, Clone)]
32pub struct Suggestion {
33    /// Human-facing action title, e.g. "remove `Clock` from the `given` clause".
34    pub message: String,
35    pub edits: Vec<(Span, String)>,
36    pub applicability: Applicability,
37}
38
39/// Whether a [`Suggestion`] can be applied without review (mirrors rustc;
40/// gates a future CLI `--fix` and the LSP's one-click apply).
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum Applicability {
43    /// The fix is exactly right — safe to apply mechanically.
44    MachineApplicable,
45    /// The fix contains placeholder text a human must complete; never
46    /// auto-applied.
47    HasPlaceholders,
48}
49
50/// Severity classification for a [`CompileError`]. Mirrors LSP severity levels
51/// so the LSP server can map diagnostics to the protocol without reinterpreting
52/// error categories. Lives in the syntax leaf beside `CompileError` (it
53/// classifies one): shared by the IDE diagnose path (`bynk-ide`) and the
54/// `short`/`json` renderers, without either depending on the other.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum Severity {
57    Error,
58    Warning,
59}
60
61impl Severity {
62    /// Classify a [`CompileError`] by its category prefix.
63    ///
64    /// `bynk.parse.orphan_doc_block`, `bynk.given.unused_capability`,
65    /// `bynk.list.deprecated_function`, and the `bynk.index.*` hygiene hints
66    /// (`missing`/`unused`, ADR 0118 D4) are warnings; everything else is an
67    /// error. Future categories can be added as the diagnostic surface grows.
68    pub fn for_error(err: &CompileError) -> Severity {
69        match err.category {
70            "bynk.parse.orphan_doc_block"
71            | "bynk.given.unused_capability"
72            | "bynk.list.deprecated_function"
73            | "bynk.index.missing"
74            | "bynk.index.unused" => Severity::Warning,
75            _ => Severity::Error,
76        }
77    }
78}
79
80/// Split diagnostics into `(errors, warnings)` by severity (ADR 0117). The build
81/// fails iff the `errors` half is non-empty; the `warnings` half surfaces but
82/// does not gate compilation. Relative order within each half is preserved.
83pub fn partition_by_severity(
84    diagnostics: Vec<CompileError>,
85) -> (Vec<CompileError>, Vec<CompileError>) {
86    diagnostics
87        .into_iter()
88        .partition(|d| Severity::for_error(d) == Severity::Error)
89}
90
91impl CompileError {
92    pub fn new(category: &'static str, span: Span, message: impl Into<String>) -> Self {
93        Self {
94            category,
95            span,
96            message: message.into(),
97            labels: Vec::new(),
98            notes: Vec::new(),
99            suggestions: Vec::new(),
100        }
101    }
102
103    pub fn with_label(mut self, span: Span, label: impl Into<String>) -> Self {
104        self.labels.push((span, label.into()));
105        self
106    }
107
108    pub fn with_note(mut self, note: impl Into<String>) -> Self {
109        self.notes.push(note.into());
110        self
111    }
112
113    /// Attach a machine-applicable fix (v0.26). Mirrors [`Self::with_note`];
114    /// the suggestion is authored where the diagnostic is raised.
115    pub fn with_suggestion(
116        mut self,
117        message: impl Into<String>,
118        edits: Vec<(Span, String)>,
119        applicability: Applicability,
120    ) -> Self {
121        self.suggestions.push(Suggestion {
122            message: message.into(),
123            edits,
124            applicability,
125        });
126        self
127    }
128
129    /// Build an [`ariadne::Report`] for this error, anchored to the given
130    /// filename. Colour is on (for the CLI and human-facing test output).
131    pub fn report<'a>(
132        &'a self,
133        filename: &'a str,
134    ) -> Report<'a, (&'a str, std::ops::Range<usize>)> {
135        self.report_with_config(filename, Config::default())
136    }
137
138    /// Build a colourless [`ariadne::Report`], for transcripts committed to the
139    /// repo — no ANSI escape codes, so the output is byte-stable across machines.
140    pub fn report_plain<'a>(
141        &'a self,
142        filename: &'a str,
143    ) -> Report<'a, (&'a str, std::ops::Range<usize>)> {
144        self.report_with_config(filename, Config::default().with_color(false))
145    }
146
147    fn report_with_config<'a>(
148        &'a self,
149        filename: &'a str,
150        config: Config,
151    ) -> Report<'a, (&'a str, std::ops::Range<usize>)> {
152        let primary_span = (filename, self.span.range());
153        let mut builder = Report::build(ReportKind::Error, primary_span.clone())
154            .with_config(config)
155            .with_code(self.category)
156            .with_message(&self.message)
157            .with_label(
158                Label::new(primary_span)
159                    .with_message(&self.message)
160                    .with_color(Color::Red),
161            );
162
163        for (span, label) in &self.labels {
164            builder = builder.with_label(
165                Label::new((filename, span.range()))
166                    .with_message(label)
167                    .with_color(Color::Yellow),
168            );
169        }
170
171        for note in &self.notes {
172            builder = builder.with_note(note);
173        }
174
175        builder.finish()
176    }
177}
178
179#[cfg(test)]
180mod warning_channel_tests {
181    use super::*;
182    use crate::span::Span;
183
184    #[test]
185    fn partition_splits_by_severity() {
186        let warn = CompileError::new("bynk.given.unused_capability", Span::default(), "unused");
187        let err = CompileError::new("bynk.types.argument_mismatch", Span::default(), "bad");
188        let (errors, warnings) = partition_by_severity(vec![warn, err]);
189        assert_eq!(errors.len(), 1);
190        assert_eq!(errors[0].category, "bynk.types.argument_mismatch");
191        assert_eq!(warnings.len(), 1);
192        assert_eq!(warnings[0].category, "bynk.given.unused_capability");
193    }
194}