Skip to content

Commit 018fe95

Browse files
committed
refactor: Make short a field on HumanReadableErrorType varinants
1 parent ffb67d0 commit 018fe95

File tree

7 files changed

+91
-91
lines changed

7 files changed

+91
-91
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;
4747
/// Describes the way the content of the `rendered` field of the json output is generated
4848
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4949
pub enum HumanReadableErrorType {
50-
Default,
51-
AnnotateSnippet { unicode: bool },
52-
Short,
50+
Default { short: bool },
51+
AnnotateSnippet { short: bool, unicode: bool },
5352
}
5453

5554
impl HumanReadableErrorType {
5655
pub fn short(&self) -> bool {
57-
*self == HumanReadableErrorType::Short
56+
match self {
57+
HumanReadableErrorType::Default { short }
58+
| HumanReadableErrorType::AnnotateSnippet { short, .. } => *short,
59+
}
5860
}
5961
}
6062

compiler/rustc_errors/src/json.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -371,42 +371,44 @@ impl Diagnostic {
371371
.insert(0, Diagnostic::from_sub_diagnostic(&diag.emitted_at_sub_diag(), &args, je));
372372
}
373373
let buf = BufWriter(Arc::new(Mutex::new(Vec::new())));
374-
let short = je.json_rendered.short();
375374
let dst: Destination = AutoStream::new(
376375
Box::new(buf.clone()),
377376
match je.color_config.to_color_choice() {
378377
ColorChoice::Auto => ColorChoice::Always,
379378
choice => choice,
380379
},
381380
);
382-
if let HumanReadableErrorType::AnnotateSnippet { unicode } = je.json_rendered {
383-
AnnotateSnippetEmitter::new(dst, je.translator.clone())
384-
.short_message(short)
385-
.sm(je.sm.clone())
386-
.diagnostic_width(je.diagnostic_width)
387-
.macro_backtrace(je.macro_backtrace)
388-
.track_diagnostics(je.track_diagnostics)
389-
.terminal_url(je.terminal_url)
390-
.ui_testing(je.ui_testing)
391-
.ignored_directories_in_source_blocks(
392-
je.ignored_directories_in_source_blocks.clone(),
393-
)
394-
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
395-
.emit_diagnostic(diag, registry)
396-
} else {
397-
HumanEmitter::new(dst, je.translator.clone())
398-
.short_message(short)
399-
.sm(je.sm.clone())
400-
.diagnostic_width(je.diagnostic_width)
401-
.macro_backtrace(je.macro_backtrace)
402-
.track_diagnostics(je.track_diagnostics)
403-
.terminal_url(je.terminal_url)
404-
.ui_testing(je.ui_testing)
405-
.ignored_directories_in_source_blocks(
406-
je.ignored_directories_in_source_blocks.clone(),
407-
)
408-
.theme(OutputTheme::Ascii)
409-
.emit_diagnostic(diag, registry)
381+
match je.json_rendered {
382+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
383+
AnnotateSnippetEmitter::new(dst, je.translator.clone())
384+
.short_message(short)
385+
.sm(je.sm.clone())
386+
.diagnostic_width(je.diagnostic_width)
387+
.macro_backtrace(je.macro_backtrace)
388+
.track_diagnostics(je.track_diagnostics)
389+
.terminal_url(je.terminal_url)
390+
.ui_testing(je.ui_testing)
391+
.ignored_directories_in_source_blocks(
392+
je.ignored_directories_in_source_blocks.clone(),
393+
)
394+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
395+
.emit_diagnostic(diag, registry)
396+
}
397+
HumanReadableErrorType::Default { short } => {
398+
HumanEmitter::new(dst, je.translator.clone())
399+
.short_message(short)
400+
.sm(je.sm.clone())
401+
.diagnostic_width(je.diagnostic_width)
402+
.macro_backtrace(je.macro_backtrace)
403+
.track_diagnostics(je.track_diagnostics)
404+
.terminal_url(je.terminal_url)
405+
.ui_testing(je.ui_testing)
406+
.ignored_directories_in_source_blocks(
407+
je.ignored_directories_in_source_blocks.clone(),
408+
)
409+
.theme(OutputTheme::Ascii)
410+
.emit_diagnostic(diag, registry)
411+
}
410412
}
411413

412414
let buf = Arc::try_unwrap(buf.0).unwrap().into_inner().unwrap();

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn test_search_paths_tracking_hash_different_order() {
321321
let early_dcx = EarlyDiagCtxt::new(JSON);
322322
const JSON: ErrorOutputType = ErrorOutputType::Json {
323323
pretty: false,
324-
json_rendered: HumanReadableErrorType::Default,
324+
json_rendered: HumanReadableErrorType::Default { short: false },
325325
color_config: ColorConfig::Never,
326326
};
327327

compiler/rustc_session/src/config.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ pub enum ErrorOutputType {
837837
/// Output meant for the consumption of humans.
838838
#[default]
839839
HumanReadable {
840-
kind: HumanReadableErrorType = HumanReadableErrorType::Default,
840+
kind: HumanReadableErrorType = HumanReadableErrorType::Default { short: false },
841841
color_config: ColorConfig = ColorConfig::Auto,
842842
},
843843
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
@@ -2051,7 +2051,7 @@ impl JsonUnusedExterns {
20512051
/// The first value returned is how to render JSON diagnostics, and the second
20522052
/// is whether or not artifact notifications are enabled.
20532053
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
2054-
let mut json_rendered = HumanReadableErrorType::Default;
2054+
let mut json_rendered = HumanReadableErrorType::Default { short: false };
20552055
let mut json_color = ColorConfig::Never;
20562056
let mut json_artifact_notifications = false;
20572057
let mut json_unused_externs = JsonUnusedExterns::No;
@@ -2067,9 +2067,12 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
20672067

20682068
for sub_option in option.split(',') {
20692069
match sub_option {
2070-
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
2070+
"diagnostic-short" => {
2071+
json_rendered = HumanReadableErrorType::Default { short: true }
2072+
}
20712073
"diagnostic-unicode" => {
2072-
json_rendered = HumanReadableErrorType::AnnotateSnippet { unicode: true };
2074+
json_rendered =
2075+
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true };
20732076
}
20742077
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
20752078
"artifacts" => json_artifact_notifications = true,
@@ -2108,7 +2111,7 @@ pub fn parse_error_format(
21082111
match matches.opt_str("error-format").as_deref() {
21092112
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
21102113
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
2111-
kind: HumanReadableErrorType::AnnotateSnippet { unicode: false },
2114+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false },
21122115
color_config,
21132116
},
21142117
Some("json") => {
@@ -2117,11 +2120,12 @@ pub fn parse_error_format(
21172120
Some("pretty-json") => {
21182121
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
21192122
}
2120-
Some("short") => {
2121-
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
2122-
}
2123+
Some("short") => ErrorOutputType::HumanReadable {
2124+
kind: HumanReadableErrorType::Default { short: true },
2125+
color_config,
2126+
},
21232127
Some("human-unicode") => ErrorOutputType::HumanReadable {
2124-
kind: HumanReadableErrorType::AnnotateSnippet { unicode: true },
2128+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true },
21252129
color_config,
21262130
},
21272131
Some(arg) => {
@@ -2189,8 +2193,8 @@ fn check_error_format_stability(
21892193
let format = match format {
21902194
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
21912195
ErrorOutputType::HumanReadable { kind, .. } => match kind {
2192-
HumanReadableErrorType::AnnotateSnippet { unicode: false } => "human-annotate-rs",
2193-
HumanReadableErrorType::AnnotateSnippet { unicode: true } => "human-unicode",
2196+
HumanReadableErrorType::AnnotateSnippet { unicode: false, .. } => "human-annotate-rs",
2197+
HumanReadableErrorType::AnnotateSnippet { unicode: true, .. } => "human-unicode",
21942198
_ => return,
21952199
},
21962200
_ => return,

compiler/rustc_session/src/session.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,8 @@ fn default_emitter(
950950
let source_map = if sopts.unstable_opts.link_only { None } else { Some(source_map) };
951951

952952
match sopts.error_format {
953-
config::ErrorOutputType::HumanReadable { kind, color_config } => {
954-
let short = kind.short();
955-
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
953+
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
954+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => {
956955
let emitter =
957956
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
958957
.sm(source_map)
@@ -969,7 +968,8 @@ fn default_emitter(
969968
.clone(),
970969
);
971970
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
972-
} else {
971+
}
972+
HumanReadableErrorType::Default { short } => {
973973
let emitter = HumanEmitter::new(stderr_destination(color_config), translator)
974974
.sm(source_map)
975975
.short_message(short)
@@ -983,7 +983,7 @@ fn default_emitter(
983983
);
984984
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
985985
}
986-
}
986+
},
987987
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => Box::new(
988988
JsonEmitter::new(
989989
Box::new(io::BufWriter::new(io::stderr())),
@@ -1490,22 +1490,18 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
14901490
let translator =
14911491
Translator::with_fallback_bundle(vec![rustc_errors::DEFAULT_LOCALE_RESOURCE], false);
14921492
let emitter: Box<DynEmitter> = match output {
1493-
config::ErrorOutputType::HumanReadable { kind, color_config } => {
1494-
let short = kind.short();
1495-
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
1496-
Box::new(
1497-
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
1498-
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
1499-
.short_message(short),
1500-
)
1501-
} else {
1502-
Box::new(
1503-
HumanEmitter::new(stderr_destination(color_config), translator)
1504-
.theme(OutputTheme::Ascii)
1505-
.short_message(short),
1506-
)
1507-
}
1508-
}
1493+
config::ErrorOutputType::HumanReadable { kind, color_config } => match kind {
1494+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => Box::new(
1495+
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
1496+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
1497+
.short_message(short),
1498+
),
1499+
HumanReadableErrorType::Default { short } => Box::new(
1500+
HumanEmitter::new(stderr_destination(color_config), translator)
1501+
.theme(OutputTheme::Ascii)
1502+
.short_message(short),
1503+
),
1504+
},
15091505
config::ErrorOutputType::Json { pretty, json_rendered, color_config } => {
15101506
Box::new(JsonEmitter::new(
15111507
Box::new(io::BufWriter::new(io::stderr())),

src/librustdoc/core.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,26 @@ pub(crate) fn new_dcx(
153153
) -> rustc_errors::DiagCtxt {
154154
let translator = rustc_driver::default_translator();
155155
let emitter: Box<DynEmitter> = match error_format {
156-
ErrorOutputType::HumanReadable { kind, color_config } => {
157-
let short = kind.short();
158-
if let HumanReadableErrorType::AnnotateSnippet { unicode } = kind {
159-
Box::new(
160-
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
161-
.sm(source_map.map(|sm| sm as _))
162-
.short_message(short)
163-
.diagnostic_width(diagnostic_width)
164-
.track_diagnostics(unstable_opts.track_diagnostics)
165-
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
166-
.ui_testing(unstable_opts.ui_testing),
167-
)
168-
} else {
169-
Box::new(
170-
HumanEmitter::new(stderr_destination(color_config), translator)
171-
.sm(source_map.map(|sm| sm as _))
172-
.short_message(short)
173-
.diagnostic_width(diagnostic_width)
174-
.track_diagnostics(unstable_opts.track_diagnostics)
175-
.theme(OutputTheme::Ascii)
176-
.ui_testing(unstable_opts.ui_testing),
177-
)
178-
}
179-
}
156+
ErrorOutputType::HumanReadable { kind, color_config } => match kind {
157+
HumanReadableErrorType::AnnotateSnippet { short, unicode } => Box::new(
158+
AnnotateSnippetEmitter::new(stderr_destination(color_config), translator)
159+
.sm(source_map.map(|sm| sm as _))
160+
.short_message(short)
161+
.diagnostic_width(diagnostic_width)
162+
.track_diagnostics(unstable_opts.track_diagnostics)
163+
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
164+
.ui_testing(unstable_opts.ui_testing),
165+
),
166+
HumanReadableErrorType::Default { short } => Box::new(
167+
HumanEmitter::new(stderr_destination(color_config), translator)
168+
.sm(source_map.map(|sm| sm as _))
169+
.short_message(short)
170+
.diagnostic_width(diagnostic_width)
171+
.track_diagnostics(unstable_opts.track_diagnostics)
172+
.theme(OutputTheme::Ascii)
173+
.ui_testing(unstable_opts.ui_testing),
174+
),
175+
},
180176
ErrorOutputType::Json { pretty, json_rendered, color_config } => {
181177
let source_map = source_map.unwrap_or_else(|| {
182178
Arc::new(source_map::SourceMap::new(source_map::FilePathMapping::empty()))

src/librustdoc/doctest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ fn run_test(
599599
]);
600600
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
601601
let short = kind.short();
602-
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true };
602+
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true, short };
603603

604604
if short {
605605
compiler_args.extend_from_slice(&["--error-format".to_owned(), "short".to_owned()]);

0 commit comments

Comments
 (0)