1
Fork 0

proc_macro: don't use DiagnosticBuilder for building up Diagnostics.

This commit is contained in:
Eduard-Mihai Burtescu 2018-04-21 21:33:20 +03:00
parent 11864c4e6c
commit c0adb05d34
2 changed files with 24 additions and 28 deletions

View file

@ -10,7 +10,8 @@
use Span; use Span;
use rustc_errors as rustc; use rustc_errors as errors;
use syntax_pos::MultiSpan;
/// An enum representing a diagnostic level. /// An enum representing a diagnostic level.
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")] #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
@ -97,37 +98,32 @@ impl Diagnostic {
/// Emit the diagnostic. /// Emit the diagnostic.
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")] #[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn emit(self) { pub fn emit(self) {
let level = self.level.to_internal();
let mut diag = errors::Diagnostic::new(level, &*self.message);
if let Some(span) = self.span {
diag.set_span(span.0);
}
for child in self.children {
let span = child.span.map_or(MultiSpan::new(), |s| s.0.into());
let level = child.level.to_internal();
diag.sub(level, &*child.message, span, None);
}
::__internal::with_sess(move |sess, _| { ::__internal::with_sess(move |sess, _| {
let handler = &sess.span_diagnostic; errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();
let level = __internal::level_to_internal_level(self.level);
let mut diag = rustc::DiagnosticBuilder::new(handler, level, &*self.message);
if let Some(span) = self.span {
diag.set_span(span.0);
}
for child in self.children {
let span = child.span.map(|s| s.0);
let level = __internal::level_to_internal_level(child.level);
diag.sub(level, &*child.message, span);
}
diag.emit();
}); });
} }
} }
#[unstable(feature = "proc_macro_internals", issue = "27812")] impl Level {
#[doc(hidden)] fn to_internal(self) -> errors::Level {
pub mod __internal { match self {
use super::{Level, rustc}; Level::Error => errors::Level::Error,
Level::Warning => errors::Level::Warning,
pub fn level_to_internal_level(level: Level) -> rustc::Level { Level::Note => errors::Level::Note,
match level { Level::Help => errors::Level::Help,
Level::Error => rustc::Level::Error,
Level::Warning => rustc::Level::Warning,
Level::Note => rustc::Level::Note,
Level::Help => rustc::Level::Help,
Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive") Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive")
} }
} }

View file

@ -379,7 +379,7 @@ impl Diagnostic {
/// Convenience function for internal use, clients should use one of the /// Convenience function for internal use, clients should use one of the
/// public methods above. /// public methods above.
pub(crate) fn sub(&mut self, pub fn sub(&mut self,
level: Level, level: Level,
message: &str, message: &str,
span: MultiSpan, span: MultiSpan,