]> git.lizzy.rs Git - rust.git/blob - src/libproc_macro/diagnostic.rs
Rollup merge of #69776 - ssomers:fix69769, r=Mark-Simulacrum
[rust.git] / src / libproc_macro / diagnostic.rs
1 use crate::Span;
2
3 /// An enum representing a diagnostic level.
4 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
5 #[derive(Copy, Clone, Debug)]
6 #[non_exhaustive]
7 pub enum Level {
8     /// An error.
9     Error,
10     /// A warning.
11     Warning,
12     /// A note.
13     Note,
14     /// A help message.
15     Help,
16 }
17
18 /// Trait implemented by types that can be converted into a set of `Span`s.
19 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
20 pub trait MultiSpan {
21     /// Converts `self` into a `Vec<Span>`.
22     fn into_spans(self) -> Vec<Span>;
23 }
24
25 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
26 impl MultiSpan for Span {
27     fn into_spans(self) -> Vec<Span> {
28         vec![self]
29     }
30 }
31
32 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
33 impl MultiSpan for Vec<Span> {
34     fn into_spans(self) -> Vec<Span> {
35         self
36     }
37 }
38
39 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
40 impl<'a> MultiSpan for &'a [Span] {
41     fn into_spans(self) -> Vec<Span> {
42         self.to_vec()
43     }
44 }
45
46 /// A structure representing a diagnostic message and associated children
47 /// messages.
48 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
49 #[derive(Clone, Debug)]
50 pub struct Diagnostic {
51     level: Level,
52     message: String,
53     spans: Vec<Span>,
54     children: Vec<Diagnostic>,
55 }
56
57 macro_rules! diagnostic_child_methods {
58     ($spanned:ident, $regular:ident, $level:expr) => {
59         /// Adds a new child diagnostic message to `self` with the level
60         /// identified by this method's name with the given `spans` and
61         /// `message`.
62         #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
63         pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
64         where
65             S: MultiSpan,
66             T: Into<String>,
67         {
68             self.children.push(Diagnostic::spanned(spans, $level, message));
69             self
70         }
71
72         /// Adds a new child diagnostic message to `self` with the level
73         /// identified by this method's name with the given `message`.
74         #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
75         pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
76             self.children.push(Diagnostic::new($level, message));
77             self
78         }
79     };
80 }
81
82 /// Iterator over the children diagnostics of a `Diagnostic`.
83 #[derive(Debug, Clone)]
84 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
85 pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
86
87 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
88 impl<'a> Iterator for Children<'a> {
89     type Item = &'a Diagnostic;
90
91     fn next(&mut self) -> Option<Self::Item> {
92         self.0.next()
93     }
94 }
95
96 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
97 impl Diagnostic {
98     /// Creates a new diagnostic with the given `level` and `message`.
99     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
100     pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
101         Diagnostic { level, message: message.into(), spans: vec![], children: vec![] }
102     }
103
104     /// Creates a new diagnostic with the given `level` and `message` pointing to
105     /// the given set of `spans`.
106     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
107     pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
108     where
109         S: MultiSpan,
110         T: Into<String>,
111     {
112         Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: vec![] }
113     }
114
115     diagnostic_child_methods!(span_error, error, Level::Error);
116     diagnostic_child_methods!(span_warning, warning, Level::Warning);
117     diagnostic_child_methods!(span_note, note, Level::Note);
118     diagnostic_child_methods!(span_help, help, Level::Help);
119
120     /// Returns the diagnostic `level` for `self`.
121     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
122     pub fn level(&self) -> Level {
123         self.level
124     }
125
126     /// Sets the level in `self` to `level`.
127     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
128     pub fn set_level(&mut self, level: Level) {
129         self.level = level;
130     }
131
132     /// Returns the message in `self`.
133     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
134     pub fn message(&self) -> &str {
135         &self.message
136     }
137
138     /// Sets the message in `self` to `message`.
139     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
140     pub fn set_message<T: Into<String>>(&mut self, message: T) {
141         self.message = message.into();
142     }
143
144     /// Returns the `Span`s in `self`.
145     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
146     pub fn spans(&self) -> &[Span] {
147         &self.spans
148     }
149
150     /// Sets the `Span`s in `self` to `spans`.
151     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
152     pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
153         self.spans = spans.into_spans();
154     }
155
156     /// Returns an iterator over the children diagnostics of `self`.
157     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
158     pub fn children(&self) -> Children<'_> {
159         Children(self.children.iter())
160     }
161
162     /// Emit the diagnostic.
163     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
164     pub fn emit(self) {
165         fn to_internal(spans: Vec<Span>) -> crate::bridge::client::MultiSpan {
166             let mut multi_span = crate::bridge::client::MultiSpan::new();
167             for span in spans {
168                 multi_span.push(span.0);
169             }
170             multi_span
171         }
172
173         let mut diag = crate::bridge::client::Diagnostic::new(
174             self.level,
175             &self.message[..],
176             to_internal(self.spans),
177         );
178         for c in self.children {
179             diag.sub(c.level, &c.message[..], to_internal(c.spans));
180         }
181         diag.emit();
182     }
183 }