rustfix/
diagnostics.rs

1//! Rustc Diagnostic JSON Output.
2//!
3//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/4fd68eb47bad1c121417ac4450b2f0456150db86/compiler/rustc_errors/src/json.rs).
4//!
5//! For examples of the JSON output, see JSON fixture files under `tests/` directory.
6
7use serde::Deserialize;
8
9/// The root diagnostic JSON output emitted by the compiler.
10#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
11pub struct Diagnostic {
12    /// The primary error message.
13    pub message: String,
14    pub code: Option<DiagnosticCode>,
15    /// "error: internal compiler error", "error", "warning", "note", "help".
16    level: String,
17    pub spans: Vec<DiagnosticSpan>,
18    /// Associated diagnostic messages.
19    pub children: Vec<Diagnostic>,
20    /// The message as rustc would render it.
21    pub rendered: Option<String>,
22}
23
24/// Span information of a diagnostic item.
25#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
26pub struct DiagnosticSpan {
27    pub file_name: String,
28    pub byte_start: u32,
29    pub byte_end: u32,
30    /// 1-based.
31    pub line_start: usize,
32    pub line_end: usize,
33    /// 1-based, character offset.
34    pub column_start: usize,
35    pub column_end: usize,
36    /// Is this a "primary" span -- meaning the point, or one of the points,
37    /// where the error occurred?
38    pub is_primary: bool,
39    /// Source text from the start of `line_start` to the end of `line_end`.
40    pub text: Vec<DiagnosticSpanLine>,
41    /// Label that should be placed at this location (if any)
42    label: Option<String>,
43    /// If we are suggesting a replacement, this will contain text
44    /// that should be sliced in atop this span.
45    pub suggested_replacement: Option<String>,
46    /// If the suggestion is approximate
47    pub suggestion_applicability: Option<Applicability>,
48    /// Macro invocations that created the code at this span, if any.
49    expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
50}
51
52/// Indicates the confidence in the correctness of a suggestion.
53///
54/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
55/// to determine whether it should be automatically applied or if the user should be consulted
56/// before applying the suggestion.
57#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
58pub enum Applicability {
59    /// The suggestion is definitely what the user intended, or maintains the exact meaning of the code.
60    /// This suggestion should be automatically applied.
61    ///
62    /// In case of multiple `MachineApplicable` suggestions (whether as part of
63    /// the same `multipart_suggestion` or not), all of them should be
64    /// automatically applied.
65    MachineApplicable,
66
67    /// The suggestion may be what the user intended, but it is uncertain. The suggestion should
68    /// result in valid Rust code if it is applied.
69    MaybeIncorrect,
70
71    /// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
72    /// cannot be applied automatically because it will not result in valid Rust code. The user
73    /// will need to fill in the placeholders.
74    HasPlaceholders,
75
76    /// The applicability of the suggestion is unknown.
77    Unspecified,
78}
79
80/// Span information of a single line.
81#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
82pub struct DiagnosticSpanLine {
83    pub text: String,
84
85    /// 1-based, character offset in self.text.
86    pub highlight_start: usize,
87
88    pub highlight_end: usize,
89}
90
91/// Span information for macro expansions.
92#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
93struct DiagnosticSpanMacroExpansion {
94    /// span where macro was applied to generate this code; note that
95    /// this may itself derive from a macro (if
96    /// `span.expansion.is_some()`)
97    span: DiagnosticSpan,
98
99    /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
100    macro_decl_name: String,
101
102    /// span where macro was defined (if known)
103    def_site_span: Option<DiagnosticSpan>,
104}
105
106/// The error code emitted by the compiler. See [Rust error codes index].
107///
108/// [Rust error codes index]: https://doc.rust-lang.org/error_codes/error-index.html
109#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
110pub struct DiagnosticCode {
111    /// The code itself.
112    pub code: String,
113    /// An explanation for the code.
114    explanation: Option<String>,
115}