Skip to content

Commit 9cb7deb

Browse files
committed
refactor: Make short a field on HumanReadableErrorType varinants
1 parent 4748d92 commit 9cb7deb

File tree

8 files changed

+92
-92
lines changed

8 files changed

+92
-92
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_errors/src/json/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
5050
Some(sm),
5151
translator,
5252
true, // pretty
53-
HumanReadableErrorType::Short,
53+
HumanReadableErrorType::Default { short: true },
5454
ColorConfig::Never,
5555
);
5656

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
@@ -826,7 +826,7 @@ pub enum ErrorOutputType {
826826
/// Output meant for the consumption of humans.
827827
#[default]
828828
HumanReadable {
829-
kind: HumanReadableErrorType = HumanReadableErrorType::Default,
829+
kind: HumanReadableErrorType = HumanReadableErrorType::Default { short: false },
830830
color_config: ColorConfig = ColorConfig::Auto,
831831
},
832832
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
@@ -2042,7 +2042,7 @@ impl JsonUnusedExterns {
20422042
/// The first value returned is how to render JSON diagnostics, and the second
20432043
/// is whether or not artifact notifications are enabled.
20442044
pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> JsonConfig {
2045-
let mut json_rendered = HumanReadableErrorType::Default;
2045+
let mut json_rendered = HumanReadableErrorType::Default { short: false };
20462046
let mut json_color = ColorConfig::Never;
20472047
let mut json_artifact_notifications = false;
20482048
let mut json_unused_externs = JsonUnusedExterns::No;
@@ -2058,9 +2058,12 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
20582058

20592059
for sub_option in option.split(',') {
20602060
match sub_option {
2061-
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
2061+
"diagnostic-short" => {
2062+
json_rendered = HumanReadableErrorType::Default { short: true }
2063+
}
20622064
"diagnostic-unicode" => {
2063-
json_rendered = HumanReadableErrorType::AnnotateSnippet { unicode: true };
2065+
json_rendered =
2066+
HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true };
20642067
}
20652068
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
20662069
"artifacts" => json_artifact_notifications = true,
@@ -2099,7 +2102,7 @@ pub fn parse_error_format(
20992102
match matches.opt_str("error-format").as_deref() {
21002103
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
21012104
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
2102-
kind: HumanReadableErrorType::AnnotateSnippet { unicode: false },
2105+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: false },
21032106
color_config,
21042107
},
21052108
Some("json") => {
@@ -2108,11 +2111,12 @@ pub fn parse_error_format(
21082111
Some("pretty-json") => {
21092112
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
21102113
}
2111-
Some("short") => {
2112-
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
2113-
}
2114+
Some("short") => ErrorOutputType::HumanReadable {
2115+
kind: HumanReadableErrorType::Default { short: true },
2116+
color_config,
2117+
},
21142118
Some("human-unicode") => ErrorOutputType::HumanReadable {
2115-
kind: HumanReadableErrorType::AnnotateSnippet { unicode: true },
2119+
kind: HumanReadableErrorType::AnnotateSnippet { short: false, unicode: true },
21162120
color_config,
21172121
},
21182122
Some(arg) => {
@@ -2180,8 +2184,8 @@ fn check_error_format_stability(
21802184
let format = match format {
21812185
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
21822186
ErrorOutputType::HumanReadable { kind, .. } => match kind {
2183-
HumanReadableErrorType::AnnotateSnippet { unicode: false } => "human-annotate-rs",
2184-
HumanReadableErrorType::AnnotateSnippet { unicode: true } => "human-unicode",
2187+
HumanReadableErrorType::AnnotateSnippet { unicode: false, .. } => "human-annotate-rs",
2188+
HumanReadableErrorType::AnnotateSnippet { unicode: true, .. } => "human-unicode",
21852189
_ => return,
21862190
},
21872191
_ => 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
@@ -600,7 +600,7 @@ fn run_test(
600600
]);
601601
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
602602
let short = kind.short();
603-
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true };
603+
let unicode = kind == HumanReadableErrorType::AnnotateSnippet { unicode: true, short };
604604

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

0 commit comments

Comments
 (0)