Replace DiagnosticBuilder with Diagnostic when emitting error

This commit is contained in:
Mark Rousskov 2019-09-07 09:57:11 -04:00
parent 5670d048c0
commit cdd805506e
11 changed files with 60 additions and 26 deletions

View File

@ -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();
@ -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<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);
@ -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);

View File

@ -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(),

View File

@ -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);

View File

@ -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<Lrc<SourceMapperDyn>>,
short_message: bool
short_message: bool,
external_macro_backtrace: bool,
) -> Self {
Self {
source_map,
short_message,
ui_testing: false,
external_macro_backtrace,
}
}

View File

@ -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<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)
}
}
@ -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<usize>,
external_macro_backtrace: bool,
}
#[derive(Debug)]
@ -465,6 +469,7 @@ pub fn stderr(
short_message: bool,
teach: bool,
terminal_width: Option<usize>,
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<usize>,
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,
}
}

View File

@ -383,7 +383,8 @@ pub fn with_tty_emitter_and_flags(color_config: ColorConfig,
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)
}

View File

@ -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)
)
},

View File

@ -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);

View File

@ -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<SourceMap>,
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<SourceMap>,
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();

View File

@ -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)
}

View File

@ -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");