1 // Copyright 2017 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 rustc_errors as errors;
14 use syntax_pos::MultiSpan;
16 /// An enum representing a diagnostic level.
17 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
18 #[derive(Copy, Clone, Debug)]
31 /// A structure representing a diagnostic message and associated children
33 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
34 #[derive(Clone, Debug)]
35 pub struct Diagnostic {
39 children: Vec<Diagnostic>
42 macro_rules! diagnostic_child_methods {
43 ($spanned:ident, $regular:ident, $level:expr) => (
44 /// Add a new child diagnostic message to `self` with the level
45 /// identified by this methods name with the given `span` and `message`.
46 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
47 pub fn $spanned<T: Into<String>>(mut self, span: Span, message: T) -> Diagnostic {
48 self.children.push(Diagnostic::spanned(span, $level, message));
52 /// Add a new child diagnostic message to `self` with the level
53 /// identified by this method's name with the given `message`.
54 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
55 pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
56 self.children.push(Diagnostic::new($level, message));
63 /// Create a new diagnostic with the given `level` and `message`.
64 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
65 pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
68 message: message.into(),
74 /// Create a new diagnostic with the given `level` and `message` pointing to
76 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
77 pub fn spanned<T: Into<String>>(span: Span, level: Level, message: T) -> Diagnostic {
80 message: message.into(),
86 diagnostic_child_methods!(span_error, error, Level::Error);
87 diagnostic_child_methods!(span_warning, warning, Level::Warning);
88 diagnostic_child_methods!(span_note, note, Level::Note);
89 diagnostic_child_methods!(span_help, help, Level::Help);
91 /// Returns the diagnostic `level` for `self`.
92 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
93 pub fn level(&self) -> Level {
97 /// Emit the diagnostic.
98 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
100 let level = self.level.to_internal();
101 let mut diag = errors::Diagnostic::new(level, &*self.message);
103 if let Some(span) = self.span {
104 diag.set_span(span.0);
107 for child in self.children {
108 let span = child.span.map_or(MultiSpan::new(), |s| s.0.into());
109 let level = child.level.to_internal();
110 diag.sub(level, &*child.message, span, None);
113 ::__internal::with_sess(move |sess, _| {
114 errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();