1 //! lib-proc-macro diagnostic
3 //! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/diagnostic.rs>
4 //! augmented with removing unstable features
8 /// An enum representing a diagnostic level.
9 #[derive(Copy, Clone, Debug)]
22 /// Trait implemented by types that can be converted into a set of `Span`s.
24 /// Converts `self` into a `Vec<Span>`.
25 fn into_spans(self) -> Vec<Span>;
28 impl MultiSpan for Span {
29 fn into_spans(self) -> Vec<Span> {
34 impl MultiSpan for Vec<Span> {
35 fn into_spans(self) -> Vec<Span> {
40 impl<'a> MultiSpan for &'a [Span] {
41 fn into_spans(self) -> Vec<Span> {
46 /// A structure representing a diagnostic message and associated children
48 #[derive(Clone, Debug)]
49 pub struct Diagnostic {
53 children: Vec<Diagnostic>,
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
61 pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
66 self.children.push(Diagnostic::spanned(spans, $level, message));
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));
79 /// Iterator over the children diagnostics of a `Diagnostic`.
80 #[derive(Debug, Clone)]
81 pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
83 impl<'a> Iterator for Children<'a> {
84 type Item = &'a Diagnostic;
86 fn next(&mut self) -> Option<Self::Item> {
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![] }
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
104 Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: vec![] }
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);
112 /// Returns the diagnostic `level` for `self`.
113 pub fn level(&self) -> Level {
117 /// Sets the level in `self` to `level`.
118 pub fn set_level(&mut self, level: Level) {
122 /// Returns the message in `self`.
123 pub fn message(&self) -> &str {
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();
132 /// Returns the `Span`s in `self`.
133 pub fn spans(&self) -> &[Span] {
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();
142 /// Returns an iterator over the children diagnostics of `self`.
143 pub fn children(&self) -> Children<'_> {
144 Children(self.children.iter())
147 /// Emit the diagnostic.
149 fn to_internal(spans: Vec<Span>) -> super::bridge::client::MultiSpan {
150 let mut multi_span = super::bridge::client::MultiSpan::new();
152 multi_span.push(span.0);
157 let mut diag = super::bridge::client::Diagnostic::new(
160 to_internal(self.spans),
162 for c in self.children {
163 diag.sub(c.level, &c.message[..], to_internal(c.spans));