Skip to content

Commit 2882c20

Browse files
committed
Auto merge of rust-lang#95296 - workingjubilee:pretty-session, r=Dylan-DPC
Prettify rustc_session with recent conveniences No functional changes. I felt like making something beautiful.
2 parents d4acac9 + fd2448b commit 2882c20

File tree

9 files changed

+85
-120
lines changed

9 files changed

+85
-120
lines changed

compiler/rustc_session/src/cgu_reuse_tracker.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl CguReuseTracker {
5252

5353
pub fn set_actual_reuse(&self, cgu_name: &str, kind: CguReuse) {
5454
if let Some(ref data) = self.data {
55-
debug!("set_actual_reuse({:?}, {:?})", cgu_name, kind);
55+
debug!("set_actual_reuse({cgu_name:?}, {kind:?})");
5656

5757
let prev_reuse = data.lock().unwrap().actual_reuse.insert(cgu_name.to_string(), kind);
5858

@@ -74,7 +74,7 @@ impl CguReuseTracker {
7474
comparison_kind: ComparisonKind,
7575
) {
7676
if let Some(ref data) = self.data {
77-
debug!("set_expectation({:?}, {:?}, {:?})", cgu_name, expected_reuse, comparison_kind);
77+
debug!("set_expectation({cgu_name:?}, {expected_reuse:?}, {comparison_kind:?})");
7878
let mut data = data.lock().unwrap();
7979

8080
data.expected_reuse.insert(
@@ -100,17 +100,15 @@ impl CguReuseTracker {
100100
if error {
101101
let at_least = if at_least { "at least " } else { "" };
102102
let msg = format!(
103-
"CGU-reuse for `{}` is `{:?}` but \
104-
should be {}`{:?}`",
105-
cgu_user_name, actual_reuse, at_least, expected_reuse
103+
"CGU-reuse for `{cgu_user_name}` is `{actual_reuse:?}` but \
104+
should be {at_least}`{expected_reuse:?}`"
106105
);
107106
diag.span_err(error_span.0, &msg);
108107
}
109108
} else {
110109
let msg = format!(
111-
"CGU-reuse for `{}` (mangled: `{}`) was \
112-
not recorded",
113-
cgu_user_name, cgu_name
110+
"CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \
111+
not recorded"
114112
);
115113
diag.span_fatal(error_span.0, &msg)
116114
}

compiler/rustc_session/src/code_stats.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ impl CodeStats {
9191
}
9292
});
9393

94-
for info in &sorted {
94+
for info in sorted {
95+
let TypeSizeInfo { type_description, overall_size, align, kind, variants, .. } = info;
9596
println!(
96-
"print-type-size type: `{}`: {} bytes, alignment: {} bytes",
97-
info.type_description, info.overall_size, info.align
97+
"print-type-size type: `{type_description}`: {overall_size} bytes, alignment: {align} bytes"
9898
);
9999
let indent = " ";
100100

101101
let discr_size = if let Some(discr_size) = info.opt_discr_size {
102-
println!("print-type-size {}discriminant: {} bytes", indent, discr_size);
102+
println!("print-type-size {indent}discriminant: {discr_size} bytes");
103103
discr_size
104104
} else {
105105
0
@@ -111,22 +111,20 @@ impl CodeStats {
111111
// to reflect the presence of the discriminant.
112112
let mut max_variant_size = discr_size;
113113

114-
let struct_like = match info.kind {
114+
let struct_like = match kind {
115115
DataTypeKind::Struct | DataTypeKind::Closure => true,
116116
DataTypeKind::Enum | DataTypeKind::Union => false,
117117
};
118-
for (i, variant_info) in info.variants.iter().enumerate() {
118+
for (i, variant_info) in variants.into_iter().enumerate() {
119119
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
120120
let indent = if !struct_like {
121121
let name = match name.as_ref() {
122122
Some(name) => name.to_owned(),
123123
None => i.to_string(),
124124
};
125125
println!(
126-
"print-type-size {}variant `{}`: {} bytes",
127-
indent,
128-
name,
129-
size - discr_size
126+
"print-type-size {indent}variant `{name}`: {diff} bytes",
127+
diff = size - discr_size
130128
);
131129
" "
132130
} else {
@@ -144,49 +142,39 @@ impl CodeStats {
144142
let mut fields = fields.clone();
145143
fields.sort_by_key(|f| (f.offset, f.size));
146144

147-
for field in fields.iter() {
148-
let FieldInfo { ref name, offset, size, align } = *field;
145+
for field in fields {
146+
let FieldInfo { ref name, offset, size, align } = field;
149147

150148
if offset > min_offset {
151149
let pad = offset - min_offset;
152-
println!("print-type-size {}padding: {} bytes", indent, pad);
150+
println!("print-type-size {indent}padding: {pad} bytes");
153151
}
154152

155153
if offset < min_offset {
156154
// If this happens it's probably a union.
157155
println!(
158-
"print-type-size {}field `.{}`: {} bytes, \
159-
offset: {} bytes, \
160-
alignment: {} bytes",
161-
indent, name, size, offset, align
156+
"print-type-size {indent}field `.{name}`: {size} bytes, \
157+
offset: {offset} bytes, \
158+
alignment: {align} bytes"
162159
);
163160
} else if info.packed || offset == min_offset {
164-
println!("print-type-size {}field `.{}`: {} bytes", indent, name, size);
161+
println!("print-type-size {indent}field `.{name}`: {size} bytes");
165162
} else {
166163
// Include field alignment in output only if it caused padding injection
167164
println!(
168-
"print-type-size {}field `.{}`: {} bytes, \
169-
alignment: {} bytes",
170-
indent, name, size, align
165+
"print-type-size {indent}field `.{name}`: {size} bytes, \
166+
alignment: {align} bytes"
171167
);
172168
}
173169

174170
min_offset = offset + size;
175171
}
176172
}
177173

178-
assert!(
179-
max_variant_size <= info.overall_size,
180-
"max_variant_size {} !<= {} overall_size",
181-
max_variant_size,
182-
info.overall_size
183-
);
184-
if max_variant_size < info.overall_size {
185-
println!(
186-
"print-type-size {}end padding: {} bytes",
187-
indent,
188-
info.overall_size - max_variant_size
189-
);
174+
match overall_size.checked_sub(max_variant_size) {
175+
None => panic!("max_variant_size {max_variant_size} > {overall_size} overall_size"),
176+
Some(diff @ 1..) => println!("print-type-size {indent}end padding: {diff} bytes"),
177+
Some(0) => {}
190178
}
191179
}
192180
}

compiler/rustc_session/src/config.rs

+30-42
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl OutputFilenames {
659659
single_output_file,
660660
temps_directory,
661661
outputs,
662-
filestem: format!("{}{}", out_filestem, extra),
662+
filestem: format!("{out_filestem}{extra}"),
663663
}
664664
}
665665

@@ -1514,7 +1514,7 @@ pub fn get_cmd_lint_options(
15141514

15151515
let lint_cap = matches.opt_str("cap-lints").map(|cap| {
15161516
lint::Level::from_str(&cap)
1517-
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{}`", cap)))
1517+
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{cap}`")))
15181518
});
15191519

15201520
(lint_opts, describe_lints, lint_cap)
@@ -1533,8 +1533,7 @@ pub fn parse_color(matches: &getopts::Matches) -> ColorConfig {
15331533
ErrorOutputType::default(),
15341534
&format!(
15351535
"argument for `--color` must be auto, \
1536-
always or never (instead was `{}`)",
1537-
arg
1536+
always or never (instead was `{arg}`)"
15381537
),
15391538
),
15401539
}
@@ -1579,7 +1578,7 @@ pub fn parse_json(matches: &getopts::Matches) -> JsonConfig {
15791578
"future-incompat" => json_future_incompat = true,
15801579
s => early_error(
15811580
ErrorOutputType::default(),
1582-
&format!("unknown `--json` option `{}`", s),
1581+
&format!("unknown `--json` option `{s}`"),
15831582
),
15841583
}
15851584
}
@@ -1619,8 +1618,7 @@ pub fn parse_error_format(
16191618
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color)),
16201619
&format!(
16211620
"argument for `--error-format` must be `human`, `json` or \
1622-
`short` (instead was `{}`)",
1623-
arg
1621+
`short` (instead was `{arg}`)"
16241622
),
16251623
),
16261624
}
@@ -1654,8 +1652,7 @@ pub fn parse_crate_edition(matches: &getopts::Matches) -> Edition {
16541652
ErrorOutputType::default(),
16551653
&format!(
16561654
"argument for `--edition` must be one of: \
1657-
{}. (instead was `{}`)",
1658-
EDITION_NAME_LIST, arg
1655+
{EDITION_NAME_LIST}. (instead was `{arg}`)"
16591656
),
16601657
)
16611658
}),
@@ -1670,7 +1667,7 @@ pub fn parse_crate_edition(matches: &getopts::Matches) -> Edition {
16701667
edition, LATEST_STABLE_EDITION
16711668
)
16721669
} else {
1673-
format!("edition {} is unstable and only available with -Z unstable-options", edition)
1670+
format!("edition {edition} is unstable and only available with -Z unstable-options")
16741671
};
16751672
early_error(ErrorOutputType::default(), &msg)
16761673
}
@@ -1718,9 +1715,8 @@ fn parse_output_types(
17181715
early_error(
17191716
error_format,
17201717
&format!(
1721-
"unknown emission type: `{}` - expected one of: {}",
1722-
shorthand,
1723-
OutputType::shorthands_display(),
1718+
"unknown emission type: `{shorthand}` - expected one of: {display}",
1719+
display = OutputType::shorthands_display(),
17241720
),
17251721
)
17261722
});
@@ -1758,9 +1754,8 @@ fn should_override_cgus_and_disable_thinlto(
17581754
early_warn(
17591755
error_format,
17601756
&format!(
1761-
"`--emit={}` with `-o` incompatible with \
1757+
"`--emit={ot}` with `-o` incompatible with \
17621758
`-C codegen-units=N` for N > 1",
1763-
ot
17641759
),
17651760
);
17661761
}
@@ -1835,7 +1830,7 @@ fn collect_print_requests(
18351830
}
18361831
}
18371832
"link-args" => PrintRequest::LinkArgs,
1838-
req => early_error(error_format, &format!("unknown print request `{}`", req)),
1833+
req => early_error(error_format, &format!("unknown print request `{req}`")),
18391834
}));
18401835

18411836
prints
@@ -1849,7 +1844,7 @@ pub fn parse_target_triple(
18491844
Some(target) if target.ends_with(".json") => {
18501845
let path = Path::new(&target);
18511846
TargetTriple::from_path(&path).unwrap_or_else(|_| {
1852-
early_error(error_format, &format!("target file {:?} does not exist", path))
1847+
early_error(error_format, &format!("target file {path:?} does not exist"))
18531848
})
18541849
}
18551850
Some(target) => TargetTriple::TargetTriple(target),
@@ -1892,8 +1887,7 @@ fn parse_opt_level(
18921887
error_format,
18931888
&format!(
18941889
"optimization level needs to be \
1895-
between 0-3, s or z (instead was `{}`)",
1896-
arg
1890+
between 0-3, s or z (instead was `{arg}`)"
18971891
),
18981892
);
18991893
}
@@ -1927,8 +1921,7 @@ fn select_debuginfo(
19271921
error_format,
19281922
&format!(
19291923
"debug info level needs to be between \
1930-
0-2 (instead was `{}`)",
1931-
arg
1924+
0-2 (instead was `{arg}`)"
19321925
),
19331926
);
19341927
}
@@ -1943,10 +1936,9 @@ crate fn parse_assert_incr_state(
19431936
match opt_assertion {
19441937
Some(s) if s.as_str() == "loaded" => Some(IncrementalStateAssertion::Loaded),
19451938
Some(s) if s.as_str() == "not-loaded" => Some(IncrementalStateAssertion::NotLoaded),
1946-
Some(s) => early_error(
1947-
error_format,
1948-
&format!("unexpected incremental state assertion value: {}", s),
1949-
),
1939+
Some(s) => {
1940+
early_error(error_format, &format!("unexpected incremental state assertion value: {s}"))
1941+
}
19501942
None => None,
19511943
}
19521944
}
@@ -1991,7 +1983,7 @@ fn parse_native_lib_kind(
19911983
}
19921984
s => early_error(
19931985
error_format,
1994-
&format!("unknown library kind `{}`, expected one of dylib, framework, or static", s),
1986+
&format!("unknown library kind `{s}`, expected one of dylib, framework, or static"),
19951987
),
19961988
};
19971989
match modifiers {
@@ -2066,9 +2058,8 @@ fn parse_native_lib_modifiers(
20662058
_ => early_error(
20672059
error_format,
20682060
&format!(
2069-
"unrecognized linking modifier `{}`, expected one \
2070-
of: bundle, verbatim, whole-archive, as-needed",
2071-
modifier
2061+
"unrecognized linking modifier `{modifier}`, expected one \
2062+
of: bundle, verbatim, whole-archive, as-needed"
20722063
),
20732064
),
20742065
}
@@ -2109,7 +2100,7 @@ fn parse_borrowck_mode(dopts: &DebuggingOptions, error_format: ErrorOutputType)
21092100
match dopts.borrowck.as_ref() {
21102101
"migrate" => BorrowckMode::Migrate,
21112102
"mir" => BorrowckMode::Mir,
2112-
m => early_error(error_format, &format!("unknown borrowck mode `{}`", m)),
2103+
m => early_error(error_format, &format!("unknown borrowck mode `{m}`")),
21132104
}
21142105
}
21152106

@@ -2197,7 +2188,7 @@ pub fn parse_externs(
21972188
);
21982189
}
21992190
}
2200-
_ => early_error(error_format, &format!("unknown --extern option `{}`", opt)),
2191+
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
22012192
}
22022193
}
22032194
}
@@ -2234,7 +2225,7 @@ fn parse_extern_dep_specs(
22342225
let loc = parts.next().unwrap_or_else(|| {
22352226
early_error(
22362227
error_format,
2237-
&format!("`--extern-location`: specify location for extern crate `{}`", name),
2228+
&format!("`--extern-location`: specify location for extern crate `{name}`"),
22382229
)
22392230
});
22402231

@@ -2255,14 +2246,14 @@ fn parse_extern_dep_specs(
22552246
let json = json::from_str(raw).unwrap_or_else(|_| {
22562247
early_error(
22572248
error_format,
2258-
&format!("`--extern-location`: malformed json location `{}`", raw),
2249+
&format!("`--extern-location`: malformed json location `{raw}`"),
22592250
)
22602251
});
22612252
ExternDepSpec::Json(json)
22622253
}
22632254
[bad, ..] => early_error(
22642255
error_format,
2265-
&format!("unknown location type `{}`: use `raw` or `json`", bad),
2256+
&format!("unknown location type `{bad}`: use `raw` or `json`"),
22662257
),
22672258
[] => early_error(error_format, "missing location specification"),
22682259
};
@@ -2527,9 +2518,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
25272518
&& !target_triple.triple().contains("apple")
25282519
&& cg.split_debuginfo.is_some()
25292520
{
2530-
{
2531-
early_error(error_format, "`-Csplit-debuginfo` is unstable on this platform");
2532-
}
2521+
early_error(error_format, "`-Csplit-debuginfo` is unstable on this platform");
25332522
}
25342523

25352524
// Try to find a directory containing the Rust `src`, for more details see
@@ -2561,7 +2550,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
25612550
};
25622551

25632552
let working_dir = std::env::current_dir().unwrap_or_else(|e| {
2564-
early_error(error_format, &format!("Current directory is invalid: {}", e));
2553+
early_error(error_format, &format!("Current directory is invalid: {e}"));
25652554
});
25662555

25672556
let (path, remapped) =
@@ -2636,12 +2625,11 @@ fn parse_pretty(debugging_opts: &DebuggingOptions, efmt: ErrorOutputType) -> Opt
26362625
"argument to `unpretty` must be one of `normal`, `identified`, \
26372626
`expanded`, `expanded,identified`, `expanded,hygiene`, \
26382627
`ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \
2639-
`hir,typed`, `hir-tree`, `thir-tree`, `mir` or `mir-cfg`; got {}",
2640-
name
2628+
`hir,typed`, `hir-tree`, `thir-tree`, `mir` or `mir-cfg`; got {name}"
26412629
),
26422630
),
26432631
};
2644-
tracing::debug!("got unpretty option: {:?}", first);
2632+
tracing::debug!("got unpretty option: {first:?}");
26452633
Some(first)
26462634
}
26472635

@@ -2667,7 +2655,7 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
26672655
"cdylib" => CrateType::Cdylib,
26682656
"bin" => CrateType::Executable,
26692657
"proc-macro" => CrateType::ProcMacro,
2670-
_ => return Err(format!("unknown crate type: `{}`", part)),
2658+
_ => return Err(format!("unknown crate type: `{part}`")),
26712659
};
26722660
if !crate_types.contains(&new_part) {
26732661
crate_types.push(new_part)

0 commit comments

Comments
 (0)