3 /// An enum representing a diagnostic level.
4 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
5 #[derive(Copy, Clone, Debug)]
18 /// Trait implemented by types that can be converted into a set of `Span`s.
19 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
21 /// Converts `self` into a `Vec<Span>`.
22 fn into_spans(self) -> Vec<Span>;
25 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
26 impl MultiSpan for Span {
27 fn into_spans(self) -> Vec<Span> {
32 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
33 impl MultiSpan for Vec<Span> {
34 fn into_spans(self) -> Vec<Span> {
39 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
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 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
49 #[derive(Clone, Debug)]
50 pub struct Diagnostic {
54 children: Vec<Diagnostic>
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
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>
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 #[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));
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>);
85 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
86 impl<'a> Iterator for Children<'a> {
87 type Item = &'a Diagnostic;
89 fn next(&mut self) -> Option<Self::Item> {
94 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
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 {
101 message: message.into(),
107 /// Creates a new diagnostic with the given `level` and `message` pointing to
108 /// the given set of `spans`.
109 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
110 pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
111 where S: MultiSpan, T: Into<String>
115 message: message.into(),
116 spans: spans.into_spans(),
121 diagnostic_child_methods!(span_error, error, Level::Error);
122 diagnostic_child_methods!(span_warning, warning, Level::Warning);
123 diagnostic_child_methods!(span_note, note, Level::Note);
124 diagnostic_child_methods!(span_help, help, Level::Help);
126 /// Returns the diagnostic `level` for `self`.
127 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
128 pub fn level(&self) -> Level {
132 /// Sets the level in `self` to `level`.
133 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
134 pub fn set_level(&mut self, level: Level) {
138 /// Returns the message in `self`.
139 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
140 pub fn message(&self) -> &str {
144 /// Sets the message in `self` to `message`.
145 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
146 pub fn set_message<T: Into<String>>(&mut self, message: T) {
147 self.message = message.into();
150 /// Returns the `Span`s in `self`.
151 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
152 pub fn spans(&self) -> &[Span] {
156 /// Sets the `Span`s in `self` to `spans`.
157 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
158 pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
159 self.spans = spans.into_spans();
162 /// Returns an iterator over the children diagnostics of `self`.
163 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
164 pub fn children(&self) -> Children<'_> {
165 Children(self.children.iter())
168 /// Emit the diagnostic.
169 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
171 fn to_internal(spans: Vec<Span>) -> crate::bridge::client::MultiSpan {
172 let mut multi_span = crate::bridge::client::MultiSpan::new();
174 multi_span.push(span.0);
179 let mut diag = crate::bridge::client::Diagnostic::new(
182 to_internal(self.spans),
184 for c in self.children {
185 diag.sub(c.level, &c.message[..], to_internal(c.spans));