Skip to content

Commit

Permalink
proc_macro: don't use DiagnosticBuilder for building up Diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jul 19, 2018
1 parent 11864c4 commit c0adb05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
46 changes: 21 additions & 25 deletions src/libproc_macro/diagnostic.rs
Expand Up @@ -10,7 +10,8 @@

use Span;

use rustc_errors as rustc;
use rustc_errors as errors;
use syntax_pos::MultiSpan;

/// An enum representing a diagnostic level.
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
Expand Down Expand Up @@ -97,37 +98,32 @@ impl Diagnostic {
/// Emit the diagnostic.
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
pub fn emit(self) {
::__internal::with_sess(move |sess, _| {
let handler = &sess.span_diagnostic;
let level = __internal::level_to_internal_level(self.level);
let mut diag = rustc::DiagnosticBuilder::new(handler, level, &*self.message);
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);
}
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);
}
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);
}

diag.emit();
::__internal::with_sess(move |sess, _| {
errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit();
});
}
}

#[unstable(feature = "proc_macro_internals", issue = "27812")]
#[doc(hidden)]
pub mod __internal {
use super::{Level, rustc};

pub fn level_to_internal_level(level: Level) -> rustc::Level {
match level {
Level::Error => rustc::Level::Error,
Level::Warning => rustc::Level::Warning,
Level::Note => rustc::Level::Note,
Level::Help => rustc::Level::Help,
impl Level {
fn to_internal(self) -> errors::Level {
match self {
Level::Error => errors::Level::Error,
Level::Warning => errors::Level::Warning,
Level::Note => errors::Level::Note,
Level::Help => errors::Level::Help,
Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_errors/diagnostic.rs
Expand Up @@ -379,7 +379,7 @@ impl Diagnostic {

/// Convenience function for internal use, clients should use one of the
/// public methods above.
pub(crate) fn sub(&mut self,
pub fn sub(&mut self,
level: Level,
message: &str,
span: MultiSpan,
Expand Down

0 comments on commit c0adb05

Please sign in to comment.