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