Skip to content

Handle build scripts better in -Zmacro-stats output. #143228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ fn configure_and_expand(
fn print_macro_stats(ecx: &ExtCtxt<'_>) {
use std::fmt::Write;

let crate_name = ecx.ecfg.crate_name.as_str();
let crate_name = if crate_name == "build_script_build" {
// This is a build script. Get the package name from the environment.
let pkg_name =
std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "<unknown crate>".to_string());
format!("{pkg_name} build script")
} else {
crate_name.to_string()
};

// No instability because we immediately sort the produced vector.
#[allow(rustc::potential_query_instability)]
let mut macro_stats: Vec<_> = ecx
Expand Down Expand Up @@ -327,7 +337,7 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
// non-interleaving, though.
let mut s = String::new();
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
_ = writeln!(s, "{prefix} MACRO EXPANSION STATS: {}", ecx.ecfg.crate_name);
_ = writeln!(s, "{prefix} MACRO EXPANSION STATS: {}", crate_name);
_ = writeln!(
s,
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
Expand Down
Loading