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.
15 use syntax_pos::{MultiSpan, Span};
19 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
20 pub struct Diagnostic {
22 pub message: Vec<(String, Style)>,
23 pub code: Option<String>,
25 pub children: Vec<SubDiagnostic>,
26 pub suggestion: Option<CodeSuggestion>,
29 /// For example a note attached to an error.
30 #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
31 pub struct SubDiagnostic {
33 pub message: Vec<(String, Style)>,
35 pub render_span: Option<RenderSpan>,
38 #[derive(PartialEq, Eq)]
39 pub struct DiagnosticStyledString(pub Vec<StringPart>);
41 impl DiagnosticStyledString {
42 pub fn new() -> DiagnosticStyledString {
43 DiagnosticStyledString(vec![])
45 pub fn push_normal<S: Into<String>>(&mut self, t: S) {
46 self.0.push(StringPart::Normal(t.into()));
48 pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
49 self.0.push(StringPart::Highlighted(t.into()));
51 pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
52 DiagnosticStyledString(vec![StringPart::Normal(t.into())])
55 pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
56 DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
59 pub fn content(&self) -> String {
60 self.0.iter().map(|x| x.content()).collect::<String>()
64 #[derive(PartialEq, Eq)]
71 pub fn content(&self) -> String {
73 &StringPart::Normal(ref s) | & StringPart::Highlighted(ref s) => s.to_owned()
79 pub fn new(level: Level, message: &str) -> Self {
80 Diagnostic::new_with_code(level, None, message)
83 pub fn new_with_code(level: Level, code: Option<String>, message: &str) -> Self {
86 message: vec![(message.to_owned(), Style::NoStyle)],
88 span: MultiSpan::new(),
94 /// Cancel the diagnostic (a structured diagnostic must either be emitted or
95 /// cancelled or it will panic when dropped).
96 /// BEWARE: if this DiagnosticBuilder is an error, then creating it will
97 /// bump the error count on the Handler and cancelling it won't undo that.
98 /// If you want to decrement the error count you should use `Handler::cancel`.
99 pub fn cancel(&mut self) {
100 self.level = Level::Cancelled;
103 pub fn cancelled(&self) -> bool {
104 self.level == Level::Cancelled
107 pub fn is_fatal(&self) -> bool {
108 self.level == Level::Fatal
111 /// Add a span/label to be included in the resulting snippet.
112 /// This is pushed onto the `MultiSpan` that was created when the
113 /// diagnostic was first built. If you don't call this function at
114 /// all, and you just supplied a `Span` to create the diagnostic,
115 /// then the snippet will just include that `Span`, which is
116 /// called the primary span.
117 pub fn span_label(&mut self, span: Span, label: &fmt::Display)
119 self.span.push_span_label(span, format!("{}", label));
123 pub fn note_expected_found(&mut self,
124 label: &fmt::Display,
125 expected: DiagnosticStyledString,
126 found: DiagnosticStyledString)
129 self.note_expected_found_extra(label, expected, found, &"", &"")
132 pub fn note_expected_found_extra(&mut self,
133 label: &fmt::Display,
134 expected: DiagnosticStyledString,
135 found: DiagnosticStyledString,
136 expected_extra: &fmt::Display,
137 found_extra: &fmt::Display)
140 let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)];
141 msg.extend(expected.0.iter()
143 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
144 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
146 msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
147 msg.push((format!(" found {} `", label), Style::NoStyle));
148 msg.extend(found.0.iter()
150 StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
151 StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
153 msg.push((format!("`{}", found_extra), Style::NoStyle));
155 // For now, just attach these as notes
156 self.highlighted_note(msg);
160 pub fn note(&mut self, msg: &str) -> &mut Self {
161 self.sub(Level::Note, msg, MultiSpan::new(), None);
165 pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
166 self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
170 pub fn span_note<S: Into<MultiSpan>>(&mut self,
174 self.sub(Level::Note, msg, sp.into(), None);
178 pub fn warn(&mut self, msg: &str) -> &mut Self {
179 self.sub(Level::Warning, msg, MultiSpan::new(), None);
183 pub fn span_warn<S: Into<MultiSpan>>(&mut self,
187 self.sub(Level::Warning, msg, sp.into(), None);
191 pub fn help(&mut self , msg: &str) -> &mut Self {
192 self.sub(Level::Help, msg, MultiSpan::new(), None);
196 pub fn span_help<S: Into<MultiSpan>>(&mut self,
200 self.sub(Level::Help, msg, sp.into(), None);
204 /// Prints out a message with a suggested edit of the code.
206 /// See `diagnostic::CodeSuggestion` for more information.
207 pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
208 assert!(self.suggestion.is_none());
209 self.suggestion = Some(CodeSuggestion {
211 substitutes: vec![suggestion],
217 pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
218 self.span = sp.into();
222 pub fn code(&mut self, s: String) -> &mut Self {
227 pub fn message(&self) -> String {
228 self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
231 pub fn styled_message(&self) -> &Vec<(String, Style)> {
235 pub fn level(&self) -> Level {
239 /// Used by a lint. Copies over all details *but* the "main
241 pub fn copy_details_not_message(&mut self, from: &Diagnostic) {
242 self.span = from.span.clone();
243 self.code = from.code.clone();
244 self.children.extend(from.children.iter().cloned())
247 /// Convenience function for internal use, clients should use one of the
248 /// public methods above.
253 render_span: Option<RenderSpan>) {
254 let sub = SubDiagnostic {
256 message: vec![(message.to_owned(), Style::NoStyle)],
258 render_span: render_span,
260 self.children.push(sub);
263 /// Convenience function for internal use, clients should use one of the
264 /// public methods above.
265 fn sub_with_highlights(&mut self,
267 message: Vec<(String, Style)>,
269 render_span: Option<RenderSpan>) {
270 let sub = SubDiagnostic {
274 render_span: render_span,
276 self.children.push(sub);
281 pub fn message(&self) -> String {
282 self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
285 pub fn styled_message(&self) -> &Vec<(String, Style)> {