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.
12 use DiagnosticStyledString;
16 use std::fmt::{self, Debug};
17 use std::ops::{Deref, DerefMut};
18 use std::thread::panicking;
19 use syntax_pos::{MultiSpan, Span};
21 /// Used for emitting structured error messages and other diagnostic information.
24 pub struct DiagnosticBuilder<'a> {
26 diagnostic: Diagnostic,
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),*);
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),*);
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),*);
64 impl<'a> Deref for DiagnosticBuilder<'a> {
65 type Target = Diagnostic;
67 fn deref(&self) -> &Diagnostic {
72 impl<'a> DerefMut for DiagnosticBuilder<'a> {
73 fn deref_mut(&mut self) -> &mut Diagnostic {
78 impl<'a> DiagnosticBuilder<'a> {
79 /// Emit the diagnostic.
80 pub fn emit(&mut self) {
90 self.handler.bump_err_count();
100 self.handler.emitter.borrow_mut().emit(&self);
102 self.handler.panic_if_treat_err_as_bug();
104 // if self.is_fatal() {
105 // panic!(FatalError);
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)
118 forward!(pub fn note_expected_found(&mut self,
119 label: &fmt::Display,
120 expected: DiagnosticStyledString,
121 found: DiagnosticStyledString)
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)
132 forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
133 forward!(pub fn span_note<S: Into<MultiSpan>>(&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,
144 forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&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);
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)
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,
162 code: Option<String>,
164 -> DiagnosticBuilder<'a> {
167 diagnostic: Diagnostic::new_with_code(level, code, message)
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();
179 impl<'a> Debug for DiagnosticBuilder<'a> {
180 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181 self.diagnostic.fmt(f)
185 /// Destructor bomb - a DiagnosticBuilder must be either emitted or cancelled or
187 impl<'a> Drop for DiagnosticBuilder<'a> {
189 if !panicking() && !self.cancelled() {
190 let mut db = DiagnosticBuilder::new(self.handler,
192 "Error constructed but not emitted");