1 //! This module contains `Diagnostic` and the types/functions it uses for deserialization.
5 /// The error code associated to this diagnostic.
6 #[derive(Debug, Clone, Serialize, Deserialize)]
7 pub struct DiagnosticCode
{
10 /// An explanation for the code
11 pub explanation
: Option
<String
>,
14 __do_not_match_exhaustively
: (),
17 /// A line of code associated with the Diagnostic
18 #[derive(Debug, Clone, Serialize, Deserialize)]
19 pub struct DiagnosticSpanLine
{
20 /// The line of code associated with the error
22 /// Start of the section of the line to highlight. 1-based, character offset in self.text
23 pub highlight_start
: usize,
24 /// End of the section of the line to highlight. 1-based, character offset in self.text
25 pub highlight_end
: usize,
28 __do_not_match_exhaustively
: (),
31 /// Macro expansion information associated with a diagnostic.
32 #[derive(Debug, Clone, Serialize, Deserialize)]
33 pub struct DiagnosticSpanMacroExpansion
{
34 /// span where macro was applied to generate this code; note that
35 /// this may itself derive from a macro (if
36 /// `span.expansion.is_some()`)
37 pub span
: DiagnosticSpan
,
39 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
40 pub macro_decl_name
: String
,
42 /// span where macro was defined (if known)
43 pub def_site_span
: Option
<DiagnosticSpan
>,
46 __do_not_match_exhaustively
: (),
49 /// A section of the source code associated with a Diagnostic
50 #[derive(Debug, Clone, Serialize, Deserialize)]
51 pub struct DiagnosticSpan
{
52 /// The file name or the macro name this diagnostic comes from.
53 pub file_name
: String
,
54 /// The byte offset in the file where this diagnostic starts from.
56 /// The byte offset in the file where this diagnostic ends.
58 /// 1-based. The line in the file.
59 pub line_start
: usize,
60 /// 1-based. The line in the file.
62 /// 1-based, character offset.
63 pub column_start
: usize,
64 /// 1-based, character offset.
65 pub column_end
: usize,
66 /// Is this a "primary" span -- meaning the point, or one of the points,
67 /// where the error occurred?
69 /// There are rare cases where multiple spans are marked as primary,
70 /// e.g. "immutable borrow occurs here" and "mutable borrow ends here" can
71 /// be two separate spans both "primary". Top (parent) messages should
72 /// always have at least one primary span, unless it has 0 spans. Child
73 /// messages may have 0 or more primary spans.
75 /// Source text from the start of line_start to the end of line_end.
76 pub text
: Vec
<DiagnosticSpanLine
>,
77 /// Label that should be placed at this location (if any)
78 pub label
: Option
<String
>,
79 /// If we are suggesting a replacement, this will contain text
80 /// that should be sliced in atop this span.
81 pub suggested_replacement
: Option
<String
>,
82 /// If the suggestion is approximate
83 pub suggestion_applicability
: Option
<Applicability
>,
84 /// Macro invocations that created the code at this span, if any.
85 pub expansion
: Option
<Box
<DiagnosticSpanMacroExpansion
>>,
88 __do_not_match_exhaustively
: (),
91 /// Whether a suggestion can be safely applied.
92 #[derive(Debug, Clone, Serialize, Deserialize)]
93 pub enum Applicability
{
94 /// The suggested replacement can be applied automatically safely
96 /// The suggested replacement has placeholders that will need to be manually
99 /// The suggested replacement may be incorrect in some circumstances. Needs
102 /// The suggested replacement will probably not work.
109 /// The diagnostic level
110 #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
111 #[serde(rename_all = "lowercase")]
112 pub enum DiagnosticLevel
{
113 /// Internal compiler error
114 #[serde(rename = "error: internal compiler error")]
129 /// A diagnostic message generated by rustc
130 #[derive(Debug, Clone, Serialize, Deserialize)]
131 pub struct Diagnostic
{
132 /// The error message of this diagnostic.
134 /// The associated error code for this diagnostic
135 pub code
: Option
<DiagnosticCode
>,
136 /// "error: internal compiler error", "error", "warning", "note", "help"
137 pub level
: DiagnosticLevel
,
138 /// A list of source code spans this diagnostic is associated with.
139 pub spans
: Vec
<DiagnosticSpan
>,
140 /// Associated diagnostic messages.
141 pub children
: Vec
<Diagnostic
>,
142 /// The message as rustc would render it
143 pub rendered
: Option
<String
>,
146 __do_not_match_exhaustively
: (),
149 impl fmt
::Display
for Diagnostic
{
150 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
151 if let Some(ref rendered
) = self.rendered
{
152 f
.write_str(rendered
)?
;
154 f
.write_str("cargo didn't render this message")?
;