]> git.lizzy.rs Git - rust.git/blob - crates/proc_macro_srv/src/abis/abi_1_47/proc_macro/diagnostic.rs
Introduce proc_macro_srv::abis, impl 1.47 and 1.55
[rust.git] / crates / proc_macro_srv / src / abis / abi_1_47 / proc_macro / diagnostic.rs
1 //! lib-proc-macro diagnostic
2 //!
3 //! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/diagnostic.rs>
4 //! augmented with removing unstable features
5
6 use super::Span;
7
8 /// An enum representing a diagnostic level.
9 #[derive(Copy, Clone, Debug)]
10 #[non_exhaustive]
11 pub enum Level {
12     /// An error.
13     Error,
14     /// A warning.
15     Warning,
16     /// A note.
17     Note,
18     /// A help message.
19     Help,
20 }
21
22 /// Trait implemented by types that can be converted into a set of `Span`s.
23 pub trait MultiSpan {
24     /// Converts `self` into a `Vec<Span>`.
25     fn into_spans(self) -> Vec<Span>;
26 }
27
28 impl MultiSpan for Span {
29     fn into_spans(self) -> Vec<Span> {
30         vec![self]
31     }
32 }
33
34 impl MultiSpan for Vec<Span> {
35     fn into_spans(self) -> Vec<Span> {
36         self
37     }
38 }
39
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 #[derive(Clone, Debug)]
49 pub struct Diagnostic {
50     level: Level,
51     message: String,
52     spans: Vec<Span>,
53     children: Vec<Diagnostic>,
54 }
55
56 macro_rules! diagnostic_child_methods {
57     ($spanned:ident, $regular:ident, $level:expr) => {
58         /// Adds a new child diagnostic message to `self` with the level
59         /// identified by this method's name with the given `spans` and
60         /// `message`.
61         pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
62         where
63             S: MultiSpan,
64             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         pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
73             self.children.push(Diagnostic::new($level, message));
74             self
75         }
76     };
77 }
78
79 /// Iterator over the children diagnostics of a `Diagnostic`.
80 #[derive(Debug, Clone)]
81 pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
82
83 impl<'a> Iterator for Children<'a> {
84     type Item = &'a Diagnostic;
85
86     fn next(&mut self) -> Option<Self::Item> {
87         self.0.next()
88     }
89 }
90
91 impl Diagnostic {
92     /// Creates a new diagnostic with the given `level` and `message`.
93     pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
94         Diagnostic { level, message: message.into(), spans: vec![], children: vec![] }
95     }
96
97     /// Creates a new diagnostic with the given `level` and `message` pointing to
98     /// the given set of `spans`.
99     pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
100     where
101         S: MultiSpan,
102         T: Into<String>,
103     {
104         Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: vec![] }
105     }
106
107     diagnostic_child_methods!(span_error, error, Level::Error);
108     diagnostic_child_methods!(span_warning, warning, Level::Warning);
109     diagnostic_child_methods!(span_note, note, Level::Note);
110     diagnostic_child_methods!(span_help, help, Level::Help);
111
112     /// Returns the diagnostic `level` for `self`.
113     pub fn level(&self) -> Level {
114         self.level
115     }
116
117     /// Sets the level in `self` to `level`.
118     pub fn set_level(&mut self, level: Level) {
119         self.level = level;
120     }
121
122     /// Returns the message in `self`.
123     pub fn message(&self) -> &str {
124         &self.message
125     }
126
127     /// Sets the message in `self` to `message`.
128     pub fn set_message<T: Into<String>>(&mut self, message: T) {
129         self.message = message.into();
130     }
131
132     /// Returns the `Span`s in `self`.
133     pub fn spans(&self) -> &[Span] {
134         &self.spans
135     }
136
137     /// Sets the `Span`s in `self` to `spans`.
138     pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
139         self.spans = spans.into_spans();
140     }
141
142     /// Returns an iterator over the children diagnostics of `self`.
143     pub fn children(&self) -> Children<'_> {
144         Children(self.children.iter())
145     }
146
147     /// Emit the diagnostic.
148     pub fn emit(self) {
149         fn to_internal(spans: Vec<Span>) -> super::bridge::client::MultiSpan {
150             let mut multi_span = super::bridge::client::MultiSpan::new();
151             for span in spans {
152                 multi_span.push(span.0);
153             }
154             multi_span
155         }
156
157         let mut diag = super::bridge::client::Diagnostic::new(
158             self.level,
159             &self.message[..],
160             to_internal(self.spans),
161         );
162         for c in self.children {
163             diag.sub(c.level, &c.message[..], to_internal(c.spans));
164         }
165         diag.emit();
166     }
167 }