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.
13 use DiagnosticStyledString;
18 use std::fmt::{self, Debug};
19 use std::ops::{Deref, DerefMut};
20 use std::thread::panicking;
21 use syntax_pos::{MultiSpan, Span};
23 /// Used for emitting structured error messages and other diagnostic information.
25 /// If there is some state in a downstream crate you would like to
26 /// access in the methods of `DiagnosticBuilder` here, consider
27 /// extending `HandlerFlags`, accessed via `self.handler.flags`.
30 pub struct DiagnosticBuilder<'a> {
31 pub handler: &'a Handler,
32 diagnostic: Diagnostic,
33 allow_suggestions: bool,
36 /// In general, the `DiagnosticBuilder` uses deref to allow access to
37 /// the fields and methods of the embedded `diagnostic` in a
38 /// transparent way. *However,* many of the methods are intended to
39 /// be used in a chained way, and hence ought to return `self`. In
40 /// that case, we can't just naively forward to the method on the
41 /// `diagnostic`, because the return type would be a `&Diagnostic`
42 /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
43 /// it easy to declare such methods on the builder.
44 macro_rules! forward {
45 // Forward pattern for &self -> &Self
46 (pub fn $n:ident(&self, $($name:ident: $ty:ty),* $(,)*) -> &Self) => {
47 pub fn $n(&self, $($name: $ty),*) -> &Self {
49 self.diagnostic.$n($($name),*);
54 // Forward pattern for &mut self -> &mut Self
55 (pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)*) -> &mut Self) => {
56 pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
58 self.diagnostic.$n($($name),*);
63 // Forward pattern for &mut self -> &mut Self, with S: Into<MultiSpan>
64 // type parameter. No obvious way to make this more generic.
65 (pub fn $n:ident<S: Into<MultiSpan>>(
67 $($name:ident: $ty:ty),*
68 $(,)*) -> &mut Self) => {
69 pub fn $n<S: Into<MultiSpan>>(&mut self, $($name: $ty),*) -> &mut Self {
71 self.diagnostic.$n($($name),*);
77 impl<'a> Deref for DiagnosticBuilder<'a> {
78 type Target = Diagnostic;
80 fn deref(&self) -> &Diagnostic {
85 impl<'a> DerefMut for DiagnosticBuilder<'a> {
86 fn deref_mut(&mut self) -> &mut Diagnostic {
91 impl<'a> DiagnosticBuilder<'a> {
92 /// Emit the diagnostic.
93 pub fn emit(&mut self) {
98 self.handler.emit_db(&self);
102 /// Buffers the diagnostic for later emission, unless handler
103 /// has disabled such buffering.
104 pub fn buffer(mut self, buffered_diagnostics: &mut Vec<Diagnostic>) {
105 if self.handler.flags.dont_buffer_diagnostics || self.handler.flags.treat_err_as_bug {
110 // We need to use `ptr::read` because `DiagnosticBuilder`
111 // implements `Drop`.
114 diagnostic = ::std::ptr::read(&self.diagnostic);
115 ::std::mem::forget(self);
117 // Logging here is useful to help track down where in logs an error was
119 debug!("buffer: diagnostic={:?}", diagnostic);
120 buffered_diagnostics.push(diagnostic);
123 /// Convenience function for internal use, clients should use one of the
124 /// span_* methods instead.
125 pub fn sub<S: Into<MultiSpan>>(
131 let span = span.map(|s| s.into()).unwrap_or_else(|| MultiSpan::new());
132 self.diagnostic.sub(level, message, span, None);
136 /// Delay emission of this diagnostic as a bug.
138 /// This can be useful in contexts where an error indicates a bug but
139 /// typically this only happens when other compilation errors have already
140 /// happened. In those cases this can be used to defer emission of this
141 /// diagnostic as a bug in the compiler only if no other errors have been
144 /// In the meantime, though, callsites are required to deal with the "bug"
145 /// locally in whichever way makes the most sense.
146 pub fn delay_as_bug(&mut self) {
147 self.level = Level::Bug;
148 self.handler.delay_as_bug(self.diagnostic.clone());
152 /// Add a span/label to be included in the resulting snippet.
153 /// This is pushed onto the `MultiSpan` that was created when the
154 /// diagnostic was first built. If you don't call this function at
155 /// all, and you just supplied a `Span` to create the diagnostic,
156 /// then the snippet will just include that `Span`, which is
157 /// called the primary span.
158 pub fn span_label<T: Into<String>>(&mut self, span: Span, label: T) -> &mut Self {
159 self.diagnostic.span_label(span, label);
163 forward!(pub fn note_expected_found(&mut self,
164 label: &dyn fmt::Display,
165 expected: DiagnosticStyledString,
166 found: DiagnosticStyledString,
169 forward!(pub fn note_expected_found_extra(&mut self,
170 label: &dyn fmt::Display,
171 expected: DiagnosticStyledString,
172 found: DiagnosticStyledString,
173 expected_extra: &dyn fmt::Display,
174 found_extra: &dyn fmt::Display,
177 forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
178 forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self,
182 forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
183 forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
184 forward!(pub fn help(&mut self , msg: &str) -> &mut Self);
185 forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self,
190 #[deprecated(note = "Use `span_suggestion_short_with_applicability`")]
191 forward!(pub fn span_suggestion_short(
198 #[deprecated(note = "Use `multipart_suggestion_with_applicability`")]
199 forward!(pub fn multipart_suggestion(
202 suggestion: Vec<(Span, String)>,
205 #[deprecated(note = "Use `span_suggestion_with_applicability`")]
206 forward!(pub fn span_suggestion(&mut self,
212 #[deprecated(note = "Use `span_suggestions_with_applicability`")]
213 forward!(pub fn span_suggestions(&mut self,
216 suggestions: Vec<String>,
219 pub fn multipart_suggestion_with_applicability(&mut self,
221 suggestion: Vec<(Span, String)>,
222 applicability: Applicability,
224 if !self.allow_suggestions {
227 self.diagnostic.multipart_suggestion_with_applicability(
235 pub fn span_suggestion_with_applicability(&mut self,
239 applicability: Applicability)
241 if !self.allow_suggestions {
244 self.diagnostic.span_suggestion_with_applicability(
253 pub fn span_suggestions_with_applicability(&mut self,
256 suggestions: impl Iterator<Item = String>,
257 applicability: Applicability)
259 if !self.allow_suggestions {
262 self.diagnostic.span_suggestions_with_applicability(
271 pub fn span_suggestion_short_with_applicability(&mut self,
275 applicability: Applicability)
277 if !self.allow_suggestions {
280 self.diagnostic.span_suggestion_short_with_applicability(
288 forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
289 forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
291 pub fn allow_suggestions(&mut self, allow: bool) -> &mut Self {
292 self.allow_suggestions = allow;
296 /// Convenience function for internal use, clients should use one of the
297 /// struct_* methods on Handler.
298 pub fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
299 DiagnosticBuilder::new_with_code(handler, level, None, message)
302 /// Convenience function for internal use, clients should use one of the
303 /// struct_* methods on Handler.
304 pub fn new_with_code(handler: &'a Handler,
306 code: Option<DiagnosticId>,
308 -> DiagnosticBuilder<'a> {
309 let diagnostic = Diagnostic::new_with_code(level, code, message);
310 DiagnosticBuilder::new_diagnostic(handler, diagnostic)
313 /// Creates a new `DiagnosticBuilder` with an already constructed
315 pub fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic)
316 -> DiagnosticBuilder<'a> {
320 allow_suggestions: true,
325 impl<'a> Debug for DiagnosticBuilder<'a> {
326 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
327 self.diagnostic.fmt(f)
331 /// Destructor bomb - a `DiagnosticBuilder` must be either emitted or canceled
332 /// or we emit a bug.
333 impl<'a> Drop for DiagnosticBuilder<'a> {
335 if !panicking() && !self.cancelled() {
336 let mut db = DiagnosticBuilder::new(self.handler,
338 "Error constructed but not emitted");