1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use rustc_errors as errors;
15 /// An enum representing a diagnostic level.
16 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
17 #[derive(Copy, Clone, Debug)]
30 /// Trait implemented by types that can be converted into a set of `Span`s.
31 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
33 /// Converts `self` into a `Vec<Span>`.
34 fn into_spans(self) -> Vec<Span>;
37 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
38 impl MultiSpan for Span {
39 fn into_spans(self) -> Vec<Span> {
44 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
45 impl MultiSpan for Vec<Span> {
46 fn into_spans(self) -> Vec<Span> {
51 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
52 impl<'a> MultiSpan for &'a [Span] {
53 fn into_spans(self) -> Vec<Span> {
58 /// A structure representing a diagnostic message and associated children
60 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
61 #[derive(Clone, Debug)]
62 pub struct Diagnostic {
66 children: Vec<Diagnostic>
69 macro_rules! diagnostic_child_methods {
70 ($spanned:ident, $regular:ident, $level:expr) => (
71 /// Add a new child diagnostic message to `self` with the level
72 /// identified by this method's name with the given `spans` and
74 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
75 pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
76 where S: MultiSpan, T: Into<String>
78 self.children.push(Diagnostic::spanned(spans, $level, message));
82 /// Add a new child diagnostic message to `self` with the level
83 /// identified by this method's name with the given `message`.
84 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
85 pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
86 self.children.push(Diagnostic::new($level, message));
92 /// Iterator over the children diagnostics of a `Diagnostic`.
93 #[derive(Debug, Clone)]
94 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
95 pub struct Children<'a>(::std::slice::Iter<'a, Diagnostic>);
97 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
98 impl<'a> Iterator for Children<'a> {
99 type Item = &'a Diagnostic;
101 fn next(&mut self) -> Option<Self::Item> {
106 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
108 /// Create a new diagnostic with the given `level` and `message`.
109 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
110 pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
113 message: message.into(),
119 /// Create a new diagnostic with the given `level` and `message` pointing to
120 /// the given set of `spans`.
121 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
122 pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
123 where S: MultiSpan, T: Into<String>
127 message: message.into(),
128 spans: spans.into_spans(),
133 diagnostic_child_methods!(span_error, error, Level::Error);
134 diagnostic_child_methods!(span_warning, warning, Level::Warning);
135 diagnostic_child_methods!(span_note, note, Level::Note);
136 diagnostic_child_methods!(span_help, help, Level::Help);
138 /// Returns the diagnostic `level` for `self`.
139 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
140 pub fn level(&self) -> Level {
144 /// Sets the level in `self` to `level`.
145 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
146 pub fn set_level(&mut self, level: Level) {
150 /// Returns the message in `self`.
151 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
152 pub fn message(&self) -> &str {
156 /// Sets the message in `self` to `message`.
157 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
158 pub fn set_message<T: Into<String>>(&mut self, message: T) {
159 self.message = message.into();
162 /// Returns the `Span`s in `self`.
163 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
164 pub fn spans(&self) -> &[Span] {
168 /// Sets the `Span`s in `self` to `spans`.
169 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
170 pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
171 self.spans = spans.into_spans();
174 /// Returns an iterator over the children diagnostics of `self`.
175 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
176 pub fn children(&self) -> Children {
177 Children(self.children.iter())
180 /// Emit the diagnostic.
181 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
183 fn to_internal(spans: Vec<Span>) -> ::syntax_pos::MultiSpan {
184 let spans: Vec<_> = spans.into_iter().map(|s| s.0).collect();
185 ::syntax_pos::MultiSpan::from_spans(spans)
188 let level = self.level.to_internal();
189 let mut diag = errors::Diagnostic::new(level, &*self.message);
190 diag.set_span(to_internal(self.spans));
192 for child in self.children {
193 let level = child.level.to_internal();
194 diag.sub(level, &*child.message, to_internal(child.spans), None);
197 ::__internal::with_sess(move |sess, _| {
198 errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();