Skip to content

Commit

Permalink
Auto merge of #95296 - workingjubilee:pretty-session, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Prettify rustc_session with recent conveniences

No functional changes.

I felt like making something beautiful.
  • Loading branch information
bors committed Mar 26, 2022
2 parents d4acac9 + fd2448b commit 2882c20
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 120 deletions.
14 changes: 6 additions & 8 deletions compiler/rustc_session/src/cgu_reuse_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl CguReuseTracker {

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

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

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

data.expected_reuse.insert(
Expand All @@ -100,17 +100,15 @@ impl CguReuseTracker {
if error {
let at_least = if at_least { "at least " } else { "" };
let msg = format!(
"CGU-reuse for `{}` is `{:?}` but \
should be {}`{:?}`",
cgu_user_name, actual_reuse, at_least, expected_reuse
"CGU-reuse for `{cgu_user_name}` is `{actual_reuse:?}` but \
should be {at_least}`{expected_reuse:?}`"
);
diag.span_err(error_span.0, &msg);
}
} else {
let msg = format!(
"CGU-reuse for `{}` (mangled: `{}`) was \
not recorded",
cgu_user_name, cgu_name
"CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \
not recorded"
);
diag.span_fatal(error_span.0, &msg)
}
Expand Down
54 changes: 21 additions & 33 deletions compiler/rustc_session/src/code_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ impl CodeStats {
}
});

for info in &sorted {
for info in sorted {
let TypeSizeInfo { type_description, overall_size, align, kind, variants, .. } = info;
println!(
"print-type-size type: `{}`: {} bytes, alignment: {} bytes",
info.type_description, info.overall_size, info.align
"print-type-size type: `{type_description}`: {overall_size} bytes, alignment: {align} bytes"
);
let indent = " ";

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

let struct_like = match info.kind {
let struct_like = match kind {
DataTypeKind::Struct | DataTypeKind::Closure => true,
DataTypeKind::Enum | DataTypeKind::Union => false,
};
for (i, variant_info) in info.variants.iter().enumerate() {
for (i, variant_info) in variants.into_iter().enumerate() {
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
let indent = if !struct_like {
let name = match name.as_ref() {
Some(name) => name.to_owned(),
None => i.to_string(),
};
println!(
"print-type-size {}variant `{}`: {} bytes",
indent,
name,
size - discr_size
"print-type-size {indent}variant `{name}`: {diff} bytes",
diff = size - discr_size
);
" "
} else {
Expand All @@ -144,49 +142,39 @@ impl CodeStats {
let mut fields = fields.clone();
fields.sort_by_key(|f| (f.offset, f.size));

for field in fields.iter() {
let FieldInfo { ref name, offset, size, align } = *field;
for field in fields {
let FieldInfo { ref name, offset, size, align } = field;

if offset > min_offset {
let pad = offset - min_offset;
println!("print-type-size {}padding: {} bytes", indent, pad);
println!("print-type-size {indent}padding: {pad} bytes");
}

if offset < min_offset {
// If this happens it's probably a union.
println!(
"print-type-size {}field `.{}`: {} bytes, \
offset: {} bytes, \
alignment: {} bytes",
indent, name, size, offset, align
"print-type-size {indent}field `.{name}`: {size} bytes, \
offset: {offset} bytes, \
alignment: {align} bytes"
);
} else if info.packed || offset == min_offset {
println!("print-type-size {}field `.{}`: {} bytes", indent, name, size);
println!("print-type-size {indent}field `.{name}`: {size} bytes");
} else {
// Include field alignment in output only if it caused padding injection
println!(
"print-type-size {}field `.{}`: {} bytes, \
alignment: {} bytes",
indent, name, size, align
"print-type-size {indent}field `.{name}`: {size} bytes, \
alignment: {align} bytes"
);
}

min_offset = offset + size;
}
}

assert!(
max_variant_size <= info.overall_size,
"max_variant_size {} !<= {} overall_size",
max_variant_size,
info.overall_size
);
if max_variant_size < info.overall_size {
println!(
"print-type-size {}end padding: {} bytes",
indent,
info.overall_size - max_variant_size
);
match overall_size.checked_sub(max_variant_size) {
None => panic!("max_variant_size {max_variant_size} > {overall_size} overall_size"),
Some(diff @ 1..) => println!("print-type-size {indent}end padding: {diff} bytes"),
Some(0) => {}
}
}
}
Expand Down
72 changes: 30 additions & 42 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ impl OutputFilenames {
single_output_file,
temps_directory,
outputs,
filestem: format!("{}{}", out_filestem, extra),
filestem: format!("{out_filestem}{extra}"),
}
}

Expand Down Expand Up @@ -1514,7 +1514,7 @@ pub fn get_cmd_lint_options(

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

(lint_opts, describe_lints, lint_cap)
Expand All @@ -1533,8 +1533,7 @@ pub fn parse_color(matches: &getopts::Matches) -> ColorConfig {
ErrorOutputType::default(),
&format!(
"argument for `--color` must be auto, \
always or never (instead was `{}`)",
arg
always or never (instead was `{arg}`)"
),
),
}
Expand Down Expand Up @@ -1579,7 +1578,7 @@ pub fn parse_json(matches: &getopts::Matches) -> JsonConfig {
"future-incompat" => json_future_incompat = true,
s => early_error(
ErrorOutputType::default(),
&format!("unknown `--json` option `{}`", s),
&format!("unknown `--json` option `{s}`"),
),
}
}
Expand Down Expand Up @@ -1619,8 +1618,7 @@ pub fn parse_error_format(
ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(color)),
&format!(
"argument for `--error-format` must be `human`, `json` or \
`short` (instead was `{}`)",
arg
`short` (instead was `{arg}`)"
),
),
}
Expand Down Expand Up @@ -1654,8 +1652,7 @@ pub fn parse_crate_edition(matches: &getopts::Matches) -> Edition {
ErrorOutputType::default(),
&format!(
"argument for `--edition` must be one of: \
{}. (instead was `{}`)",
EDITION_NAME_LIST, arg
{EDITION_NAME_LIST}. (instead was `{arg}`)"
),
)
}),
Expand All @@ -1670,7 +1667,7 @@ pub fn parse_crate_edition(matches: &getopts::Matches) -> Edition {
edition, LATEST_STABLE_EDITION
)
} else {
format!("edition {} is unstable and only available with -Z unstable-options", edition)
format!("edition {edition} is unstable and only available with -Z unstable-options")
};
early_error(ErrorOutputType::default(), &msg)
}
Expand Down Expand Up @@ -1718,9 +1715,8 @@ fn parse_output_types(
early_error(
error_format,
&format!(
"unknown emission type: `{}` - expected one of: {}",
shorthand,
OutputType::shorthands_display(),
"unknown emission type: `{shorthand}` - expected one of: {display}",
display = OutputType::shorthands_display(),
),
)
});
Expand Down Expand Up @@ -1758,9 +1754,8 @@ fn should_override_cgus_and_disable_thinlto(
early_warn(
error_format,
&format!(
"`--emit={}` with `-o` incompatible with \
"`--emit={ot}` with `-o` incompatible with \
`-C codegen-units=N` for N > 1",
ot
),
);
}
Expand Down Expand Up @@ -1835,7 +1830,7 @@ fn collect_print_requests(
}
}
"link-args" => PrintRequest::LinkArgs,
req => early_error(error_format, &format!("unknown print request `{}`", req)),
req => early_error(error_format, &format!("unknown print request `{req}`")),
}));

prints
Expand All @@ -1849,7 +1844,7 @@ pub fn parse_target_triple(
Some(target) if target.ends_with(".json") => {
let path = Path::new(&target);
TargetTriple::from_path(&path).unwrap_or_else(|_| {
early_error(error_format, &format!("target file {:?} does not exist", path))
early_error(error_format, &format!("target file {path:?} does not exist"))
})
}
Some(target) => TargetTriple::TargetTriple(target),
Expand Down Expand Up @@ -1892,8 +1887,7 @@ fn parse_opt_level(
error_format,
&format!(
"optimization level needs to be \
between 0-3, s or z (instead was `{}`)",
arg
between 0-3, s or z (instead was `{arg}`)"
),
);
}
Expand Down Expand Up @@ -1927,8 +1921,7 @@ fn select_debuginfo(
error_format,
&format!(
"debug info level needs to be between \
0-2 (instead was `{}`)",
arg
0-2 (instead was `{arg}`)"
),
);
}
Expand All @@ -1943,10 +1936,9 @@ crate fn parse_assert_incr_state(
match opt_assertion {
Some(s) if s.as_str() == "loaded" => Some(IncrementalStateAssertion::Loaded),
Some(s) if s.as_str() == "not-loaded" => Some(IncrementalStateAssertion::NotLoaded),
Some(s) => early_error(
error_format,
&format!("unexpected incremental state assertion value: {}", s),
),
Some(s) => {
early_error(error_format, &format!("unexpected incremental state assertion value: {s}"))
}
None => None,
}
}
Expand Down Expand Up @@ -1991,7 +1983,7 @@ fn parse_native_lib_kind(
}
s => early_error(
error_format,
&format!("unknown library kind `{}`, expected one of dylib, framework, or static", s),
&format!("unknown library kind `{s}`, expected one of dylib, framework, or static"),
),
};
match modifiers {
Expand Down Expand Up @@ -2066,9 +2058,8 @@ fn parse_native_lib_modifiers(
_ => early_error(
error_format,
&format!(
"unrecognized linking modifier `{}`, expected one \
of: bundle, verbatim, whole-archive, as-needed",
modifier
"unrecognized linking modifier `{modifier}`, expected one \
of: bundle, verbatim, whole-archive, as-needed"
),
),
}
Expand Down Expand Up @@ -2109,7 +2100,7 @@ fn parse_borrowck_mode(dopts: &DebuggingOptions, error_format: ErrorOutputType)
match dopts.borrowck.as_ref() {
"migrate" => BorrowckMode::Migrate,
"mir" => BorrowckMode::Mir,
m => early_error(error_format, &format!("unknown borrowck mode `{}`", m)),
m => early_error(error_format, &format!("unknown borrowck mode `{m}`")),
}
}

Expand Down Expand Up @@ -2197,7 +2188,7 @@ pub fn parse_externs(
);
}
}
_ => early_error(error_format, &format!("unknown --extern option `{}`", opt)),
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
}
}
}
Expand Down Expand Up @@ -2234,7 +2225,7 @@ fn parse_extern_dep_specs(
let loc = parts.next().unwrap_or_else(|| {
early_error(
error_format,
&format!("`--extern-location`: specify location for extern crate `{}`", name),
&format!("`--extern-location`: specify location for extern crate `{name}`"),
)
});

Expand All @@ -2255,14 +2246,14 @@ fn parse_extern_dep_specs(
let json = json::from_str(raw).unwrap_or_else(|_| {
early_error(
error_format,
&format!("`--extern-location`: malformed json location `{}`", raw),
&format!("`--extern-location`: malformed json location `{raw}`"),
)
});
ExternDepSpec::Json(json)
}
[bad, ..] => early_error(
error_format,
&format!("unknown location type `{}`: use `raw` or `json`", bad),
&format!("unknown location type `{bad}`: use `raw` or `json`"),
),
[] => early_error(error_format, "missing location specification"),
};
Expand Down Expand Up @@ -2527,9 +2518,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
&& !target_triple.triple().contains("apple")
&& cg.split_debuginfo.is_some()
{
{
early_error(error_format, "`-Csplit-debuginfo` is unstable on this platform");
}
early_error(error_format, "`-Csplit-debuginfo` is unstable on this platform");
}

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

let working_dir = std::env::current_dir().unwrap_or_else(|e| {
early_error(error_format, &format!("Current directory is invalid: {}", e));
early_error(error_format, &format!("Current directory is invalid: {e}"));
});

let (path, remapped) =
Expand Down Expand Up @@ -2636,12 +2625,11 @@ fn parse_pretty(debugging_opts: &DebuggingOptions, efmt: ErrorOutputType) -> Opt
"argument to `unpretty` must be one of `normal`, `identified`, \
`expanded`, `expanded,identified`, `expanded,hygiene`, \
`ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \
`hir,typed`, `hir-tree`, `thir-tree`, `mir` or `mir-cfg`; got {}",
name
`hir,typed`, `hir-tree`, `thir-tree`, `mir` or `mir-cfg`; got {name}"
),
),
};
tracing::debug!("got unpretty option: {:?}", first);
tracing::debug!("got unpretty option: {first:?}");
Some(first)
}

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

0 comments on commit 2882c20

Please sign in to comment.