]> git.proxmox.com Git - rustc.git/blob - vendor/cargo_metadata-0.9.1/src/diagnostic.rs
New upstream version 1.46.0+dfsg1
[rustc.git] / vendor / cargo_metadata-0.9.1 / src / diagnostic.rs
1 //! This module contains `Diagnostic` and the types/functions it uses for deserialization.
2
3 use std::fmt;
4
5 /// The error code associated to this diagnostic.
6 #[derive(Debug, Clone, Serialize, Deserialize)]
7 pub struct DiagnosticCode {
8 /// The code itself.
9 pub code: String,
10 /// An explanation for the code
11 pub explanation: Option<String>,
12 #[doc(hidden)]
13 #[serde(skip)]
14 __do_not_match_exhaustively: (),
15 }
16
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
21 pub text: String,
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,
26 #[doc(hidden)]
27 #[serde(skip)]
28 __do_not_match_exhaustively: (),
29 }
30
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,
38
39 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
40 pub macro_decl_name: String,
41
42 /// span where macro was defined (if known)
43 pub def_site_span: Option<DiagnosticSpan>,
44 #[doc(hidden)]
45 #[serde(skip)]
46 __do_not_match_exhaustively: (),
47 }
48
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.
55 pub byte_start: u32,
56 /// The byte offset in the file where this diagnostic ends.
57 pub byte_end: u32,
58 /// 1-based. The line in the file.
59 pub line_start: usize,
60 /// 1-based. The line in the file.
61 pub line_end: usize,
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?
68 ///
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.
74 pub is_primary: bool,
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>>,
86 #[doc(hidden)]
87 #[serde(skip)]
88 __do_not_match_exhaustively: (),
89 }
90
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
95 MachineApplicable,
96 /// The suggested replacement has placeholders that will need to be manually
97 /// replaced.
98 HasPlaceholders,
99 /// The suggested replacement may be incorrect in some circumstances. Needs
100 /// human review.
101 MaybeIncorrect,
102 /// The suggested replacement will probably not work.
103 Unspecified,
104 #[doc(hidden)]
105 #[serde(other)]
106 Unknown,
107 }
108
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")]
115 Ice,
116 /// Error
117 Error,
118 /// Warning
119 Warning,
120 /// Note
121 Note,
122 /// Help
123 Help,
124 #[doc(hidden)]
125 #[serde(other)]
126 Unknown,
127 }
128
129 /// A diagnostic message generated by rustc
130 #[derive(Debug, Clone, Serialize, Deserialize)]
131 pub struct Diagnostic {
132 /// The error message of this diagnostic.
133 pub message: String,
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>,
144 #[doc(hidden)]
145 #[serde(skip)]
146 __do_not_match_exhaustively: (),
147 }
148
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)?;
153 } else {
154 f.write_str("cargo didn't render this message")?;
155 }
156 Ok(())
157 }
158 }