2017-08-28 02:56:43 -07:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use Span;
|
|
|
|
|
2018-04-21 21:33:20 +03:00
|
|
|
use rustc_errors as errors;
|
2017-08-28 02:56:43 -07:00
|
|
|
|
|
|
|
/// An enum representing a diagnostic level.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-15 16:56:54 +01:00
|
|
|
#[non_exhaustive]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub enum Level {
|
|
|
|
/// An error.
|
|
|
|
Error,
|
|
|
|
/// A warning.
|
|
|
|
Warning,
|
|
|
|
/// A note.
|
|
|
|
Note,
|
|
|
|
/// A help message.
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
/// Trait implemented by types that can be converted into a set of `Span`s.
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
pub trait MultiSpan {
|
|
|
|
/// Converts `self` into a `Vec<Span>`.
|
|
|
|
fn into_spans(self) -> Vec<Span>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
impl MultiSpan for Span {
|
|
|
|
fn into_spans(self) -> Vec<Span> {
|
|
|
|
vec![self]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
impl MultiSpan for Vec<Span> {
|
|
|
|
fn into_spans(self) -> Vec<Span> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
impl<'a> MultiSpan for &'a [Span] {
|
|
|
|
fn into_spans(self) -> Vec<Span> {
|
|
|
|
self.to_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 02:56:43 -07:00
|
|
|
/// A structure representing a diagnostic message and associated children
|
|
|
|
/// messages.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Diagnostic {
|
|
|
|
level: Level,
|
|
|
|
message: String,
|
2018-09-13 08:16:54 +00:00
|
|
|
spans: Vec<Span>,
|
2017-08-28 02:56:43 -07:00
|
|
|
children: Vec<Diagnostic>
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! diagnostic_child_methods {
|
|
|
|
($spanned:ident, $regular:ident, $level:expr) => (
|
|
|
|
/// Add a new child diagnostic message to `self` with the level
|
2018-09-13 08:16:54 +00:00
|
|
|
/// identified by this method's name with the given `spans` and
|
|
|
|
/// `message`.
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic
|
|
|
|
where S: MultiSpan, T: Into<String>
|
|
|
|
{
|
|
|
|
self.children.push(Diagnostic::spanned(spans, $level, message));
|
2017-08-28 02:56:43 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a new child diagnostic message to `self` with the level
|
|
|
|
/// identified by this method's name with the given `message`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic {
|
|
|
|
self.children.push(Diagnostic::new($level, message));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:06:13 -07:00
|
|
|
/// Iterator over the children diagnostics of a `Diagnostic`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
pub struct Children<'a>(::std::slice::Iter<'a, Diagnostic>);
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
impl<'a> Iterator for Children<'a> {
|
|
|
|
type Item = &'a Diagnostic;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
impl Diagnostic {
|
|
|
|
/// Create a new diagnostic with the given `level` and `message`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic {
|
|
|
|
Diagnostic {
|
|
|
|
level: level,
|
|
|
|
message: message.into(),
|
2018-09-13 08:16:54 +00:00
|
|
|
spans: vec![],
|
2017-08-28 02:56:43 -07:00
|
|
|
children: vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new diagnostic with the given `level` and `message` pointing to
|
2018-09-13 08:16:54 +00:00
|
|
|
/// the given set of `spans`.
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic
|
|
|
|
where S: MultiSpan, T: Into<String>
|
|
|
|
{
|
2017-08-28 02:56:43 -07:00
|
|
|
Diagnostic {
|
|
|
|
level: level,
|
|
|
|
message: message.into(),
|
2018-09-13 08:16:54 +00:00
|
|
|
spans: spans.into_spans(),
|
2017-08-28 02:56:43 -07:00
|
|
|
children: vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostic_child_methods!(span_error, error, Level::Error);
|
|
|
|
diagnostic_child_methods!(span_warning, warning, Level::Warning);
|
|
|
|
diagnostic_child_methods!(span_note, note, Level::Note);
|
|
|
|
diagnostic_child_methods!(span_help, help, Level::Help);
|
|
|
|
|
|
|
|
/// Returns the diagnostic `level` for `self`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub fn level(&self) -> Level {
|
|
|
|
self.level
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:06:13 -07:00
|
|
|
/// Sets the level in `self` to `level`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
pub fn set_level(&mut self, level: Level) {
|
|
|
|
self.level = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the message in `self`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
pub fn message(&self) -> &str {
|
|
|
|
&self.message
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the message in `self` to `message`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
pub fn set_message<T: Into<String>>(&mut self, message: T) {
|
|
|
|
self.message = message.into();
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
/// Returns the `Span`s in `self`.
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
pub fn spans(&self) -> &[Span] {
|
|
|
|
&self.spans
|
2018-07-31 00:06:13 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 08:16:54 +00:00
|
|
|
/// Sets the `Span`s in `self` to `spans`.
|
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
|
|
|
pub fn set_spans<S: MultiSpan>(&mut self, spans: S) {
|
|
|
|
self.spans = spans.into_spans();
|
2018-07-31 00:06:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the children diagnostics of `self`.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2018-07-31 00:06:13 -07:00
|
|
|
pub fn children(&self) -> Children {
|
|
|
|
Children(self.children.iter())
|
|
|
|
}
|
|
|
|
|
2017-08-28 02:56:43 -07:00
|
|
|
/// Emit the diagnostic.
|
2018-09-13 08:16:54 +00:00
|
|
|
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
|
2017-08-28 02:56:43 -07:00
|
|
|
pub fn emit(self) {
|
2018-09-13 08:16:54 +00:00
|
|
|
fn to_internal(spans: Vec<Span>) -> ::syntax_pos::MultiSpan {
|
|
|
|
let spans: Vec<_> = spans.into_iter().map(|s| s.0).collect();
|
|
|
|
::syntax_pos::MultiSpan::from_spans(spans)
|
|
|
|
}
|
|
|
|
|
2018-04-21 21:33:20 +03:00
|
|
|
let level = self.level.to_internal();
|
|
|
|
let mut diag = errors::Diagnostic::new(level, &*self.message);
|
2018-09-13 08:16:54 +00:00
|
|
|
diag.set_span(to_internal(self.spans));
|
2017-08-28 02:56:43 -07:00
|
|
|
|
2018-04-21 21:33:20 +03:00
|
|
|
for child in self.children {
|
|
|
|
let level = child.level.to_internal();
|
2018-09-13 08:16:54 +00:00
|
|
|
diag.sub(level, &*child.message, to_internal(child.spans), None);
|
2018-04-21 21:33:20 +03:00
|
|
|
}
|
2017-08-28 02:56:43 -07:00
|
|
|
|
2018-04-21 21:33:20 +03:00
|
|
|
::__internal::with_sess(move |sess, _| {
|
|
|
|
errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();
|
2017-08-28 02:56:43 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|