1use crate::Span;
2
3#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
5#[derive(#[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl ::core::marker::Copy for Level { }Copy, #[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl ::core::clone::Clone for Level {
#[inline]
fn clone(&self) -> Level { *self }
}Clone, #[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl ::core::fmt::Debug for Level {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Level::Error => "Error",
Level::Warning => "Warning",
Level::Note => "Note",
Level::Help => "Help",
})
}
}Debug)]
6#[non_exhaustive]
7pub enum Level {
8 Error,
10 Warning,
12 Note,
14 Help,
16}
17
18#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
20pub trait MultiSpan {
21 fn into_spans(self) -> Vec<Span>;
23}
24
25#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
26impl MultiSpan for Span {
27 fn into_spans(self) -> Vec<Span> {
28 <[_]>::into_vec(::alloc::boxed::box_new([self]))vec![self]
29 }
30}
31
32#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
33impl MultiSpan for Vec<Span> {
34 fn into_spans(self) -> Vec<Span> {
35 self
36 }
37}
38
39#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
40impl<'a> MultiSpan for &'a [Span] {
41 fn into_spans(self) -> Vec<Span> {
42 self.to_vec()
43 }
44}
45
46#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
49#[derive(#[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl ::core::clone::Clone for Diagnostic {
#[inline]
fn clone(&self) -> Diagnostic {
Diagnostic {
level: ::core::clone::Clone::clone(&self.level),
message: ::core::clone::Clone::clone(&self.message),
spans: ::core::clone::Clone::clone(&self.spans),
children: ::core::clone::Clone::clone(&self.children),
}
}
}Clone, #[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl ::core::fmt::Debug for Diagnostic {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f, "Diagnostic",
"level", &self.level, "message", &self.message, "spans",
&self.spans, "children", &&self.children)
}
}Debug)]
50pub struct Diagnostic {
51 level: Level,
52 message: String,
53 spans: Vec<Span>,
54 children: Vec<Diagnostic>,
55}
56
57macro_rules! diagnostic_child_methods {
58 ($spanned:ident, $regular:ident, $level:expr) => {
59 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
60 #[doc = concat!("Adds a new child diagnostics message to `self` with the [`",
61 stringify!($level), "`] level, and the given `spans` and `message`.")]
62 pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
63 where
64 S: MultiSpan,
65 T: Into<String>,
66 {
67 self.children.push(Diagnostic::spanned(spans, $level, message));
68 self
69 }
70
71 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
72 #[doc = concat!("Adds a new child diagnostic message to `self` with the [`",
73 stringify!($level), "`] level, and the given `message`.")]
74 pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
75 self.children.push(Diagnostic::new($level, message));
76 self
77 }
78 };
79}
80
81#[derive(#[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl<'a> ::core::fmt::Debug for Children<'a> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Children",
&&self.0)
}
}Debug, #[automatically_derived]
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
impl<'a> ::core::clone::Clone for Children<'a> {
#[inline]
fn clone(&self) -> Children<'a> {
Children(::core::clone::Clone::clone(&self.0))
}
}Clone)]
83#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
84pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
85
86#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
87impl<'a> Iterator for Children<'a> {
88 type Item = &'a Diagnostic;
89
90 fn next(&mut self) -> Option<Self::Item> {
91 self.0.next()
92 }
93}
94
95#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
96impl Diagnostic {
97 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
99 pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
100 Diagnostic { level, message: message.into(), spans: ::alloc::vec::Vec::new()vec![], children: ::alloc::vec::Vec::new()vec![] }
101 }
102
103 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
106 pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
107 where
108 S: MultiSpan,
109 T: Into<String>,
110 {
111 Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: ::alloc::vec::Vec::new()vec![] }
112 }
113
114 "Adds a new child diagnostics message to `self` with the [`Level::Error`] level, and the given `spans` and `message`."
S
T
String
Self
mut self
S
spans
T
message
Diagnostic
self.children.push(Diagnostic::spanned(spans, Level::Error, message));
self;
"Adds a new child diagnostic message to `self` with the [`Level::Error`] level, and the given `message`."
String
Self
mut self
T
message
Diagnostic
self.children.push(Diagnostic::new(Level::Error, message));
self;diagnostic_child_methods!(span_error, error, Level::Error);
115 "Adds a new child diagnostics message to `self` with the [`Level::Warning`] level, and the given `spans` and `message`."
S
T
String
Self
mut self
S
spans
T
message
Diagnostic
self.children.push(Diagnostic::spanned(spans, Level::Warning, message));
self;
"Adds a new child diagnostic message to `self` with the [`Level::Warning`] level, and the given `message`."
String
Self
mut self
T
message
Diagnostic
self.children.push(Diagnostic::new(Level::Warning, message));
self;diagnostic_child_methods!(span_warning, warning, Level::Warning);
116 "Adds a new child diagnostics message to `self` with the [`Level::Note`] level, and the given `spans` and `message`."
S
T
String
Self
mut self
S
spans
T
message
Diagnostic
self.children.push(Diagnostic::spanned(spans, Level::Note, message));
self;
"Adds a new child diagnostic message to `self` with the [`Level::Note`] level, and the given `message`."
String
Self
mut self
T
message
Diagnostic
self.children.push(Diagnostic::new(Level::Note, message));
self;diagnostic_child_methods!(span_note, note, Level::Note);
117 "Adds a new child diagnostics message to `self` with the [`Level::Help`] level, and the given `spans` and `message`."
S
T
String
Self
mut self
S
spans
T
message
Diagnostic
self.children.push(Diagnostic::spanned(spans, Level::Help, message));
self;
"Adds a new child diagnostic message to `self` with the [`Level::Help`] level, and the given `message`."
String
Self
mut self
T
message
Diagnostic
self.children.push(Diagnostic::new(Level::Help, message));
self;diagnostic_child_methods!(span_help, help, Level::Help);
118
119 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
121 pub fn level(&self) -> Level {
122 self.level
123 }
124
125 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
127 pub fn set_level(&mut self, level: Level) {
128 self.level = level;
129 }
130
131 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
133 pub fn message(&self) -> &str {
134 &self.message
135 }
136
137 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
139 pub fn set_message<T: Into<String>>(&mut self, message: T) {
140 self.message = message.into();
141 }
142
143 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
145 pub fn spans(&self) -> &[Span] {
146 &self.spans
147 }
148
149 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
151 pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
152 self.spans = spans.into_spans();
153 }
154
155 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
157 pub fn children(&self) -> Children<'_> {
158 Children(self.children.iter())
159 }
160
161 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
163 pub fn emit(self) {
164 fn to_internal(diag: Diagnostic) -> crate::bridge::Diagnostic<crate::bridge::client::Span> {
165 crate::bridge::Diagnostic {
166 level: diag.level,
167 message: diag.message,
168 spans: diag.spans.into_iter().map(|s| s.0).collect(),
169 children: diag.children.into_iter().map(to_internal).collect(),
170 }
171 }
172
173 crate::bridge::client::FreeFunctions::emit_diagnostic(to_internal(self));
174 }
175}