]> git.lizzy.rs Git - rust.git/blob - src/libproc_macro/diagnostic.rs
Rollup merge of #67507 - Mark-Simulacrum:purge-uninit, r=Centril
[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 S: MultiSpan, T: Into<String>
65         {
66             self.children.push(Diagnostic::spanned(spans, $level, message));
67             self
68         }
69
70         /// Adds a new child diagnostic message to `self` with the level
71         /// identified by this method's name with the given `message`.
72         #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
73         pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
74             self.children.push(Diagnostic::new($level, message));
75             self
76         }
77     )
78 }
79
80 /// Iterator over the children diagnostics of a `Diagnostic`.
81 #[derive(Debug, Clone)]
82 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
83 pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
84
85 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
86 impl<'a> Iterator for Children<'a> {
87     type Item = &'a Diagnostic;
88
89     fn next(&mut self) -> Option<Self::Item> {
90         self.0.next()
91     }
92 }
93
94 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
95 impl Diagnostic {
96     /// Creates a new diagnostic with the given `level` and `message`.
97     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
98     pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
99         Diagnostic { level: level, message: message.into(), spans: vec![], children: vec![] }
100     }
101
102     /// Creates a new diagnostic with the given `level` and `message` pointing to
103     /// the given set of `spans`.
104     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
105     pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
106     where
107         S: MultiSpan,
108         T: Into<String>,
109     {
110         Diagnostic {
111             level: level,
112             message: message.into(),
113             spans: spans.into_spans(),
114             children: vec![],
115         }
116     }
117
118     diagnostic_child_methods!(span_error, error, Level::Error);
119     diagnostic_child_methods!(span_warning, warning, Level::Warning);
120     diagnostic_child_methods!(span_note, note, Level::Note);
121     diagnostic_child_methods!(span_help, help, Level::Help);
122
123     /// Returns the diagnostic `level` for `self`.
124     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
125     pub fn level(&self) -> Level {
126         self.level
127     }
128
129     /// Sets the level in `self` to `level`.
130     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
131     pub fn set_level(&mut self, level: Level) {
132         self.level = level;
133     }
134
135     /// Returns the message in `self`.
136     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
137     pub fn message(&self) -> &str {
138         &self.message
139     }
140
141     /// Sets the message in `self` to `message`.
142     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
143     pub fn set_message<T: Into<String>>(&mut self, message: T) {
144         self.message = message.into();
145     }
146
147     /// Returns the `Span`s in `self`.
148     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
149     pub fn spans(&self) -> &[Span] {
150         &self.spans
151     }
152
153     /// Sets the `Span`s in `self` to `spans`.
154     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
155     pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
156         self.spans = spans.into_spans();
157     }
158
159     /// Returns an iterator over the children diagnostics of `self`.
160     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
161     pub fn children(&self) -> Children<'_> {
162         Children(self.children.iter())
163     }
164
165     /// Emit the diagnostic.
166     #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
167     pub fn emit(self) {
168         fn to_internal(spans: Vec<Span>) -> crate::bridge::client::MultiSpan {
169             let mut multi_span = crate::bridge::client::MultiSpan::new();
170             for span in spans {
171                 multi_span.push(span.0);
172             }
173             multi_span
174         }
175
176         let mut diag = crate::bridge::client::Diagnostic::new(
177             self.level,
178             &self.message[..],
179             to_internal(self.spans),
180         );
181         for c in self.children {
182             diag.sub(c.level, &c.message[..], to_internal(c.spans));
183         }
184         diag.emit();
185     }
186 }