Skip to content

Commit 774976a

Browse files
authored
Rollup merge of #138111 - estebank:use-dfv, r=nnethercote
Use `default_field_values` for `rustc_errors::Context`, `rustc_session::config::NextSolverConfig` and `rustc_session::config::ErrorOutputType` Wanted to see where `#![feature(default_field_values)]` could be used in the codebase. These three seemed like no-brainers. There are a bunch of more places where we could remove manual `Default` impls, but they `derive` other traits that rely on `syn`, which [doesn't yet support `default_field_values`](dtolnay/syn#1774).
2 parents d207e21 + 0c4eaa5 commit 774976a

File tree

7 files changed

+37
-49
lines changed

7 files changed

+37
-49
lines changed

compiler/rustc_errors/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(associated_type_defaults)]
1515
#![feature(box_into_inner)]
1616
#![feature(box_patterns)]
17+
#![feature(default_field_values)]
1718
#![feature(error_reporter)]
1819
#![feature(if_let_guard)]
1920
#![feature(let_chains)]

compiler/rustc_errors/src/markdown/parse.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ type ParseResult<'a> = Option<Parsed<'a>>;
4040

4141
/// Parsing context
4242
#[derive(Clone, Copy, Debug, PartialEq)]
43+
// The default values are the most common setting for non top-level parsing: not top block, not at
44+
// line start (yes leading whitespace, not escaped).
4345
struct Context {
4446
/// If true, we are at a the topmost level (not recursing a nested tt)
45-
top_block: bool,
47+
top_block: bool = false,
4648
/// Previous character
47-
prev: Prev,
49+
prev: Prev = Prev::Whitespace,
4850
}
4951

5052
/// Character class preceding this one
@@ -57,14 +59,6 @@ enum Prev {
5759
Any,
5860
}
5961

60-
impl Default for Context {
61-
/// Most common setting for non top-level parsing: not top block, not at
62-
/// line start (yes leading whitespace, not escaped)
63-
fn default() -> Self {
64-
Self { top_block: false, prev: Prev::Whitespace }
65-
}
66-
}
67-
6862
/// Flags to simple parser function
6963
#[derive(Clone, Copy, Debug, PartialEq)]
7064
enum ParseOpt {
@@ -248,7 +242,7 @@ fn parse_heading(buf: &[u8]) -> ParseResult<'_> {
248242
}
249243

250244
let (txt, rest) = parse_to_newline(&buf[1..]);
251-
let ctx = Context { top_block: false, prev: Prev::Whitespace };
245+
let ctx = Context { .. };
252246
let stream = parse_recursive(txt, ctx);
253247

254248
Some((MdTree::Heading(level.try_into().unwrap(), stream), rest))
@@ -257,7 +251,7 @@ fn parse_heading(buf: &[u8]) -> ParseResult<'_> {
257251
/// Bulleted list
258252
fn parse_unordered_li(buf: &[u8]) -> Parsed<'_> {
259253
let (txt, rest) = get_indented_section(&buf[2..]);
260-
let ctx = Context { top_block: false, prev: Prev::Whitespace };
254+
let ctx = Context { .. };
261255
let stream = parse_recursive(trim_ascii_start(txt), ctx);
262256
(MdTree::UnorderedListItem(stream), rest)
263257
}
@@ -266,7 +260,7 @@ fn parse_unordered_li(buf: &[u8]) -> Parsed<'_> {
266260
fn parse_ordered_li(buf: &[u8]) -> Parsed<'_> {
267261
let (num, pos) = ord_list_start(buf).unwrap(); // success tested in caller
268262
let (txt, rest) = get_indented_section(&buf[pos..]);
269-
let ctx = Context { top_block: false, prev: Prev::Whitespace };
263+
let ctx = Context { .. };
270264
let stream = parse_recursive(trim_ascii_start(txt), ctx);
271265
(MdTree::OrderedListItem(num, stream), rest)
272266
}

compiler/rustc_session/src/config.rs

+24-32
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,14 @@ impl OutputType {
681681
}
682682

683683
/// The type of diagnostics output to generate.
684-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
684+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
685685
pub enum ErrorOutputType {
686686
/// Output meant for the consumption of humans.
687-
HumanReadable(HumanReadableErrorType, ColorConfig),
687+
#[default]
688+
HumanReadable {
689+
kind: HumanReadableErrorType = HumanReadableErrorType::Default,
690+
color_config: ColorConfig = ColorConfig::Auto,
691+
},
688692
/// Output that's consumed by other tools such as `rustfix` or the `RLS`.
689693
Json {
690694
/// Render the JSON in a human readable way (with indents and newlines).
@@ -696,12 +700,6 @@ pub enum ErrorOutputType {
696700
},
697701
}
698702

699-
impl Default for ErrorOutputType {
700-
fn default() -> Self {
701-
Self::HumanReadable(HumanReadableErrorType::Default, ColorConfig::Auto)
702-
}
703-
}
704-
705703
#[derive(Clone, Hash, Debug)]
706704
pub enum ResolveDocLinks {
707705
/// Do not resolve doc links.
@@ -898,18 +896,13 @@ pub enum PrintKind {
898896
DeploymentTarget,
899897
}
900898

901-
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
899+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Default)]
902900
pub struct NextSolverConfig {
903901
/// Whether the new trait solver should be enabled in coherence.
904-
pub coherence: bool,
902+
pub coherence: bool = true,
905903
/// Whether the new trait solver should be enabled everywhere.
906904
/// This is only `true` if `coherence` is also enabled.
907-
pub globally: bool,
908-
}
909-
impl Default for NextSolverConfig {
910-
fn default() -> Self {
911-
NextSolverConfig { coherence: true, globally: false }
912-
}
905+
pub globally: bool = false,
913906
}
914907

915908
#[derive(Clone)]
@@ -1825,7 +1818,7 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
18251818
pub fn parse_error_format(
18261819
early_dcx: &mut EarlyDiagCtxt,
18271820
matches: &getopts::Matches,
1828-
color: ColorConfig,
1821+
color_config: ColorConfig,
18291822
json_color: ColorConfig,
18301823
json_rendered: HumanReadableErrorType,
18311824
) -> ErrorOutputType {
@@ -1835,35 +1828,34 @@ pub fn parse_error_format(
18351828
// `opt_present` because the latter will panic.
18361829
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
18371830
match matches.opt_str("error-format").as_deref() {
1838-
None | Some("human") => {
1839-
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color)
1840-
}
1841-
Some("human-annotate-rs") => {
1842-
ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet, color)
1843-
}
1831+
None | Some("human") => ErrorOutputType::HumanReadable { color_config, .. },
1832+
Some("human-annotate-rs") => ErrorOutputType::HumanReadable {
1833+
kind: HumanReadableErrorType::AnnotateSnippet,
1834+
color_config,
1835+
},
18441836
Some("json") => {
18451837
ErrorOutputType::Json { pretty: false, json_rendered, color_config: json_color }
18461838
}
18471839
Some("pretty-json") => {
18481840
ErrorOutputType::Json { pretty: true, json_rendered, color_config: json_color }
18491841
}
1850-
Some("short") => ErrorOutputType::HumanReadable(HumanReadableErrorType::Short, color),
1851-
Some("human-unicode") => {
1852-
ErrorOutputType::HumanReadable(HumanReadableErrorType::Unicode, color)
1842+
Some("short") => {
1843+
ErrorOutputType::HumanReadable { kind: HumanReadableErrorType::Short, color_config }
18531844
}
1845+
Some("human-unicode") => ErrorOutputType::HumanReadable {
1846+
kind: HumanReadableErrorType::Unicode,
1847+
color_config,
1848+
},
18541849
Some(arg) => {
1855-
early_dcx.set_error_format(ErrorOutputType::HumanReadable(
1856-
HumanReadableErrorType::Default,
1857-
color,
1858-
));
1850+
early_dcx.set_error_format(ErrorOutputType::HumanReadable { color_config, .. });
18591851
early_dcx.early_fatal(format!(
18601852
"argument for `--error-format` must be `human`, `human-annotate-rs`, \
18611853
`human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)"
18621854
))
18631855
}
18641856
}
18651857
} else {
1866-
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default, color)
1858+
ErrorOutputType::HumanReadable { color_config, .. }
18671859
};
18681860

18691861
match error_format {
@@ -1918,7 +1910,7 @@ fn check_error_format_stability(
19181910
}
19191911
let format = match format {
19201912
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
1921-
ErrorOutputType::HumanReadable(format, _) => match format {
1913+
ErrorOutputType::HumanReadable { kind, .. } => match kind {
19221914
HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs",
19231915
HumanReadableErrorType::Unicode => "human-unicode",
19241916
_ => return,

compiler/rustc_session/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// tidy-alphabetical-start
22
#![allow(internal_features)]
3+
#![feature(default_field_values)]
34
#![feature(iter_intersperse)]
45
#![feature(let_chains)]
56
#![feature(rustc_attrs)]

compiler/rustc_session/src/session.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ fn default_emitter(
913913
let source_map = if sopts.unstable_opts.link_only { None } else { Some(source_map) };
914914

915915
match sopts.error_format {
916-
config::ErrorOutputType::HumanReadable(kind, color_config) => {
916+
config::ErrorOutputType::HumanReadable { kind, color_config } => {
917917
let short = kind.short();
918918

919919
if let HumanReadableErrorType::AnnotateSnippet = kind {
@@ -1430,7 +1430,7 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
14301430
let fallback_bundle =
14311431
fallback_fluent_bundle(vec![rustc_errors::DEFAULT_LOCALE_RESOURCE], false);
14321432
let emitter: Box<DynEmitter> = match output {
1433-
config::ErrorOutputType::HumanReadable(kind, color_config) => {
1433+
config::ErrorOutputType::HumanReadable { kind, color_config } => {
14341434
let short = kind.short();
14351435
Box::new(
14361436
HumanEmitter::new(stderr_destination(color_config), fallback_bundle)

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub(crate) fn new_dcx(
154154
false,
155155
);
156156
let emitter: Box<DynEmitter> = match error_format {
157-
ErrorOutputType::HumanReadable(kind, color_config) => {
157+
ErrorOutputType::HumanReadable { kind, color_config } => {
158158
let short = kind.short();
159159
Box::new(
160160
HumanEmitter::new(stderr_destination(color_config), fallback_bundle)

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ fn run_test(
580580
path_for_rustdoc.to_str().expect("target path must be valid unicode")
581581
}
582582
});
583-
if let ErrorOutputType::HumanReadable(kind, color_config) = rustdoc_options.error_format {
583+
if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
584584
let short = kind.short();
585585
let unicode = kind == HumanReadableErrorType::Unicode;
586586

0 commit comments

Comments
 (0)