Skip to content

Commit 9ff4487

Browse files
committed
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.
1 parent 2999d8d commit 9ff4487

File tree

5 files changed

+32
-58
lines changed

5 files changed

+32
-58
lines changed

compiler/rustc_errors/src/emitter.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ use crate::{
2121
FluentBundle, LazyFallbackBundle, Level, MultiSpan, Subdiag, SubstitutionHighlight,
2222
SuggestionStyle, TerminalUrl,
2323
};
24-
use rustc_lint_defs::pluralize;
25-
2624
use derive_setters::Setters;
2725
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
2826
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
2927
use rustc_error_messages::{FluentArgs, SpanLabel};
28+
use rustc_lint_defs::pluralize;
3029
use rustc_span::hygiene::{ExpnKind, MacroKind};
3130
use std::borrow::Cow;
3231
use std::cmp::{max, min, Reverse};

compiler/rustc_errors/src/json.rs

+19-29
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
1010
// FIXME: spec the JSON output properly.
1111

12-
use rustc_span::source_map::SourceMap;
13-
use termcolor::{ColorSpec, WriteColor};
14-
1512
use crate::emitter::{
1613
should_show_source_code, ColorConfig, Destination, Emitter, HumanEmitter,
1714
HumanReadableErrorType,
@@ -22,32 +19,39 @@ use crate::{
2219
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
2320
Subdiag, TerminalUrl,
2421
};
25-
use rustc_lint_defs::Applicability;
26-
22+
use derive_setters::Setters;
2723
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
2824
use rustc_error_messages::FluentArgs;
25+
use rustc_lint_defs::Applicability;
2926
use rustc_span::hygiene::ExpnData;
27+
use rustc_span::source_map::SourceMap;
3028
use rustc_span::Span;
29+
use serde::Serialize;
3130
use std::error::Report;
3231
use std::io::{self, Write};
3332
use std::path::Path;
3433
use std::sync::{Arc, Mutex};
3534
use std::vec;
36-
37-
use serde::Serialize;
35+
use termcolor::{ColorSpec, WriteColor};
3836

3937
#[cfg(test)]
4038
mod tests;
4139

40+
#[derive(Setters)]
4241
pub struct JsonEmitter {
42+
#[setters(skip)]
4343
dst: IntoDynSyncSend<Box<dyn Write + Send>>,
4444
registry: Option<Registry>,
45+
#[setters(skip)]
4546
sm: Lrc<SourceMap>,
4647
fluent_bundle: Option<Lrc<FluentBundle>>,
48+
#[setters(skip)]
4749
fallback_bundle: LazyFallbackBundle,
50+
#[setters(skip)]
4851
pretty: bool,
4952
ui_testing: bool,
5053
ignored_directories_in_source_blocks: Vec<String>,
54+
#[setters(skip)]
5155
json_rendered: HumanReadableErrorType,
5256
diagnostic_width: Option<usize>,
5357
macro_backtrace: bool,
@@ -58,42 +62,28 @@ pub struct JsonEmitter {
5862
impl JsonEmitter {
5963
pub fn new(
6064
dst: Box<dyn Write + Send>,
61-
registry: Option<Registry>,
62-
source_map: Lrc<SourceMap>,
63-
fluent_bundle: Option<Lrc<FluentBundle>>,
65+
sm: Lrc<SourceMap>,
6466
fallback_bundle: LazyFallbackBundle,
6567
pretty: bool,
6668
json_rendered: HumanReadableErrorType,
67-
diagnostic_width: Option<usize>,
68-
macro_backtrace: bool,
69-
track_diagnostics: bool,
70-
terminal_url: TerminalUrl,
7169
) -> JsonEmitter {
7270
JsonEmitter {
7371
dst: IntoDynSyncSend(dst),
74-
registry,
75-
sm: source_map,
76-
fluent_bundle,
72+
registry: None,
73+
sm,
74+
fluent_bundle: None,
7775
fallback_bundle,
7876
pretty,
7977
ui_testing: false,
8078
ignored_directories_in_source_blocks: Vec::new(),
8179
json_rendered,
82-
diagnostic_width,
83-
macro_backtrace,
84-
track_diagnostics,
85-
terminal_url,
80+
diagnostic_width: None,
81+
macro_backtrace: false,
82+
track_diagnostics: false,
83+
terminal_url: TerminalUrl::No,
8684
}
8785
}
8886

89-
pub fn ui_testing(self, ui_testing: bool) -> Self {
90-
Self { ui_testing, ..self }
91-
}
92-
93-
pub fn ignored_directories_in_source_blocks(self, value: Vec<String>) -> Self {
94-
Self { ignored_directories_in_source_blocks: value, ..self }
95-
}
96-
9787
fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> {
9888
if self.pretty {
9989
serde_json::to_writer_pretty(&mut *self.dst, &val)?

compiler/rustc_errors/src/json/tests.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,10 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
4848
let output = Arc::new(Mutex::new(Vec::new()));
4949
let je = JsonEmitter::new(
5050
Box::new(Shared { data: output.clone() }),
51-
None,
5251
sm,
53-
None,
5452
fallback_bundle,
55-
true,
53+
true, // pretty
5654
HumanReadableErrorType::Short(ColorConfig::Never),
57-
None,
58-
false,
59-
false,
60-
TerminalUrl::No,
6155
);
6256

6357
let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1));

compiler/rustc_session/src/session.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1001,21 +1001,21 @@ fn default_emitter(
10011001
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(
10021002
JsonEmitter::new(
10031003
Box::new(io::BufWriter::new(io::stderr())),
1004-
Some(registry),
10051004
source_map,
1006-
bundle,
10071005
fallback_bundle,
10081006
pretty,
10091007
json_rendered,
1010-
sopts.diagnostic_width,
1011-
macro_backtrace,
1012-
track_diagnostics,
1013-
terminal_url,
10141008
)
1009+
.registry(Some(registry))
1010+
.fluent_bundle(bundle)
10151011
.ui_testing(sopts.unstable_opts.ui_testing)
10161012
.ignored_directories_in_source_blocks(
10171013
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
1018-
),
1014+
)
1015+
.diagnostic_width(sopts.diagnostic_width)
1016+
.macro_backtrace(macro_backtrace)
1017+
.track_diagnostics(track_diagnostics)
1018+
.terminal_url(terminal_url),
10191019
),
10201020
}
10211021
}
@@ -1482,16 +1482,10 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
14821482
}
14831483
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new(
14841484
Box::new(io::BufWriter::new(io::stderr())),
1485-
None,
14861485
Lrc::new(SourceMap::new(FilePathMapping::empty())),
1487-
None,
14881486
fallback_bundle,
14891487
pretty,
14901488
json_rendered,
1491-
None,
1492-
false,
1493-
false,
1494-
TerminalUrl::No,
14951489
)),
14961490
};
14971491
emitter

src/librustdoc/core.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,15 @@ pub(crate) fn new_dcx(
158158
Box::new(
159159
JsonEmitter::new(
160160
Box::new(io::BufWriter::new(io::stderr())),
161-
None,
162161
source_map,
163-
None,
164162
fallback_bundle,
165163
pretty,
166164
json_rendered,
167-
diagnostic_width,
168-
false,
169-
unstable_opts.track_diagnostics,
170-
TerminalUrl::No,
171165
)
172-
.ui_testing(unstable_opts.ui_testing),
166+
.ui_testing(unstable_opts.ui_testing)
167+
.diagnostic_width(diagnostic_width)
168+
.track_diagnostics(unstable_opts.track_diagnostics)
169+
.terminal_url(TerminalUrl::No),
173170
)
174171
}
175172
};

0 commit comments

Comments
 (0)