From cdd805506e7a01d60906f8b153afb697d687609d Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 7 Sep 2019 09:57:11 -0400 Subject: [PATCH] Replace DiagnosticBuilder with Diagnostic when emitting error --- src/librustc/session/mod.rs | 14 +++++++--- src/librustc_codegen_ssa/back/write.rs | 4 +-- src/librustc_driver/lib.rs | 1 + .../annotate_snippet_emitter_writer.rs | 12 ++++++--- src/librustc_errors/emitter.rs | 20 +++++++++----- src/librustc_errors/lib.rs | 3 ++- src/librustdoc/core.rs | 2 ++ src/librustdoc/test.rs | 2 +- src/libsyntax/json.rs | 26 +++++++++++++------ src/libsyntax/parse/lexer/tests.rs | 1 + src/libsyntax/tests.rs | 1 + 11 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 8656ebb2e6d..af9cb5a0941 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -1040,6 +1040,7 @@ fn default_emitter( source_map: &Lrc, emitter_dest: Option>, ) -> Box { + 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(); @@ -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 { @@ -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, @@ -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)) @@ -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( @@ -1086,6 +1091,7 @@ fn default_emitter( source_map.clone(), pretty, json_rendered, + external_macro_backtrace, ).ui_testing(sopts.debugging_opts.ui_testing), ), } @@ -1382,10 +1388,10 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! { let emitter: Box = 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); @@ -1396,10 +1402,10 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) { let emitter: Box = 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); diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index eec09842623..38a0818d290 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -22,7 +22,7 @@ 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; @@ -1725,7 +1725,7 @@ pub fn fatal(&self, msg: &str) { } 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(), diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 8c5d8536c32..3a09f459e9c 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -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); diff --git a/src/librustc_errors/annotate_snippet_emitter_writer.rs b/src/librustc_errors/annotate_snippet_emitter_writer.rs index c626dd0434d..0281d10fd93 100644 --- a/src/librustc_errors/annotate_snippet_emitter_writer.rs +++ b/src/librustc_errors/annotate_snippet_emitter_writer.rs @@ -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; @@ -25,11 +25,13 @@ 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); @@ -37,7 +39,7 @@ fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) { &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(), @@ -163,12 +165,14 @@ fn annotation_type_for_level(level: Level) -> AnnotationType { impl AnnotateSnippetEmitterWriter { pub fn new( source_map: Option>, - short_message: bool + short_message: bool, + external_macro_backtrace: bool, ) -> Self { Self { source_map, short_message, ui_testing: false, + external_macro_backtrace, } } diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 66608361c8d..c5274c7e99a 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -12,7 +12,7 @@ use syntax_pos::{SourceFile, Span, MultiSpan}; use crate::{ - Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, + Level, CodeSuggestion, Diagnostic, SubDiagnostic, SuggestionStyle, SourceMapperDyn, DiagnosticId, }; use crate::Level::Error; @@ -52,10 +52,12 @@ pub fn new_emitter( source_map: Option>, teach: bool, terminal_width: Option, + 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) } } @@ -180,7 +182,7 @@ fn right(&self, line_len: usize) -> usize { /// 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, @@ -204,7 +206,7 @@ fn should_show_explain(&self) -> bool { /// 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() { @@ -377,7 +379,7 @@ fn fix_multispan_in_std_macros(&self, } 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); @@ -385,7 +387,7 @@ fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) { &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(), @@ -449,6 +451,8 @@ pub struct EmitterWriter { teach: bool, ui_testing: bool, terminal_width: Option, + + external_macro_backtrace: bool, } #[derive(Debug)] @@ -465,6 +469,7 @@ pub fn stderr( short_message: bool, teach: bool, terminal_width: Option, + external_macro_backtrace: bool, ) -> EmitterWriter { let dst = Destination::from_stderr(color_config); EmitterWriter { @@ -474,6 +479,7 @@ pub fn stderr( teach, ui_testing: false, terminal_width, + external_macro_backtrace, } } @@ -484,6 +490,7 @@ pub fn new( teach: bool, colored: bool, terminal_width: Option, + external_macro_backtrace: bool, ) -> EmitterWriter { EmitterWriter { dst: Raw(dst, colored), @@ -492,6 +499,7 @@ pub fn new( teach, ui_testing: false, terminal_width, + external_macro_backtrace, } } diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index c1fba416d64..4ff18578bca 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -383,7 +383,8 @@ pub fn with_tty_emitter_and_flags(color_config: ColorConfig, cm: Option>, 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) } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 57b016a08c2..010e4cf6cd0 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -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) ) }, @@ -205,6 +206,7 @@ pub fn new_handler(error_format: ErrorOutputType, source_map, pretty, json_rendered, + false, ).ui_testing(ui_testing) ) }, diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 482c69c1ab5..424239c9982 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -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); diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index ada46f7bc5a..5cdea3aabbe 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -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}; @@ -32,6 +32,7 @@ pub struct JsonEmitter { pretty: bool, ui_testing: bool, json_rendered: HumanReadableErrorType, + external_macro_backtrace: bool, } impl JsonEmitter { @@ -40,6 +41,7 @@ pub fn stderr( source_map: Lrc, pretty: bool, json_rendered: HumanReadableErrorType, + external_macro_backtrace: bool, ) -> JsonEmitter { JsonEmitter { dst: Box::new(io::stderr()), @@ -48,13 +50,18 @@ pub fn stderr( 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( @@ -63,6 +70,7 @@ pub fn new( source_map: Lrc, pretty: bool, json_rendered: HumanReadableErrorType, + external_macro_backtrace: bool, ) -> JsonEmitter { JsonEmitter { dst, @@ -71,6 +79,7 @@ pub fn new( pretty, ui_testing: false, json_rendered, + external_macro_backtrace, } } @@ -80,8 +89,8 @@ pub fn ui_testing(self, ui_testing: bool) -> Self { } 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 { @@ -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| { @@ -219,8 +228,9 @@ fn flush(&mut self) -> io::Result<()> { } 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(); diff --git a/src/libsyntax/parse/lexer/tests.rs b/src/libsyntax/parse/lexer/tests.rs index c1ec41902e2..de301b1fc49 100644 --- a/src/libsyntax/parse/lexer/tests.rs +++ b/src/libsyntax/parse/lexer/tests.rs @@ -18,6 +18,7 @@ fn mk_sess(sm: Lrc) -> ParseSess { false, false, None, + false, ); ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm) } diff --git a/src/libsyntax/tests.rs b/src/libsyntax/tests.rs index 540881b0a54..f510ac9273d 100644 --- a/src/libsyntax/tests.rs +++ b/src/libsyntax/tests.rs @@ -147,6 +147,7 @@ fn test_harness(file_text: &str, span_labels: Vec, expected_output: & false, false, None, + false, ); let handler = Handler::with_emitter(true, None, Box::new(emitter)); handler.span_err(msp, "foo");