]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/diagnostic_builder.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / librustc_errors / diagnostic_builder.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 Diagnostic;
12 use DiagnosticStyledString;
13
14 use Level;
15 use Handler;
16 use std::fmt::{self, Debug};
17 use std::ops::{Deref, DerefMut};
18 use std::thread::panicking;
19 use syntax_pos::{MultiSpan, Span};
20
21 /// Used for emitting structured error messages and other diagnostic information.
22 #[must_use]
23 #[derive(Clone)]
24 pub struct DiagnosticBuilder<'a> {
25     handler: &'a Handler,
26     diagnostic: Diagnostic,
27 }
28
29 /// In general, the `DiagnosticBuilder` uses deref to allow access to
30 /// the fields and methods of the embedded `diagnostic` in a
31 /// transparent way.  *However,* many of the methods are intended to
32 /// be used in a chained way, and hence ought to return `self`. In
33 /// that case, we can't just naively forward to the method on the
34 /// `diagnostic`, because the return type would be a `&Diagnostic`
35 /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
36 /// it easy to declare such methods on the builder.
37 macro_rules! forward {
38     // Forward pattern for &self -> &Self
39     (pub fn $n:ident(&self, $($name:ident: $ty:ty),*) -> &Self) => {
40         pub fn $n(&self, $($name: $ty),*) -> &Self {
41             self.diagnostic.$n($($name),*);
42             self
43         }
44     };
45
46     // Forward pattern for &mut self -> &mut Self
47     (pub fn $n:ident(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
48         pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
49             self.diagnostic.$n($($name),*);
50             self
51         }
52     };
53
54     // Forward pattern for &mut self -> &mut Self, with S: Into<MultiSpan>
55     // type parameter. No obvious way to make this more generic.
56     (pub fn $n:ident<S: Into<MultiSpan>>(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
57         pub fn $n<S: Into<MultiSpan>>(&mut self, $($name: $ty),*) -> &mut Self {
58             self.diagnostic.$n($($name),*);
59             self
60         }
61     };
62 }
63
64 impl<'a> Deref for DiagnosticBuilder<'a> {
65     type Target = Diagnostic;
66
67     fn deref(&self) -> &Diagnostic {
68         &self.diagnostic
69     }
70 }
71
72 impl<'a> DerefMut for DiagnosticBuilder<'a> {
73     fn deref_mut(&mut self) -> &mut Diagnostic {
74         &mut self.diagnostic
75     }
76 }
77
78 impl<'a> DiagnosticBuilder<'a> {
79     /// Emit the diagnostic.
80     pub fn emit(&mut self) {
81         if self.cancelled() {
82             return;
83         }
84
85         match self.level {
86             Level::Bug |
87             Level::Fatal |
88             Level::PhaseFatal |
89             Level::Error => {
90                 self.handler.bump_err_count();
91             }
92
93             Level::Warning |
94             Level::Note |
95             Level::Help |
96             Level::Cancelled => {
97             }
98         }
99
100         self.handler.emitter.borrow_mut().emit(&self);
101         self.cancel();
102         self.handler.panic_if_treat_err_as_bug();
103
104         // if self.is_fatal() {
105         //     panic!(FatalError);
106         // }
107     }
108
109     /// Add a span/label to be included in the resulting snippet.
110     /// This is pushed onto the `MultiSpan` that was created when the
111     /// diagnostic was first built. If you don't call this function at
112     /// all, and you just supplied a `Span` to create the diagnostic,
113     /// then the snippet will just include that `Span`, which is
114     /// called the primary span.
115     forward!(pub fn span_label(&mut self, span: Span, label: &fmt::Display)
116                                -> &mut Self);
117
118     forward!(pub fn note_expected_found(&mut self,
119                                         label: &fmt::Display,
120                                         expected: DiagnosticStyledString,
121                                         found: DiagnosticStyledString)
122                                         -> &mut Self);
123
124     forward!(pub fn note_expected_found_extra(&mut self,
125                                               label: &fmt::Display,
126                                               expected: DiagnosticStyledString,
127                                               found: DiagnosticStyledString,
128                                               expected_extra: &fmt::Display,
129                                               found_extra: &fmt::Display)
130                                               -> &mut Self);
131
132     forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
133     forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self,
134                                                   sp: S,
135                                                   msg: &str)
136                                                   -> &mut Self);
137     forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
138     forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
139     forward!(pub fn help(&mut self , msg: &str) -> &mut Self);
140     forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self,
141                                                   sp: S,
142                                                   msg: &str)
143                                                   -> &mut Self);
144     forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
145                                                         sp: S,
146                                                         msg: &str,
147                                                         suggestion: String)
148                                                         -> &mut Self);
149     forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
150     forward!(pub fn code(&mut self, s: String) -> &mut Self);
151
152     /// Convenience function for internal use, clients should use one of the
153     /// struct_* methods on Handler.
154     pub fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
155         DiagnosticBuilder::new_with_code(handler, level, None, message)
156     }
157
158     /// Convenience function for internal use, clients should use one of the
159     /// struct_* methods on Handler.
160     pub fn new_with_code(handler: &'a Handler,
161                          level: Level,
162                          code: Option<String>,
163                          message: &str)
164                          -> DiagnosticBuilder<'a> {
165         DiagnosticBuilder {
166             handler: handler,
167             diagnostic: Diagnostic::new_with_code(level, code, message)
168         }
169     }
170
171     pub fn into_diagnostic(mut self) -> Diagnostic {
172         // annoyingly, the Drop impl means we can't actually move
173         let result = self.diagnostic.clone();
174         self.cancel();
175         result
176     }
177 }
178
179 impl<'a> Debug for DiagnosticBuilder<'a> {
180     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181         self.diagnostic.fmt(f)
182     }
183 }
184
185 /// Destructor bomb - a DiagnosticBuilder must be either emitted or cancelled or
186 /// we emit a bug.
187 impl<'a> Drop for DiagnosticBuilder<'a> {
188     fn drop(&mut self) {
189         if !panicking() && !self.cancelled() {
190             let mut db = DiagnosticBuilder::new(self.handler,
191                                                 Level::Bug,
192                                                 "Error constructed but not emitted");
193             db.emit();
194             panic!();
195         }
196     }
197 }
198