]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/diagnostic.rs
Fix invalid associated type rendering in rustdoc
[rust.git] / src / librustc_errors / diagnostic.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use CodeSuggestion;
12 use Level;
13 use RenderSpan;
14 use RenderSpan::Suggestion;
15 use std::fmt;
16 use syntax_pos::{MultiSpan, Span};
17 use snippet::Style;
18
19 #[must_use]
20 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
21 pub struct Diagnostic {
22     pub level: Level,
23     pub message: Vec<(String, Style)>,
24     pub code: Option<String>,
25     pub span: MultiSpan,
26     pub children: Vec<SubDiagnostic>,
27 }
28
29 /// For example a note attached to an error.
30 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
31 pub struct SubDiagnostic {
32     pub level: Level,
33     pub message: Vec<(String, Style)>,
34     pub span: MultiSpan,
35     pub render_span: Option<RenderSpan>,
36 }
37
38 impl Diagnostic {
39     pub fn new(level: Level, message: &str) -> Self {
40         Diagnostic::new_with_code(level, None, message)
41     }
42
43     pub fn new_with_code(level: Level, code: Option<String>, message: &str) -> Self {
44         Diagnostic {
45             level: level,
46             message: vec![(message.to_owned(), Style::NoStyle)],
47             code: code,
48             span: MultiSpan::new(),
49             children: vec![],
50         }
51     }
52
53     /// Cancel the diagnostic (a structured diagnostic must either be emitted or
54     /// cancelled or it will panic when dropped).
55     /// BEWARE: if this DiagnosticBuilder is an error, then creating it will
56     /// bump the error count on the Handler and cancelling it won't undo that.
57     /// If you want to decrement the error count you should use `Handler::cancel`.
58     pub fn cancel(&mut self) {
59         self.level = Level::Cancelled;
60     }
61
62     pub fn cancelled(&self) -> bool {
63         self.level == Level::Cancelled
64     }
65
66     pub fn is_fatal(&self) -> bool {
67         self.level == Level::Fatal
68     }
69
70     /// Add a span/label to be included in the resulting snippet.
71     /// This is pushed onto the `MultiSpan` that was created when the
72     /// diagnostic was first built. If you don't call this function at
73     /// all, and you just supplied a `Span` to create the diagnostic,
74     /// then the snippet will just include that `Span`, which is
75     /// called the primary span.
76     pub fn span_label(&mut self, span: Span, label: &fmt::Display)
77                       -> &mut Self {
78         self.span.push_span_label(span, format!("{}", label));
79         self
80     }
81
82     pub fn note_expected_found(&mut self,
83                                label: &fmt::Display,
84                                expected: &fmt::Display,
85                                found: &fmt::Display)
86                                -> &mut Self
87     {
88         self.note_expected_found_extra(label, expected, found, &"", &"")
89     }
90
91     pub fn note_expected_found_extra(&mut self,
92                                      label: &fmt::Display,
93                                      expected: &fmt::Display,
94                                      found: &fmt::Display,
95                                      expected_extra: &fmt::Display,
96                                      found_extra: &fmt::Display)
97                                      -> &mut Self
98     {
99         // For now, just attach these as notes
100         self.highlighted_note(vec![
101             (format!("expected {} `", label), Style::NoStyle),
102             (format!("{}", expected), Style::Highlight),
103             (format!("`{}\n", expected_extra), Style::NoStyle),
104             (format!("   found {} `", label), Style::NoStyle),
105             (format!("{}", found), Style::Highlight),
106             (format!("`{}", found_extra), Style::NoStyle),
107         ]);
108         self
109     }
110
111     pub fn note(&mut self, msg: &str) -> &mut Self {
112         self.sub(Level::Note, msg, MultiSpan::new(), None);
113         self
114     }
115
116     pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
117         self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
118         self
119     }
120
121     pub fn span_note<S: Into<MultiSpan>>(&mut self,
122                                          sp: S,
123                                          msg: &str)
124                                          -> &mut Self {
125         self.sub(Level::Note, msg, sp.into(), None);
126         self
127     }
128
129     pub fn warn(&mut self, msg: &str) -> &mut Self {
130         self.sub(Level::Warning, msg, MultiSpan::new(), None);
131         self
132     }
133
134     pub fn span_warn<S: Into<MultiSpan>>(&mut self,
135                                          sp: S,
136                                          msg: &str)
137                                          -> &mut Self {
138         self.sub(Level::Warning, msg, sp.into(), None);
139         self
140     }
141
142     pub fn help(&mut self , msg: &str) -> &mut Self {
143         self.sub(Level::Help, msg, MultiSpan::new(), None);
144         self
145     }
146
147     pub fn span_help<S: Into<MultiSpan>>(&mut self,
148                                          sp: S,
149                                          msg: &str)
150                                          -> &mut Self {
151         self.sub(Level::Help, msg, sp.into(), None);
152         self
153     }
154
155     /// Prints out a message with a suggested edit of the code.
156     ///
157     /// See `diagnostic::RenderSpan::Suggestion` for more information.
158     pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
159                                                sp: S,
160                                                msg: &str,
161                                                suggestion: String)
162                                                -> &mut Self {
163         self.sub(Level::Help,
164                  msg,
165                  MultiSpan::new(),
166                  Some(Suggestion(CodeSuggestion {
167                      msp: sp.into(),
168                      substitutes: vec![suggestion],
169                  })));
170         self
171     }
172
173     pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
174         self.span = sp.into();
175         self
176     }
177
178     pub fn code(&mut self, s: String) -> &mut Self {
179         self.code = Some(s);
180         self
181     }
182
183     pub fn message(&self) -> String {
184         self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
185     }
186
187     pub fn styled_message(&self) -> &Vec<(String, Style)> {
188         &self.message
189     }
190
191     pub fn level(&self) -> Level {
192         self.level
193     }
194
195     /// Used by a lint. Copies over all details *but* the "main
196     /// message".
197     pub fn copy_details_not_message(&mut self, from: &Diagnostic) {
198         self.span = from.span.clone();
199         self.code = from.code.clone();
200         self.children.extend(from.children.iter().cloned())
201     }
202
203     /// Convenience function for internal use, clients should use one of the
204     /// public methods above.
205     fn sub(&mut self,
206            level: Level,
207            message: &str,
208            span: MultiSpan,
209            render_span: Option<RenderSpan>) {
210         let sub = SubDiagnostic {
211             level: level,
212             message: vec![(message.to_owned(), Style::NoStyle)],
213             span: span,
214             render_span: render_span,
215         };
216         self.children.push(sub);
217     }
218
219     /// Convenience function for internal use, clients should use one of the
220     /// public methods above.
221     fn sub_with_highlights(&mut self,
222                            level: Level,
223                            message: Vec<(String, Style)>,
224                            span: MultiSpan,
225                            render_span: Option<RenderSpan>) {
226         let sub = SubDiagnostic {
227             level: level,
228             message: message,
229             span: span,
230             render_span: render_span,
231         };
232         self.children.push(sub);
233     }
234 }
235
236 impl SubDiagnostic {
237     pub fn message(&self) -> String {
238         self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
239     }
240
241     pub fn styled_message(&self) -> &Vec<(String, Style)> {
242         &self.message
243     }
244 }