]> git.lizzy.rs Git - rust.git/blob - src/libproc_macro/diagnostic.rs
Rollup merge of #53545 - FelixMcFelix:fix-50865-beta, r=petrochenkov
[rust.git] / src / libproc_macro / diagnostic.rs
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.
4 //
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.
10
11 use Span;
12
13 use rustc_errors as errors;
14 use syntax_pos::MultiSpan;
15
16 /// An enum representing a diagnostic level.
17 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
18 #[derive(Copy, Clone, Debug)]
19 #[non_exhaustive]
20 pub enum Level {
21     /// An error.
22     Error,
23     /// A warning.
24     Warning,
25     /// A note.
26     Note,
27     /// A help message.
28     Help,
29 }
30
31 /// A structure representing a diagnostic message and associated children
32 /// messages.
33 #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
34 #[derive(Clone, Debug)]
35 pub struct Diagnostic {
36     level: Level,
37     message: String,
38     span: Option<Span>,
39     children: Vec<Diagnostic>
40 }
41
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));
49             self
50         }
51
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));
57             self
58         }
59     )
60 }
61
62 impl Diagnostic {
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 {
66         Diagnostic {
67             level: level,
68             message: message.into(),
69             span: None,
70             children: vec![]
71         }
72     }
73
74     /// Create a new diagnostic with the given `level` and `message` pointing to
75     /// the given `span`.
76     #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
77     pub fn spanned<T: Into<String>>(span: Span, level: Level, message: T) -> Diagnostic {
78         Diagnostic {
79             level: level,
80             message: message.into(),
81             span: Some(span),
82             children: vec![]
83         }
84     }
85
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);
90
91     /// Returns the diagnostic `level` for `self`.
92     #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
93     pub fn level(&self) -> Level {
94         self.level
95     }
96
97     /// Emit the diagnostic.
98     #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
99     pub fn emit(self) {
100         let level = self.level.to_internal();
101         let mut diag = errors::Diagnostic::new(level, &*self.message);
102
103         if let Some(span) = self.span {
104             diag.set_span(span.0);
105         }
106
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);
111         }
112
113         ::__internal::with_sess(move |sess, _| {
114             errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();
115         });
116     }
117 }