1
Fork 0

Make JsonEmitter more like HumanEmitter.

Use `derive(Setters)` to derive setters, and then change
`JsonEmitter::new` to only have the arguments that are always used.
This commit is contained in:
Nicholas Nethercote 2024-02-29 16:03:59 +11:00
parent 2999d8dc72
commit 9ff4487999
5 changed files with 32 additions and 58 deletions

View file

@ -21,12 +21,11 @@ use crate::{
FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight, FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight,
SuggestionStyle, TerminalUrl, SuggestionStyle, TerminalUrl,
}; };
use rustc_lint_defs::pluralize;
use derive_setters::Setters; use derive_setters::Setters;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc}; use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
use rustc_error_messages::{FluentArgs, SpanLabel}; use rustc_error_messages::{FluentArgs, SpanLabel};
use rustc_lint_defs::pluralize;
use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::hygiene::{ExpnKind, MacroKind};
use std::borrow::Cow; use std::borrow::Cow;
use std::cmp::{max, min, Reverse}; use std::cmp::{max, min, Reverse};

View file

@ -9,9 +9,6 @@
// FIXME: spec the JSON output properly. // FIXME: spec the JSON output properly.
use rustc_span::source_map::SourceMap;
use termcolor::{ColorSpec, WriteColor};
use crate::emitter::{ use crate::emitter::{
should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter, should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter,
HumanReadableErrorType, HumanReadableErrorType,
@ -22,32 +19,39 @@ use crate::{
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
Subdiag, TerminalUrl, Subdiag, TerminalUrl,
}; };
use rustc_lint_defs::Applicability; use derive_setters::Setters;
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc}; use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
use rustc_error_messages::FluentArgs; use rustc_error_messages::FluentArgs;
use rustc_lint_defs::Applicability;
use rustc_span::hygiene::ExpnData; use rustc_span::hygiene::ExpnData;
use rustc_span::source_map::SourceMap;
use rustc_span::Span; use rustc_span::Span;
use serde::Serialize;
use std::error::Report; use std::error::Report;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::vec; use std::vec;
use termcolor::{ColorSpec, WriteColor};
use serde::Serialize;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[derive(Setters)]
pub struct JsonEmitter { pub struct JsonEmitter {
#[setters(skip)]
dst: IntoDynSyncSend<Box<dyn Write + Send>>, dst: IntoDynSyncSend<Box<dyn Write + Send>>,
registry: Option<Registry>, registry: Option<Registry>,
#[setters(skip)]
sm: Lrc<SourceMap>, sm: Lrc<SourceMap>,
fluent_bundle: Option<Lrc<FluentBundle>>, fluent_bundle: Option<Lrc<FluentBundle>>,
#[setters(skip)]
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
#[setters(skip)]
pretty: bool, pretty: bool,
ui_testing: bool, ui_testing: bool,
ignored_directories_in_source_blocks: Vec<String>, ignored_directories_in_source_blocks: Vec<String>,
#[setters(skip)]
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
diagnostic_width: Option<usize>, diagnostic_width: Option<usize>,
macro_backtrace: bool, macro_backtrace: bool,
@ -58,42 +62,28 @@ pub struct JsonEmitter {
impl JsonEmitter { impl JsonEmitter {
pub fn new( pub fn new(
dst: Box<dyn Write + Send>, dst: Box<dyn Write + Send>,
registry: Option<Registry>, sm: Lrc<SourceMap>,
source_map: Lrc<SourceMap>,
fluent_bundle: Option<Lrc<FluentBundle>>,
fallback_bundle: LazyFallbackBundle, fallback_bundle: LazyFallbackBundle,
pretty: bool, pretty: bool,
json_rendered: HumanReadableErrorType, json_rendered: HumanReadableErrorType,
diagnostic_width: Option<usize>,
macro_backtrace: bool,
track_diagnostics: bool,
terminal_url: TerminalUrl,
) -> JsonEmitter { ) -> JsonEmitter {
JsonEmitter { JsonEmitter {
dst: IntoDynSyncSend(dst), dst: IntoDynSyncSend(dst),
registry, registry: None,
sm: source_map, sm,
fluent_bundle, fluent_bundle: None,
fallback_bundle, fallback_bundle,
pretty, pretty,
ui_testing: false, ui_testing: false,
ignored_directories_in_source_blocks: Vec::new(), ignored_directories_in_source_blocks: Vec::new(),
json_rendered, json_rendered,
diagnostic_width, diagnostic_width: None,
macro_backtrace, macro_backtrace: false,
track_diagnostics, track_diagnostics: false,
terminal_url, terminal_url: TerminalUrl::No,
} }
} }
pub fn ui_testing(self, ui_testing: bool) -> Self {
Self { ui_testing, ..self }
}
pub fn ignored_directories_in_source_blocks(self, value: Vec<String>) -> Self {
Self { ignored_directories_in_source_blocks: value, ..self }
}
fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> { fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> {
if self.pretty { if self.pretty {
serde_json::to_writer_pretty(&mut *self.dst, &val)? serde_json::to_writer_pretty(&mut *self.dst, &val)?

View file

@ -48,16 +48,10 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
let output = Arc::new(Mutex::new(Vec::new())); let output = Arc::new(Mutex::new(Vec::new()));
let je = JsonEmitter::new( let je = JsonEmitter::new(
Box::new(Shared { data: output.clone() }), Box::new(Shared { data: output.clone() }),
None,
sm, sm,
None,
fallback_bundle, fallback_bundle,
true, true, // pretty
HumanReadableErrorType::Short(ColorConfig::Never), HumanReadableErrorType::Short(ColorConfig::Never),
None,
false,
false,
TerminalUrl::No,
); );
let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1)); let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));

View file

@ -1001,21 +1001,21 @@ fn default_emitter(
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new( config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(
JsonEmitter::new( JsonEmitter::new(
Box::new(io::BufWriter::new(io::stderr())), Box::new(io::BufWriter::new(io::stderr())),
Some(registry),
source_map, source_map,
bundle,
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
sopts.diagnostic_width,
macro_backtrace,
track_diagnostics,
terminal_url,
) )
.registry(Some(registry))
.fluent_bundle(bundle)
.ui_testing(sopts.unstable_opts.ui_testing) .ui_testing(sopts.unstable_opts.ui_testing)
.ignored_directories_in_source_blocks( .ignored_directories_in_source_blocks(
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(), sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
), )
.diagnostic_width(sopts.diagnostic_width)
.macro_backtrace(macro_backtrace)
.track_diagnostics(track_diagnostics)
.terminal_url(terminal_url),
), ),
} }
} }
@ -1482,16 +1482,10 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
} }
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new( config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new(
Box::new(io::BufWriter::new(io::stderr())), Box::new(io::BufWriter::new(io::stderr())),
None,
Lrc::new(SourceMap::new(FilePathMapping::empty())), Lrc::new(SourceMap::new(FilePathMapping::empty())),
None,
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
None,
false,
false,
TerminalUrl::No,
)), )),
}; };
emitter emitter

View file

@ -158,18 +158,15 @@ pub(crate) fn new_dcx(
Box::new( Box::new(
JsonEmitter::new( JsonEmitter::new(
Box::new(io::BufWriter::new(io::stderr())), Box::new(io::BufWriter::new(io::stderr())),
None,
source_map, source_map,
None,
fallback_bundle, fallback_bundle,
pretty, pretty,
json_rendered, json_rendered,
diagnostic_width,
false,
unstable_opts.track_diagnostics,
TerminalUrl::No,
) )
.ui_testing(unstable_opts.ui_testing), .ui_testing(unstable_opts.ui_testing)
.diagnostic_width(diagnostic_width)
.track_diagnostics(unstable_opts.track_diagnostics)
.terminal_url(TerminalUrl::No),
) )
} }
}; };