diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 87e4a5ead4da..5637c05d04c6 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -21,12 +21,11 @@ use crate::{ FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight, SuggestionStyle, TerminalUrl, }; -use rustc_lint_defs::pluralize; - use derive_setters::Setters; use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc}; use rustc_error_messages::{FluentArgs, SpanLabel}; +use rustc_lint_defs::pluralize; use rustc_span::hygiene::{ExpnKind, MacroKind}; use std::borrow::Cow; use std::cmp::{max, min, Reverse}; diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index e99a70c393e1..3166768e9e27 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -9,9 +9,6 @@ // FIXME: spec the JSON output properly. -use rustc_span::source_map::SourceMap; -use termcolor::{ColorSpec, WriteColor}; - use crate::emitter::{ should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, @@ -22,32 +19,39 @@ use crate::{ diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, Subdiag, TerminalUrl, }; -use rustc_lint_defs::Applicability; - +use derive_setters::Setters; use rustc_data_structures::sync::{IntoDynSyncSend, Lrc}; use rustc_error_messages::FluentArgs; +use rustc_lint_defs::Applicability; use rustc_span::hygiene::ExpnData; +use rustc_span::source_map::SourceMap; use rustc_span::Span; +use serde::Serialize; use std::error::Report; use std::io::{self, Write}; use std::path::Path; use std::sync::{Arc, Mutex}; use std::vec; - -use serde::Serialize; +use termcolor::{ColorSpec, WriteColor}; #[cfg(test)] mod tests; +#[derive(Setters)] pub struct JsonEmitter { + #[setters(skip)] dst: IntoDynSyncSend>, registry: Option, + #[setters(skip)] sm: Lrc, fluent_bundle: Option>, + #[setters(skip)] fallback_bundle: LazyFallbackBundle, + #[setters(skip)] pretty: bool, ui_testing: bool, ignored_directories_in_source_blocks: Vec, + #[setters(skip)] json_rendered: HumanReadableErrorType, diagnostic_width: Option, macro_backtrace: bool, @@ -58,42 +62,28 @@ pub struct JsonEmitter { impl JsonEmitter { pub fn new( dst: Box, - registry: Option, - source_map: Lrc, - fluent_bundle: Option>, + sm: Lrc, fallback_bundle: LazyFallbackBundle, pretty: bool, json_rendered: HumanReadableErrorType, - diagnostic_width: Option, - macro_backtrace: bool, - track_diagnostics: bool, - terminal_url: TerminalUrl, ) -> JsonEmitter { JsonEmitter { dst: IntoDynSyncSend(dst), - registry, - sm: source_map, - fluent_bundle, + registry: None, + sm, + fluent_bundle: None, fallback_bundle, pretty, ui_testing: false, ignored_directories_in_source_blocks: Vec::new(), json_rendered, - diagnostic_width, - macro_backtrace, - track_diagnostics, - terminal_url, + diagnostic_width: None, + macro_backtrace: false, + track_diagnostics: false, + 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) -> Self { - Self { ignored_directories_in_source_blocks: value, ..self } - } - fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> { if self.pretty { serde_json::to_writer_pretty(&mut *self.dst, &val)? diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs index fc9948cb2fc6..80b4d2bf75c0 100644 --- a/compiler/rustc_errors/src/json/tests.rs +++ b/compiler/rustc_errors/src/json/tests.rs @@ -48,16 +48,10 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { let output = Arc::new(Mutex::new(Vec::new())); let je = JsonEmitter::new( Box::new(Shared { data: output.clone() }), - None, sm, - None, fallback_bundle, - true, + true, // pretty HumanReadableErrorType::Short(ColorConfig::Never), - None, - false, - false, - TerminalUrl::No, ); let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1)); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 2979d880dbd0..09fb6aa5d8f9 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1001,21 +1001,21 @@ fn default_emitter( config::ErrorOutputType::Json { pretty, json_rendered } => Box::new( JsonEmitter::new( Box::new(io::BufWriter::new(io::stderr())), - Some(registry), source_map, - bundle, fallback_bundle, pretty, json_rendered, - sopts.diagnostic_width, - macro_backtrace, - track_diagnostics, - terminal_url, ) + .registry(Some(registry)) + .fluent_bundle(bundle) .ui_testing(sopts.unstable_opts.ui_testing) .ignored_directories_in_source_blocks( 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 { } config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new( Box::new(io::BufWriter::new(io::stderr())), - None, Lrc::new(SourceMap::new(FilePathMapping::empty())), - None, fallback_bundle, pretty, json_rendered, - None, - false, - false, - TerminalUrl::No, )), }; emitter diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 463b8385d435..9ba79cf5d29f 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -158,18 +158,15 @@ pub(crate) fn new_dcx( Box::new( JsonEmitter::new( Box::new(io::BufWriter::new(io::stderr())), - None, source_map, - None, fallback_bundle, pretty, 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), ) } };