Skip to content

Commit 3966c54

Browse files
committed
Split ColorConfig off of HumanReadableErrorType
The previous setup tied two unrelated things together. Splitting these two is a better model.
1 parent ce20e15 commit 3966c54

File tree

8 files changed

+60
-53
lines changed

8 files changed

+60
-53
lines changed

compiler/rustc_errors/src/emitter.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,9 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
4343
/// Describes the way the content of the `rendered` field of the json output is generated
4444
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4545
pub enum HumanReadableErrorType {
46-
Default(ColorConfig),
47-
AnnotateSnippet(ColorConfig),
48-
Short(ColorConfig),
49-
}
50-
51-
impl HumanReadableErrorType {
52-
/// Returns a (`short`, `color`) tuple
53-
pub fn unzip(self) -> (bool, ColorConfig) {
54-
match self {
55-
HumanReadableErrorType::Default(cc) => (false, cc),
56-
HumanReadableErrorType::Short(cc) => (true, cc),
57-
HumanReadableErrorType::AnnotateSnippet(cc) => (false, cc),
58-
}
59-
}
46+
Default,
47+
AnnotateSnippet,
48+
Short,
6049
}
6150

6251
#[derive(Clone, Copy, Debug)]

compiler/rustc_errors/src/json.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct JsonEmitter {
5555
ignored_directories_in_source_blocks: Vec<String>,
5656
#[setters(skip)]
5757
json_rendered: HumanReadableErrorType,
58+
color_config: ColorConfig,
5859
diagnostic_width: Option<usize>,
5960
macro_backtrace: bool,
6061
track_diagnostics: bool,
@@ -68,6 +69,7 @@ impl JsonEmitter {
6869
fallback_bundle: LazyFallbackBundle,
6970
pretty: bool,
7071
json_rendered: HumanReadableErrorType,
72+
color_config: ColorConfig,
7173
) -> JsonEmitter {
7274
JsonEmitter {
7375
dst: IntoDynSyncSend(dst),
@@ -79,6 +81,7 @@ impl JsonEmitter {
7981
ui_testing: false,
8082
ignored_directories_in_source_blocks: Vec::new(),
8183
json_rendered,
84+
color_config,
8285
diagnostic_width: None,
8386
macro_backtrace: false,
8487
track_diagnostics: false,
@@ -173,7 +176,7 @@ impl Emitter for JsonEmitter {
173176
}
174177

175178
fn should_show_explain(&self) -> bool {
176-
!matches!(self.json_rendered, HumanReadableErrorType::Short(_))
179+
!matches!(self.json_rendered, HumanReadableErrorType::Short)
177180
}
178181
}
179182

@@ -353,8 +356,8 @@ impl Diagnostic {
353356

354357
let buf = BufWriter::default();
355358
let mut dst: Destination = Box::new(buf.clone());
356-
let (short, color_config) = je.json_rendered.unzip();
357-
match color_config {
359+
let short = matches!(je.json_rendered, HumanReadableErrorType::Short);
360+
match je.color_config {
358361
ColorConfig::Always | ColorConfig::Auto => dst = Box::new(termcolor::Ansi::new(dst)),
359362
ColorConfig::Never => {}
360363
}

compiler/rustc_errors/src/json/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
5050
sm,
5151
fallback_bundle,
5252
true, // pretty
53-
HumanReadableErrorType::Short(ColorConfig::Never),
53+
HumanReadableErrorType::Short,
54+
ColorConfig::Never,
5455
);
5556

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

compiler/rustc_session/src/config.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -602,20 +602,21 @@ impl OutputType {
602602
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
603603
pub enum ErrorOutputType {
604604
/// Output meant for the consumption of humans.
605-
HumanReadable(HumanReadableErrorType),
605+
HumanReadable(HumanReadableErrorType, ColorConfig),
606606
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
607607
Json {
608608
/// Render the JSON in a human readable way (with indents and newlines).
609609
pretty: bool,
610610
/// The JSON output includes a `rendered` field that includes the rendered
611611
/// human output.
612612
json_rendered: HumanReadableErrorType,
613+
color_config: ColorConfig,
613614
},
614615
}
615616

616617
impl Default for ErrorOutputType {
617618
fn default() -> Self {
618-
Self::HumanReadable(HumanReadableErrorType::Default(ColorConfig::Auto))
619+
Self::HumanReadable(HumanReadableErrorType::Default, ColorConfig::Auto)
619620
}
620621
}
621622

@@ -1631,6 +1632,7 @@ pub fn parse_color(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Col
16311632
/// Possible json config files
16321633
pub struct JsonConfig {
16331634
pub json_rendered: HumanReadableErrorType,
1635+
pub json_color: ColorConfig,
16341636
json_artifact_notifications: bool,
16351637
pub json_unused_externs: JsonUnusedExterns,
16361638
json_future_incompat: bool,
@@ -1668,8 +1670,7 @@ impl JsonUnusedExterns {
16681670
/// The first value returned is how to render JSON diagnostics, and the second
16691671
/// is whether or not artifact notifications are enabled.
16701672
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
1671-
let mut json_rendered: fn(ColorConfig) -> HumanReadableErrorType =
1672-
HumanReadableErrorType::Default;
1673+
let mut json_rendered = HumanReadableErrorType::Default;
16731674
let mut json_color = ColorConfig::Never;
16741675
let mut json_artifact_notifications = false;
16751676
let mut json_unused_externs = JsonUnusedExterns::No;
@@ -1696,7 +1697,8 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
16961697
}
16971698

16981699
JsonConfig {
1699-
json_rendered: json_rendered(json_color),
1700+
json_rendered,
1701+
json_color,
17001702
json_artifact_notifications,
17011703
json_unused_externs,
17021704
json_future_incompat,
@@ -1708,6 +1710,7 @@ pub fn parse_error_format(
17081710
early_dcx: &mut EarlyDiagCtxt,
17091711
matches: &getopts::Matches,
17101712
color: ColorConfig,
1713+
json_color: ColorConfig,
17111714
json_rendered: HumanReadableErrorType,
17121715
) -> ErrorOutputType {
17131716
// We need the `opts_present` check because the driver will send us Matches
@@ -1717,18 +1720,22 @@ pub fn parse_error_format(
17171720
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
17181721
match matches.opt_str("error-format").as_deref() {
17191722
None | Some("human") => {
1720-
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color))
1723+
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color)
17211724
}
17221725
Some("human-annotate-rs") => {
1723-
ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet(color))
1726+
ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet, color)
17241727
}
1725-
Some("json") => ErrorOutputType::Json { pretty: false, json_rendered },
1726-
Some("pretty-json") => ErrorOutputType::Json { pretty: true, json_rendered },
1727-
Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short(color)),
1728-
1728+
Some("json") => {
1729+
ErrorOutputType::Json { pretty: false, json_rendered, color_config: json_color }
1730+
}
1731+
Some("pretty-json") => {
1732+
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
1733+
}
1734+
Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short, color),
17291735
Some(arg) => {
17301736
early_dcx.abort_if_error_and_set_error_format(ErrorOutputType::HumanReadable(
1731-
HumanReadableErrorType::Default(color),
1737+
HumanReadableErrorType::Default,
1738+
color,
17321739
));
17331740
early_dcx.early_fatal(format!(
17341741
"argument for `--error-format` must be `human`, `json` or \
@@ -1737,7 +1744,7 @@ pub fn parse_error_format(
17371744
}
17381745
}
17391746
} else {
1740-
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color))
1747+
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color)
17411748
};
17421749

17431750
match error_format {
@@ -1791,7 +1798,7 @@ fn check_error_format_stability(
17911798
if let ErrorOutputType::Json { pretty: true, .. } = error_format {
17921799
early_dcx.early_fatal("`--error-format=pretty-json` is unstable");
17931800
}
1794-
if let ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet(_)) =
1801+
if let ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet, _) =
17951802
error_format
17961803
{
17971804
early_dcx.early_fatal("`--error-format=human-annotate-rs` is unstable");
@@ -2392,12 +2399,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
23922399

23932400
let JsonConfig {
23942401
json_rendered,
2402+
json_color,
23952403
json_artifact_notifications,
23962404
json_unused_externs,
23972405
json_future_incompat,
23982406
} = parse_json(early_dcx, matches);
23992407

2400-
let error_format = parse_error_format(early_dcx, matches, color, json_rendered);
2408+
let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered);
24012409

24022410
early_dcx.abort_if_error_and_set_error_format(error_format);
24032411

compiler/rustc_session/src/session.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,10 @@ fn default_emitter(
950950
t => t,
951951
};
952952
match sopts.error_format {
953-
config::ErrorOutputType::HumanReadable(kind) => {
954-
let (short, color_config) = kind.unzip();
953+
config::ErrorOutputType::HumanReadable(kind, color_config) => {
954+
let short = matches!(kind, HumanReadableErrorType::Short);
955955

956-
if let HumanReadableErrorType::AnnotateSnippet(_) = kind {
956+
if let HumanReadableErrorType::AnnotateSnippet = kind {
957957
let emitter = AnnotateSnippetEmitter::new(
958958
Some(source_map),
959959
bundle,
@@ -978,13 +978,14 @@ fn default_emitter(
978978
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
979979
}
980980
}
981-
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(
981+
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => Box::new(
982982
JsonEmitter::new(
983983
Box::new(io::BufWriter::new(io::stderr())),
984984
source_map,
985985
fallback_bundle,
986986
pretty,
987987
json_rendered,
988+
color_config,
988989
)
989990
.registry(Some(registry))
990991
.fluent_bundle(bundle)
@@ -1425,20 +1426,23 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
14251426
let fallback_bundle =
14261427
fallback_fluent_bundle(vec![rustc_errors::DEFAULT_LOCALE_RESOURCE], false);
14271428
let emitter: Box<DynEmitter> = match output {
1428-
config::ErrorOutputType::HumanReadable(kind) => {
1429-
let (short, color_config) = kind.unzip();
1429+
config::ErrorOutputType::HumanReadable(kind, color_config) => {
1430+
let short = matches!(kind, HumanReadableErrorType::Short);
14301431
Box::new(
14311432
HumanEmitter::new(stderr_destination(color_config), fallback_bundle)
14321433
.short_message(short),
14331434
)
14341435
}
1435-
config::ErrorOutputType::Json { pretty, json_rendered } => Box::new(JsonEmitter::new(
1436-
Box::new(io::BufWriter::new(io::stderr())),
1437-
Lrc::new(SourceMap::new(FilePathMapping::empty())),
1438-
fallback_bundle,
1439-
pretty,
1440-
json_rendered,
1441-
)),
1436+
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => {
1437+
Box::new(JsonEmitter::new(
1438+
Box::new(io::BufWriter::new(io::stderr())),
1439+
Lrc::new(SourceMap::new(FilePathMapping::empty())),
1440+
fallback_bundle,
1441+
pretty,
1442+
json_rendered,
1443+
color_config,
1444+
))
1445+
}
14421446
};
14431447
emitter
14441448
}

src/librustdoc/config.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ impl Options {
362362
}
363363

364364
let color = config::parse_color(early_dcx, matches);
365-
let config::JsonConfig { json_rendered, json_unused_externs, .. } =
365+
let config::JsonConfig { json_rendered, json_unused_externs, json_color, .. } =
366366
config::parse_json(early_dcx, matches);
367-
let error_format = config::parse_error_format(early_dcx, matches, color, json_rendered);
367+
let error_format =
368+
config::parse_error_format(early_dcx, matches, color, json_color, json_rendered);
368369
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
369370

370371
let codegen_options = CodegenOptions::build(early_dcx, matches);

src/librustdoc/core.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ pub(crate) fn new_dcx(
138138
false,
139139
);
140140
let emitter: Box<DynEmitter> = match error_format {
141-
ErrorOutputType::HumanReadable(kind) => {
142-
let (short, color_config) = kind.unzip();
141+
ErrorOutputType::HumanReadable(kind, color_config) => {
142+
let short = matches!(kind, HumanReadableErrorType::Short);
143143
Box::new(
144144
HumanEmitter::new(stderr_destination(color_config), fallback_bundle)
145145
.sm(source_map.map(|sm| sm as _))
@@ -150,7 +150,7 @@ pub(crate) fn new_dcx(
150150
.ui_testing(unstable_opts.ui_testing),
151151
)
152152
}
153-
ErrorOutputType::Json { pretty, json_rendered } => {
153+
ErrorOutputType::Json { pretty, json_rendered, color_config } => {
154154
let source_map = source_map.unwrap_or_else(|| {
155155
Lrc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))
156156
});
@@ -161,6 +161,7 @@ pub(crate) fn new_dcx(
161161
fallback_bundle,
162162
pretty,
163163
json_rendered,
164+
color_config,
164165
)
165166
.ui_testing(unstable_opts.ui_testing)
166167
.diagnostic_width(diagnostic_width)

src/librustdoc/doctest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ fn run_test(
422422
path_for_rustdoc.to_str().expect("target path must be valid unicode")
423423
}
424424
});
425-
if let ErrorOutputType::HumanReadable(kind) = rustdoc_options.error_format {
426-
let (short, color_config) = kind.unzip();
425+
if let ErrorOutputType::HumanReadable(kind, color_config) = rustdoc_options.error_format {
426+
let short = matches!(kind, HumanReadableErrorType::Short);
427427

428428
if short {
429429
compiler.arg("--error-format").arg("short");

0 commit comments

Comments
 (0)