1 //! This module contains `Diagnostic` and the types/functions it uses for deserialization.
3 use serde
::{Deserialize, Serialize}
;
6 /// The error code associated to this diagnostic.
7 #[derive(Debug, Clone, Serialize, Deserialize)]
8 pub struct DiagnosticCode
{
11 /// An explanation for the code
12 pub explanation
: Option
<String
>,
15 __do_not_match_exhaustively
: (),
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
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,
29 __do_not_match_exhaustively
: (),
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
,
40 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
41 pub macro_decl_name
: String
,
43 /// span where macro was defined (if known)
44 pub def_site_span
: Option
<DiagnosticSpan
>,
47 __do_not_match_exhaustively
: (),
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.
57 /// The byte offset in the file where this diagnostic ends.
59 /// 1-based. The line in the file.
60 pub line_start
: usize,
61 /// 1-based. The line in the file.
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?
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.
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
>>,
89 __do_not_match_exhaustively
: (),
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
97 /// The suggested replacement has placeholders that will need to be manually
100 /// The suggested replacement may be incorrect in some circumstances. Needs
103 /// The suggested replacement will probably not work.
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")]
130 /// A diagnostic message generated by rustc
131 #[derive(Debug, Clone, Serialize, Deserialize)]
132 pub struct Diagnostic
{
133 /// The error message of this diagnostic.
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
>,
147 __do_not_match_exhaustively
: (),
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
)?
;
155 f
.write_str("cargo didn't render this message")?
;