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 #[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
67 self.children.push(Diagnostic::spanned(spans, $level, message));
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));
81 /// Iterator over the children diagnostics of a `Diagnostic`.
82 #[derive(Debug, Clone)]
83 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
84 pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>);
86 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
87 impl<'a> Iterator for Children<'a> {
88 type Item = &'a Diagnostic;
90 fn next(&mut self) -> Option<Self::Item> {
95 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
97 /// Creates a new diagnostic with the given `level` and `message`.
98 #[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: vec![], children: vec![] }
103 /// Creates a new diagnostic with the given `level` and `message` pointing to
104 /// the given set of `spans`.
105 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
106 pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
111 Diagnostic { level, message: message.into(), spans: spans.into_spans(), children: vec![] }
114 diagnostic_child_methods!(span_error, error, Level::Error);
115 diagnostic_child_methods!(span_warning, warning, Level::Warning);
116 diagnostic_child_methods!(span_note, note, Level::Note);
117 diagnostic_child_methods!(span_help, help, Level::Help);
119 /// Returns the diagnostic `level` for `self`.
120 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
121 pub fn level(&self) -> Level {
125 /// Sets the level in `self` to `level`.
126 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
127 pub fn set_level(&mut self, level: Level) {
131 /// Returns the message in `self`.
132 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
133 pub fn message(&self) -> &str {
137 /// Sets the message in `self` to `message`.
138 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
139 pub fn set_message<T: Into<String>>(&mut self, message: T) {
140 self.message = message.into();
143 /// Returns the `Span`s in `self`.
144 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
145 pub fn spans(&self) -> &[Span] {
149 /// Sets the `Span`s in `self` to `spans`.
150 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
151 pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
152 self.spans = spans.into_spans();
155 /// Returns an iterator over the children diagnostics of `self`.
156 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
157 pub fn children(&self) -> Children<'_> {
158 Children(self.children.iter())
161 /// Emit the diagnostic.
162 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
164 fn to_internal(diag: Diagnostic) -> crate::bridge::Diagnostic<crate::bridge::client::Span> {
165 crate::bridge::Diagnostic {
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(),
173 crate::bridge::client::FreeFunctions::emit_diagnostic(to_internal(self));