]>
Commit | Line | Data |
---|---|---|
ba9703b0 XL |
1 | //! This module contains `Diagnostic` and the types/functions it uses for deserialization. |
2 | ||
3dfed10e | 3 | use serde::{Deserialize, Serialize}; |
ba9703b0 XL |
4 | use std::fmt; |
5 | ||
6 | /// The error code associated to this diagnostic. | |
7 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
8 | pub struct DiagnosticCode { | |
9 | /// The code itself. | |
10 | pub code: String, | |
11 | /// An explanation for the code | |
12 | pub explanation: Option<String>, | |
13 | #[doc(hidden)] | |
14 | #[serde(skip)] | |
15 | __do_not_match_exhaustively: (), | |
16 | } | |
17 | ||
18 | /// A line of code associated with the Diagnostic | |
19 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
20 | pub struct DiagnosticSpanLine { | |
21 | /// The line of code associated with the error | |
22 | pub text: String, | |
23 | /// Start of the section of the line to highlight. 1-based, character offset in self.text | |
24 | pub highlight_start: usize, | |
25 | /// End of the section of the line to highlight. 1-based, character offset in self.text | |
26 | pub highlight_end: usize, | |
27 | #[doc(hidden)] | |
28 | #[serde(skip)] | |
29 | __do_not_match_exhaustively: (), | |
30 | } | |
31 | ||
32 | /// Macro expansion information associated with a diagnostic. | |
33 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
34 | pub struct DiagnosticSpanMacroExpansion { | |
35 | /// span where macro was applied to generate this code; note that | |
36 | /// this may itself derive from a macro (if | |
37 | /// `span.expansion.is_some()`) | |
38 | pub span: DiagnosticSpan, | |
39 | ||
40 | /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]") | |
41 | pub macro_decl_name: String, | |
42 | ||
43 | /// span where macro was defined (if known) | |
44 | pub def_site_span: Option<DiagnosticSpan>, | |
45 | #[doc(hidden)] | |
46 | #[serde(skip)] | |
47 | __do_not_match_exhaustively: (), | |
48 | } | |
49 | ||
50 | /// A section of the source code associated with a Diagnostic | |
51 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
52 | pub struct DiagnosticSpan { | |
53 | /// The file name or the macro name this diagnostic comes from. | |
54 | pub file_name: String, | |
55 | /// The byte offset in the file where this diagnostic starts from. | |
56 | pub byte_start: u32, | |
57 | /// The byte offset in the file where this diagnostic ends. | |
58 | pub byte_end: u32, | |
59 | /// 1-based. The line in the file. | |
60 | pub line_start: usize, | |
61 | /// 1-based. The line in the file. | |
62 | pub line_end: usize, | |
63 | /// 1-based, character offset. | |
64 | pub column_start: usize, | |
65 | /// 1-based, character offset. | |
66 | pub column_end: usize, | |
67 | /// Is this a "primary" span -- meaning the point, or one of the points, | |
68 | /// where the error occurred? | |
69 | /// | |
70 | /// There are rare cases where multiple spans are marked as primary, | |
71 | /// e.g. "immutable borrow occurs here" and "mutable borrow ends here" can | |
72 | /// be two separate spans both "primary". Top (parent) messages should | |
73 | /// always have at least one primary span, unless it has 0 spans. Child | |
74 | /// messages may have 0 or more primary spans. | |
75 | pub is_primary: bool, | |
76 | /// Source text from the start of line_start to the end of line_end. | |
77 | pub text: Vec<DiagnosticSpanLine>, | |
78 | /// Label that should be placed at this location (if any) | |
79 | pub label: Option<String>, | |
80 | /// If we are suggesting a replacement, this will contain text | |
81 | /// that should be sliced in atop this span. | |
82 | pub suggested_replacement: Option<String>, | |
83 | /// If the suggestion is approximate | |
84 | pub suggestion_applicability: Option<Applicability>, | |
85 | /// Macro invocations that created the code at this span, if any. | |
86 | pub expansion: Option<Box<DiagnosticSpanMacroExpansion>>, | |
87 | #[doc(hidden)] | |
88 | #[serde(skip)] | |
89 | __do_not_match_exhaustively: (), | |
90 | } | |
91 | ||
92 | /// Whether a suggestion can be safely applied. | |
93 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
94 | pub enum Applicability { | |
95 | /// The suggested replacement can be applied automatically safely | |
96 | MachineApplicable, | |
97 | /// The suggested replacement has placeholders that will need to be manually | |
98 | /// replaced. | |
99 | HasPlaceholders, | |
100 | /// The suggested replacement may be incorrect in some circumstances. Needs | |
101 | /// human review. | |
102 | MaybeIncorrect, | |
103 | /// The suggested replacement will probably not work. | |
104 | Unspecified, | |
105 | #[doc(hidden)] | |
106 | #[serde(other)] | |
107 | Unknown, | |
108 | } | |
109 | ||
110 | /// The diagnostic level | |
111 | #[derive(Debug, Clone, Copy, Serialize, Deserialize)] | |
112 | #[serde(rename_all = "lowercase")] | |
113 | pub enum DiagnosticLevel { | |
114 | /// Internal compiler error | |
115 | #[serde(rename = "error: internal compiler error")] | |
116 | Ice, | |
117 | /// Error | |
118 | Error, | |
119 | /// Warning | |
120 | Warning, | |
121 | /// Note | |
122 | Note, | |
123 | /// Help | |
124 | Help, | |
125 | #[doc(hidden)] | |
126 | #[serde(other)] | |
127 | Unknown, | |
128 | } | |
129 | ||
130 | /// A diagnostic message generated by rustc | |
131 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
132 | pub struct Diagnostic { | |
133 | /// The error message of this diagnostic. | |
134 | pub message: String, | |
135 | /// The associated error code for this diagnostic | |
136 | pub code: Option<DiagnosticCode>, | |
137 | /// "error: internal compiler error", "error", "warning", "note", "help" | |
138 | pub level: DiagnosticLevel, | |
139 | /// A list of source code spans this diagnostic is associated with. | |
140 | pub spans: Vec<DiagnosticSpan>, | |
141 | /// Associated diagnostic messages. | |
142 | pub children: Vec<Diagnostic>, | |
143 | /// The message as rustc would render it | |
144 | pub rendered: Option<String>, | |
145 | #[doc(hidden)] | |
146 | #[serde(skip)] | |
147 | __do_not_match_exhaustively: (), | |
148 | } | |
149 | ||
150 | impl fmt::Display for Diagnostic { | |
151 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
152 | if let Some(ref rendered) = self.rendered { | |
153 | f.write_str(rendered)?; | |
154 | } else { | |
155 | f.write_str("cargo didn't render this message")?; | |
156 | } | |
157 | Ok(()) | |
158 | } | |
159 | } |