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.
14 use std::fmt::{self, Debug};
15 use std::ops::{Deref, DerefMut};
16 use std::thread::panicking;
17 use syntax_pos::{MultiSpan, Span};
19 /// Used for emitting structured error messages and other diagnostic information.
22 pub struct DiagnosticBuilder<'a> {
24 diagnostic: Diagnostic,
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),*);
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),*);
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),*);
62 impl<'a> Deref for DiagnosticBuilder<'a> {
63 type Target = Diagnostic;
65 fn deref(&self) -> &Diagnostic {
70 impl<'a> DerefMut for DiagnosticBuilder<'a> {
71 fn deref_mut(&mut self) -> &mut Diagnostic {
76 impl<'a> DiagnosticBuilder<'a> {
77 /// Emit the diagnostic.
78 pub fn emit(&mut self) {
88 self.handler.bump_err_count();
98 self.handler.emitter.borrow_mut().emit(&self);
100 self.handler.panic_if_treat_err_as_bug();
102 // if self.is_fatal() {
103 // panic!(FatalError);
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)
116 forward!(pub fn note_expected_found(&mut self,
117 label: &fmt::Display,
118 expected: &fmt::Display,
119 found: &fmt::Display)
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)
130 forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
131 forward!(pub fn span_note<S: Into<MultiSpan>>(&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,
142 forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&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);
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)
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,
160 code: Option<String>,
162 -> DiagnosticBuilder<'a> {
165 diagnostic: Diagnostic::new_with_code(level, code, message)
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();
177 impl<'a> Debug for DiagnosticBuilder<'a> {
178 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
179 self.diagnostic.fmt(f)
183 /// Destructor bomb - a DiagnosticBuilder must be either emitted or cancelled or
185 impl<'a> Drop for DiagnosticBuilder<'a> {
187 if !panicking() && !self.cancelled() {
188 let mut db = DiagnosticBuilder::new(self.handler,
190 "Error constructed but not emitted");