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.
26 pub struct DiagnosticBuilder<'a> {
27 pub handler: &'a Handler,
28 diagnostic: Diagnostic,
31 /// In general, the `DiagnosticBuilder` uses deref to allow access to
32 /// the fields and methods of the embedded `diagnostic` in a
33 /// transparent way. *However,* many of the methods are intended to
34 /// be used in a chained way, and hence ought to return `self`. In
35 /// that case, we can't just naively forward to the method on the
36 /// `diagnostic`, because the return type would be a `&Diagnostic`
37 /// instead of a `&DiagnosticBuilder<'a>`. This `forward!` macro makes
38 /// it easy to declare such methods on the builder.
39 macro_rules! forward {
40 // Forward pattern for &self -> &Self
41 (pub fn $n:ident(&self, $($name:ident: $ty:ty),*) -> &Self) => {
42 pub fn $n(&self, $($name: $ty),*) -> &Self {
43 self.diagnostic.$n($($name),*);
48 // Forward pattern for &mut self -> &mut Self
49 (pub fn $n:ident(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
50 pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
51 self.diagnostic.$n($($name),*);
56 // Forward pattern for &mut self -> &mut Self, with S: Into<MultiSpan>
57 // type parameter. No obvious way to make this more generic.
58 (pub fn $n:ident<S: Into<MultiSpan>>(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
59 pub fn $n<S: Into<MultiSpan>>(&mut self, $($name: $ty),*) -> &mut Self {
60 self.diagnostic.$n($($name),*);
66 impl<'a> Deref for DiagnosticBuilder<'a> {
67 type Target = Diagnostic;
69 fn deref(&self) -> &Diagnostic {
74 impl<'a> DerefMut for DiagnosticBuilder<'a> {
75 fn deref_mut(&mut self) -> &mut Diagnostic {
80 impl<'a> DiagnosticBuilder<'a> {
81 /// Emit the diagnostic.
82 pub fn emit(&mut self) {
87 self.handler.emit_db(&self);
91 pub fn is_error(&self) -> bool {
97 Level::FailureNote => {
104 Level::Cancelled => {
110 /// Convenience function for internal use, clients should use one of the
111 /// span_* methods instead.
112 pub fn sub<S: Into<MultiSpan>>(
118 let span = span.map(|s| s.into()).unwrap_or(MultiSpan::new());
119 self.diagnostic.sub(level, message, span, None);
123 /// Delay emission of this diagnostic as a bug.
125 /// This can be useful in contexts where an error indicates a bug but
126 /// typically this only happens when other compilation errors have already
127 /// happened. In those cases this can be used to defer emission of this
128 /// diagnostic as a bug in the compiler only if no other errors have been
131 /// In the meantime, though, callsites are required to deal with the "bug"
132 /// locally in whichever way makes the most sense.
133 pub fn delay_as_bug(&mut self) {
134 self.level = Level::Bug;
135 *self.handler.delayed_span_bug.borrow_mut() = Some(self.diagnostic.clone());
139 /// Add a span/label to be included in the resulting snippet.
140 /// This is pushed onto the `MultiSpan` that was created when the
141 /// diagnostic was first built. If you don't call this function at
142 /// all, and you just supplied a `Span` to create the diagnostic,
143 /// then the snippet will just include that `Span`, which is
144 /// called the primary span.
145 pub fn span_label<T: Into<String>>(&mut self, span: Span, label: T) -> &mut Self {
146 self.diagnostic.span_label(span, label);
150 forward!(pub fn note_expected_found(&mut self,
151 label: &fmt::Display,
152 expected: DiagnosticStyledString,
153 found: DiagnosticStyledString)
156 forward!(pub fn note_expected_found_extra(&mut self,
157 label: &fmt::Display,
158 expected: DiagnosticStyledString,
159 found: DiagnosticStyledString,
160 expected_extra: &fmt::Display,
161 found_extra: &fmt::Display)
164 forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
165 forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self,
169 forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
170 forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
171 forward!(pub fn help(&mut self , msg: &str) -> &mut Self);
172 forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self,
176 forward!(pub fn span_suggestion_short(&mut self,
181 forward!(pub fn span_suggestion(&mut self,
186 forward!(pub fn span_suggestions(&mut self,
189 suggestions: Vec<String>)
191 forward!(pub fn span_suggestion_with_applicability(&mut self,
195 applicability: Applicability)
197 forward!(pub fn span_suggestions_with_applicability(&mut self,
200 suggestions: Vec<String>,
201 applicability: Applicability)
203 forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
204 forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
206 /// Convenience function for internal use, clients should use one of the
207 /// struct_* methods on Handler.
208 pub fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
209 DiagnosticBuilder::new_with_code(handler, level, None, message)
212 /// Convenience function for internal use, clients should use one of the
213 /// struct_* methods on Handler.
214 pub fn new_with_code(handler: &'a Handler,
216 code: Option<DiagnosticId>,
218 -> DiagnosticBuilder<'a> {
219 let diagnostic = Diagnostic::new_with_code(level, code, message);
220 DiagnosticBuilder::new_diagnostic(handler, diagnostic)
223 /// Creates a new `DiagnosticBuilder` with an already constructed
225 pub fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic)
226 -> DiagnosticBuilder<'a> {
227 DiagnosticBuilder { handler, diagnostic }
231 impl<'a> Debug for DiagnosticBuilder<'a> {
232 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
233 self.diagnostic.fmt(f)
237 /// Destructor bomb - a `DiagnosticBuilder` must be either emitted or canceled
238 /// or we emit a bug.
239 impl<'a> Drop for DiagnosticBuilder<'a> {
241 if !panicking() && !self.cancelled() {
242 let mut db = DiagnosticBuilder::new(self.handler,
244 "Error constructed but not emitted");