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.
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.
17 use syntax_pos::{MultiSpan, Span};
21 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
22 pub struct Diagnostic {
24 pub message: Vec<(String, Style)>,
25 pub code: Option<DiagnosticId>,
27 pub children: Vec<SubDiagnostic>,
28 pub suggestions: Vec<CodeSuggestion>,
31 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
32 pub enum DiagnosticId {
37 /// For example a note attached to an error.
38 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
39 pub struct SubDiagnostic {
41 pub message: Vec<(String, Style)>,
43 pub render_span: Option<MultiSpan>,
46 #[derive(PartialEq, Eq)]
47 pub struct DiagnosticStyledString(pub Vec<StringPart>);
49 impl DiagnosticStyledString {
50 pub fn new() -> DiagnosticStyledString {
51 DiagnosticStyledString(vec![])
53 pub fn push_normal<S: Into<String>>(&mut self, t: S) {
54 self.0.push(StringPart::Normal(t.into()));
56 pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
57 self.0.push(StringPart::Highlighted(t.into()));
59 pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
60 DiagnosticStyledString(vec![StringPart::Normal(t.into())])
63 pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
64 DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
67 pub fn content(&self) -> String {
68 self.0.iter().map(|x| x.content()).collect::<String>()
72 #[derive(PartialEq, Eq)]
79 pub fn content(&self) -> String {
81 &StringPart::Normal(ref s) | & StringPart::Highlighted(ref s) => s.to_owned()
87 pub fn new(level: Level, message: &str) -> Self {
88 Diagnostic::new_with_code(level, None, message)
91 pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self {
94 message: vec![(message.to_owned(), Style::NoStyle)],
96 span: MultiSpan::new(),
102 pub fn is_error(&self) -> bool {
108 Level::FailureNote => {
115 Level::Cancelled => {
121 /// Cancel the diagnostic (a structured diagnostic must either be emitted or
122 /// canceled or it will panic when dropped).
123 pub fn cancel(&mut self) {
124 self.level = Level::Cancelled;
127 pub fn cancelled(&self) -> bool {
128 self.level == Level::Cancelled
131 /// Add a span/label to be included in the resulting snippet.
132 /// This is pushed onto the `MultiSpan` that was created when the
133 /// diagnostic was first built. If you don't call this function at
134 /// all, and you just supplied a `Span` to create the diagnostic,
135 /// then the snippet will just include that `Span`, which is
136 /// called the primary span.
137 pub fn span_label<T: Into<String>>(&mut self, span: Span, label: T) -> &mut Self {
138 self.span.push_span_label(span, label.into());
142 pub fn note_expected_found(&mut self,
143 label: &dyn fmt::Display,
144 expected: DiagnosticStyledString,
145 found: DiagnosticStyledString)
148 self.note_expected_found_extra(label, expected, found, &"", &"")
151 pub fn note_expected_found_extra(&mut self,
152 label: &dyn fmt::Display,
153 expected: DiagnosticStyledString,
154 found: DiagnosticStyledString,
155 expected_extra: &dyn fmt::Display,
156 found_extra: &dyn fmt::Display)
159 let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)];
160 msg.extend(expected.0.iter()
162 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
163 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
165 msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
166 msg.push((format!(" found {} `", label), Style::NoStyle));
167 msg.extend(found.0.iter()
169 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
170 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
172 msg.push((format!("`{}", found_extra), Style::NoStyle));
174 // For now, just attach these as notes
175 self.highlighted_note(msg);
179 pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
180 self.highlighted_note(vec![
181 (format!("`{}` from trait: `", name), Style::NoStyle),
182 (signature, Style::Highlight),
183 ("`".to_string(), Style::NoStyle)]);
187 pub fn note(&mut self, msg: &str) -> &mut Self {
188 self.sub(Level::Note, msg, MultiSpan::new(), None);
192 pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
193 self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
197 pub fn span_note<S: Into<MultiSpan>>(&mut self,
201 self.sub(Level::Note, msg, sp.into(), None);
205 pub fn warn(&mut self, msg: &str) -> &mut Self {
206 self.sub(Level::Warning, msg, MultiSpan::new(), None);
210 pub fn span_warn<S: Into<MultiSpan>>(&mut self,
214 self.sub(Level::Warning, msg, sp.into(), None);
218 pub fn help(&mut self , msg: &str) -> &mut Self {
219 self.sub(Level::Help, msg, MultiSpan::new(), None);
223 pub fn span_help<S: Into<MultiSpan>>(&mut self,
227 self.sub(Level::Help, msg, sp.into(), None);
231 /// Prints out a message with a suggested edit of the code. If the suggestion is presented
232 /// inline it will only show the text message and not the text.
234 /// See `CodeSuggestion` for more information.
235 pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
236 self.suggestions.push(CodeSuggestion {
237 substitutions: vec![Substitution {
238 parts: vec![SubstitutionPart {
244 show_code_when_inline: false,
245 applicability: Applicability::Unspecified,
250 /// Prints out a message with a suggested edit of the code.
252 /// In case of short messages and a simple suggestion,
253 /// rustc displays it as a label like
255 /// "try adding parentheses: `(tup.0).1`"
259 /// * should not end in any punctuation (a `:` is added automatically)
260 /// * should not be a question
261 /// * should not contain any parts like "the following", "as shown"
262 /// * may look like "to do xyz, use" or "to do xyz, use abc"
263 /// * may contain a name of a function, variable or type, but not whole expressions
265 /// See `CodeSuggestion` for more information.
266 pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
267 self.suggestions.push(CodeSuggestion {
268 substitutions: vec![Substitution {
269 parts: vec![SubstitutionPart {
275 show_code_when_inline: true,
276 applicability: Applicability::Unspecified,
281 pub fn multipart_suggestion(
284 suggestion: Vec<(Span, String)>,
286 self.suggestions.push(CodeSuggestion {
287 substitutions: vec![Substitution {
290 .map(|(span, snippet)| SubstitutionPart { snippet, span })
294 show_code_when_inline: true,
295 applicability: Applicability::Unspecified,
300 /// Prints out a message with multiple suggested edits of the code.
301 pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec<String>) -> &mut Self {
302 self.suggestions.push(CodeSuggestion {
303 substitutions: suggestions.into_iter().map(|snippet| Substitution {
304 parts: vec![SubstitutionPart {
310 show_code_when_inline: true,
311 applicability: Applicability::Unspecified,
316 /// This is a suggestion that may contain mistakes or fillers and should
317 /// be read and understood by a human.
318 pub fn span_suggestion_with_applicability(&mut self, sp: Span, msg: &str,
320 applicability: Applicability) -> &mut Self {
321 self.suggestions.push(CodeSuggestion {
322 substitutions: vec![Substitution {
323 parts: vec![SubstitutionPart {
329 show_code_when_inline: true,
335 pub fn span_suggestions_with_applicability(&mut self, sp: Span, msg: &str,
336 suggestions: Vec<String>,
337 applicability: Applicability) -> &mut Self {
338 self.suggestions.push(CodeSuggestion {
339 substitutions: suggestions.into_iter().map(|snippet| Substitution {
340 parts: vec![SubstitutionPart {
346 show_code_when_inline: true,
352 pub fn span_suggestion_short_with_applicability(
353 &mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
355 self.suggestions.push(CodeSuggestion {
356 substitutions: vec![Substitution {
357 parts: vec![SubstitutionPart {
363 show_code_when_inline: false,
364 applicability: applicability,
369 pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
370 self.span = sp.into();
374 pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
379 pub fn get_code(&self) -> Option<DiagnosticId> {
383 pub fn message(&self) -> String {
384 self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
387 pub fn styled_message(&self) -> &Vec<(String, Style)> {
391 /// Used by a lint. Copies over all details *but* the "main
393 pub fn copy_details_not_message(&mut self, from: &Diagnostic) {
394 self.span = from.span.clone();
395 self.code = from.code.clone();
396 self.children.extend(from.children.iter().cloned())
399 /// Convenience function for internal use, clients should use one of the
400 /// public methods above.
401 pub fn sub(&mut self,
405 render_span: Option<MultiSpan>) {
406 let sub = SubDiagnostic {
408 message: vec![(message.to_owned(), Style::NoStyle)],
412 self.children.push(sub);
415 /// Convenience function for internal use, clients should use one of the
416 /// public methods above.
417 fn sub_with_highlights(&mut self,
419 message: Vec<(String, Style)>,
421 render_span: Option<MultiSpan>) {
422 let sub = SubDiagnostic {
428 self.children.push(sub);
433 pub fn message(&self) -> String {
434 self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
437 pub fn styled_message(&self) -> &Vec<(String, Style)> {