Skip to content

Commit

Permalink
Replace DiagnosticBuilder with Diagnostic when emitting error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Sep 17, 2019
1 parent 5670d04 commit cdd8055
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 26 deletions.
14 changes: 10 additions & 4 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ fn default_emitter(
source_map: &Lrc<source_map::SourceMap>,
emitter_dest: Option<Box<dyn Write + Send>>,
) -> Box<dyn Emitter + sync::Send> {
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
match (sopts.error_format, emitter_dest) {
(config::ErrorOutputType::HumanReadable(kind), dst) => {
let (short, color_config) = kind.unzip();
Expand All @@ -1048,6 +1049,7 @@ fn default_emitter(
let emitter = AnnotateSnippetEmitterWriter::new(
Some(source_map.clone()),
short,
external_macro_backtrace,
);
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
} else {
Expand All @@ -1058,6 +1060,7 @@ fn default_emitter(
short,
sopts.debugging_opts.teach,
sopts.debugging_opts.terminal_width,
external_macro_backtrace,
),
Some(dst) => EmitterWriter::new(
dst,
Expand All @@ -1066,6 +1069,7 @@ fn default_emitter(
false, // no teach messages when writing to a buffer
false, // no colors when writing to a buffer
None, // no terminal width
external_macro_backtrace,
),
};
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
Expand All @@ -1077,6 +1081,7 @@ fn default_emitter(
source_map.clone(),
pretty,
json_rendered,
external_macro_backtrace,
).ui_testing(sopts.debugging_opts.ui_testing),
),
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
Expand All @@ -1086,6 +1091,7 @@ fn default_emitter(
source_map.clone(),
pretty,
json_rendered,
external_macro_backtrace,
).ui_testing(sopts.debugging_opts.ui_testing),
),
}
Expand Down Expand Up @@ -1382,10 +1388,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
let emitter: Box<dyn Emitter + sync::Send> = match output {
config::ErrorOutputType::HumanReadable(kind) => {
let (short, color_config) = kind.unzip();
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
}
config::ErrorOutputType::Json { pretty, json_rendered } =>
Box::new(JsonEmitter::basic(pretty, json_rendered)),
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
};
let handler = errors::Handler::with_emitter(true, None, emitter);
handler.emit(&MultiSpan::new(), msg, errors::Level::Fatal);
Expand All @@ -1396,10 +1402,10 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
let emitter: Box<dyn Emitter + sync::Send> = match output {
config::ErrorOutputType::HumanReadable(kind) => {
let (short, color_config) = kind.unzip();
Box::new(EmitterWriter::stderr(color_config, None, short, false, None))
Box::new(EmitterWriter::stderr(color_config, None, short, false, None, false))
}
config::ErrorOutputType::Json { pretty, json_rendered } =>
Box::new(JsonEmitter::basic(pretty, json_rendered)),
Box::new(JsonEmitter::basic(pretty, json_rendered, false)),
};
let handler = errors::Handler::with_emitter(true, None, emitter);
handler.emit(&MultiSpan::new(), msg, errors::Level::Warning);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
use rustc::util::profiling::SelfProfiler;
use rustc_fs_util::link_or_copy;
use rustc_data_structures::svh::Svh;
use rustc_errors::{Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
use rustc_errors::{Handler, Level, FatalError, DiagnosticId};
use rustc_errors::emitter::{Emitter};
use rustc_target::spec::MergeFunctions;
use syntax::attr;
Expand Down Expand Up @@ -1725,7 +1725,7 @@ impl SharedEmitter {
}

impl Emitter for SharedEmitter {
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
fn emit_diagnostic(&mut self, db: &rustc_errors::Diagnostic) {
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
msg: db.message(),
code: db.code.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
false,
false,
None,
false,
));
let handler = errors::Handler::with_emitter(true, None, emitter);

Expand Down
12 changes: 8 additions & 4 deletions src/librustc_errors/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use syntax_pos::{SourceFile, MultiSpan, Loc};
use crate::{
Level, CodeSuggestion, DiagnosticBuilder, Emitter,
Level, CodeSuggestion, Diagnostic, Emitter,
SourceMapperDyn, SubDiagnostic, DiagnosticId
};
use crate::emitter::FileWithAnnotatedLines;
Expand All @@ -25,19 +25,21 @@ pub struct AnnotateSnippetEmitterWriter {
short_message: bool,
/// If true, will normalize line numbers with `LL` to prevent noise in UI test diffs.
ui_testing: bool,

external_macro_backtrace: bool,
}

impl Emitter for AnnotateSnippetEmitterWriter {
/// The entry point for the diagnostics generation
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
fn emit_diagnostic(&mut self, db: &Diagnostic) {
let mut children = db.children.clone();
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);

self.fix_multispans_in_std_macros(&self.source_map,
&mut primary_span,
&mut children,
&db.level,
db.handler().flags.external_macro_backtrace);
self.external_macro_backtrace);

self.emit_messages_default(&db.level,
db.message(),
Expand Down Expand Up @@ -163,12 +165,14 @@ impl<'a> DiagnosticConverter<'a> {
impl AnnotateSnippetEmitterWriter {
pub fn new(
source_map: Option<Lrc<SourceMapperDyn>>,
short_message: bool
short_message: bool,
external_macro_backtrace: bool,
) -> Self {
Self {
source_map,
short_message,
ui_testing: false,
external_macro_backtrace,
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use Destination::*;
use syntax_pos::{SourceFile, Span, MultiSpan};

use crate::{
Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic,
Level, CodeSuggestion, Diagnostic, SubDiagnostic,
SuggestionStyle, SourceMapperDyn, DiagnosticId,
};
use crate::Level::Error;
Expand Down Expand Up @@ -52,10 +52,12 @@ impl HumanReadableErrorType {
source_map: Option<Lrc<SourceMapperDyn>>,
teach: bool,
terminal_width: Option<usize>,
external_macro_backtrace: bool,
) -> EmitterWriter {
let (short, color_config) = self.unzip();
let color = color_config.suggests_using_colors();
EmitterWriter::new(dst, source_map, short, teach, color, terminal_width)
EmitterWriter::new(dst, source_map, short, teach, color, terminal_width,
external_macro_backtrace)
}
}

Expand Down Expand Up @@ -180,7 +182,7 @@ const ANONYMIZED_LINE_NUM: &str = "LL";
/// Emitter trait for emitting errors.
pub trait Emitter {
/// Emit a structured diagnostic.
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>);
fn emit_diagnostic(&mut self, db: &Diagnostic);

/// Emit a notification that an artifact has been output.
/// This is currently only supported for the JSON format,
Expand All @@ -204,7 +206,7 @@ pub trait Emitter {
/// we return the original `primary_span` and the original suggestions.
fn primary_span_formatted<'a>(
&mut self,
db: &'a DiagnosticBuilder<'_>
db: &'a Diagnostic
) -> (MultiSpan, &'a [CodeSuggestion]) {
let mut primary_span = db.span.clone();
if let Some((sugg, rest)) = db.suggestions.split_first() {
Expand Down Expand Up @@ -377,15 +379,15 @@ pub trait Emitter {
}

impl Emitter for EmitterWriter {
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
fn emit_diagnostic(&mut self, db: &Diagnostic) {
let mut children = db.children.clone();
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);

self.fix_multispans_in_std_macros(&self.sm,
&mut primary_span,
&mut children,
&db.level,
db.handler().flags.external_macro_backtrace);
self.external_macro_backtrace);

self.emit_messages_default(&db.level,
&db.styled_message(),
Expand Down Expand Up @@ -449,6 +451,8 @@ pub struct EmitterWriter {
teach: bool,
ui_testing: bool,
terminal_width: Option<usize>,

external_macro_backtrace: bool,
}

#[derive(Debug)]
Expand All @@ -465,6 +469,7 @@ impl EmitterWriter {
short_message: bool,
teach: bool,
terminal_width: Option<usize>,
external_macro_backtrace: bool,
) -> EmitterWriter {
let dst = Destination::from_stderr(color_config);
EmitterWriter {
Expand All @@ -474,6 +479,7 @@ impl EmitterWriter {
teach,
ui_testing: false,
terminal_width,
external_macro_backtrace,
}
}

Expand All @@ -484,6 +490,7 @@ impl EmitterWriter {
teach: bool,
colored: bool,
terminal_width: Option<usize>,
external_macro_backtrace: bool,
) -> EmitterWriter {
EmitterWriter {
dst: Raw(dst, colored),
Expand All @@ -492,6 +499,7 @@ impl EmitterWriter {
teach,
ui_testing: false,
terminal_width,
external_macro_backtrace,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ impl Handler {
cm: Option<Lrc<SourceMapperDyn>>,
flags: HandlerFlags)
-> Handler {
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false, None));
let emitter = Box::new(EmitterWriter::stderr(
color_config, cm, false, false, None, flags.external_macro_backtrace));
Handler::with_emitter_and_flags(emitter, flags)
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub fn new_handler(error_format: ErrorOutputType,
short,
sessopts.debugging_opts.teach,
sessopts.debugging_opts.terminal_width,
false,
).ui_testing(ui_testing)
)
},
Expand All @@ -205,6 +206,7 @@ pub fn new_handler(error_format: ErrorOutputType,
source_map,
pretty,
json_rendered,
false,
).ui_testing(ui_testing)
)
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ pub fn make_test(s: &str,
// Any errors in parsing should also appear when the doctest is compiled for real, so just
// send all the errors that libsyntax emits directly into a `Sink` instead of stderr.
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None);
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
let handler = Handler::with_emitter(false, None, box emitter);
let sess = ParseSess::with_span_handler(handler, cm);
Expand Down
26 changes: 18 additions & 8 deletions src/libsyntax/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use crate::source_map::{SourceMap, FilePathMapping};

use errors::registry::Registry;
use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, SourceMapper};
use errors::{SubDiagnostic, CodeSuggestion, SourceMapper};
use errors::{DiagnosticId, Applicability};
use errors::emitter::{Emitter, HumanReadableErrorType};

Expand All @@ -32,6 +32,7 @@ pub struct JsonEmitter {
pretty: bool,
ui_testing: bool,
json_rendered: HumanReadableErrorType,
external_macro_backtrace: bool,
}

impl JsonEmitter {
Expand All @@ -40,6 +41,7 @@ impl JsonEmitter {
source_map: Lrc<SourceMap>,
pretty: bool,
json_rendered: HumanReadableErrorType,
external_macro_backtrace: bool,
) -> JsonEmitter {
JsonEmitter {
dst: Box::new(io::stderr()),
Expand All @@ -48,13 +50,18 @@ impl JsonEmitter {
pretty,
ui_testing: false,
json_rendered,
external_macro_backtrace,
}
}

pub fn basic(pretty: bool, json_rendered: HumanReadableErrorType) -> JsonEmitter {
pub fn basic(
pretty: bool,
json_rendered: HumanReadableErrorType,
external_macro_backtrace: bool,
) -> JsonEmitter {
let file_path_mapping = FilePathMapping::empty();
JsonEmitter::stderr(None, Lrc::new(SourceMap::new(file_path_mapping)),
pretty, json_rendered)
pretty, json_rendered, external_macro_backtrace)
}

pub fn new(
Expand All @@ -63,6 +70,7 @@ impl JsonEmitter {
source_map: Lrc<SourceMap>,
pretty: bool,
json_rendered: HumanReadableErrorType,
external_macro_backtrace: bool,
) -> JsonEmitter {
JsonEmitter {
dst,
Expand All @@ -71,6 +79,7 @@ impl JsonEmitter {
pretty,
ui_testing: false,
json_rendered,
external_macro_backtrace,
}
}

Expand All @@ -80,8 +89,8 @@ impl JsonEmitter {
}

impl Emitter for JsonEmitter {
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
let data = Diagnostic::from_diagnostic_builder(db, self);
fn emit_diagnostic(&mut self, db: &errors::Diagnostic) {
let data = Diagnostic::from_errors_diagnostic(db, self);
let result = if self.pretty {
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
} else {
Expand Down Expand Up @@ -189,7 +198,7 @@ struct ArtifactNotification<'a> {
}

impl Diagnostic {
fn from_diagnostic_builder(db: &DiagnosticBuilder<'_>,
fn from_errors_diagnostic(db: &errors::Diagnostic,
je: &JsonEmitter)
-> Diagnostic {
let sugg = db.suggestions.iter().map(|sugg| {
Expand Down Expand Up @@ -219,8 +228,9 @@ impl Diagnostic {
}
let buf = BufWriter::default();
let output = buf.clone();
je.json_rendered.new_emitter(Box::new(buf), Some(je.sm.clone()), false, None)
.ui_testing(je.ui_testing).emit_diagnostic(db);
je.json_rendered.new_emitter(
Box::new(buf), Some(je.sm.clone()), false, None, je.external_macro_backtrace
).ui_testing(je.ui_testing).emit_diagnostic(db);
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
let output = String::from_utf8(output).unwrap();

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
false,
false,
None,
false,
);
ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm)
}
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
false,
false,
None,
false,
);
let handler = Handler::with_emitter(true, None, Box::new(emitter));
handler.span_err(msp, "foo");
Expand Down

0 comments on commit cdd8055

Please sign in to comment.