Skip to content

Plumb rustc -Zhint-mostly-unused flag through as a profile option #15643

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 3 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions crates/cargo-util-schemas/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,14 @@
}
],
"default": null
},
"hint-mostly-unused": {
"description": "Unstable feature `hint-mostly-unused`",
"type": [
"boolean",
"null"
],
"default": null
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,8 @@ pub struct TomlProfile {
pub build_override: Option<Box<TomlProfile>>,
/// Unstable feature `-Ztrim-paths`.
pub trim_paths: Option<TomlTrimPaths>,
/// Unstable feature `hint-mostly-unused`
pub hint_mostly_unused: Option<bool>,
}

impl TomlProfile {
Expand Down Expand Up @@ -998,6 +1000,10 @@ impl TomlProfile {
if let Some(v) = &profile.trim_paths {
self.trim_paths = Some(v.clone())
}

if let Some(v) = profile.hint_mostly_unused {
self.hint_mostly_unused = Some(v);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ fn build_base_args(
strip,
rustflags: profile_rustflags,
trim_paths,
hint_mostly_unused,
..
} = unit.profile.clone();
let test = unit.mode.is_any_test();
Expand Down Expand Up @@ -1325,6 +1326,16 @@ fn build_base_args(
opt(cmd, "-C", "incremental=", Some(dir));
}

if hint_mostly_unused {
if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
cmd.arg("-Zhint-mostly-unused");
} else {
bcx.gctx
.shell()
.warn("ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it")?;
}
}

let strip = strip.into_inner();
if strip != StripInner::None {
cmd.arg("-C").arg(format!("strip={}", strip));
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ unstable_cli_options!(
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
package_workspace: bool = ("Handle intra-workspace dependencies when packaging"),
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
profile_hint_mostly_unused: bool = ("Enable the `hint-mostly-unused` setting in profiles to mark a crate as mostly unused."),
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
Expand Down Expand Up @@ -1366,6 +1367,7 @@ impl CliUnstable {
"package-workspace" => self.package_workspace = parse_empty(k, v)?,
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
"public-dependency" => self.public_dependency = parse_empty(k, v)?,
"profile-hint-mostly-unused" => self.profile_hint_mostly_unused = parse_empty(k, v)?,
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
"trim-paths" => self.trim_paths = parse_empty(k, v)?,
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,
Expand Down
7 changes: 7 additions & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
if let Some(trim_paths) = &toml.trim_paths {
profile.trim_paths = Some(trim_paths.clone());
}
if let Some(hint_mostly_unused) = toml.hint_mostly_unused {
profile.hint_mostly_unused = hint_mostly_unused;
}
profile.strip = match toml.strip {
Some(StringOrBool::Bool(true)) => Strip::Resolved(StripInner::Named("symbols".into())),
Some(StringOrBool::Bool(false)) => Strip::Resolved(StripInner::None),
Expand Down Expand Up @@ -626,6 +629,8 @@ pub struct Profile {
// remove when `-Ztrim-paths` is stablized
#[serde(skip_serializing_if = "Option::is_none")]
pub trim_paths: Option<TomlTrimPaths>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub hint_mostly_unused: bool,
}

impl Default for Profile {
Expand All @@ -647,6 +652,7 @@ impl Default for Profile {
strip: Strip::Deferred(StripInner::None),
rustflags: vec![],
trim_paths: None,
hint_mostly_unused: false,
}
}
}
Expand Down Expand Up @@ -676,6 +682,7 @@ compact_debug! {
strip
rustflags
trim_paths
hint_mostly_unused
)]
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Each new feature described below should explain how to use it.
* [rustdoc-depinfo](#rustdoc-depinfo) --- Use dep-info files in rustdoc rebuild detection.
* `Cargo.toml` extensions
* [Profile `rustflags` option](#profile-rustflags-option) --- Passed directly to rustc.
* [Profile `hint-mostly-unused` option](#profile-hint-mostly-unused-option) --- Hint that a dependency is mostly unused, to optimize compilation time.
* [codegen-backend](#codegen-backend) --- Select the codegen backend used by rustc.
* [per-package-target](#per-package-target) --- Sets the `--target` to use for each individual package.
* [artifact dependencies](#artifact-dependencies) --- Allow build artifacts to be included into other build artifacts and build them for different targets.
Expand Down Expand Up @@ -924,6 +925,25 @@ profile-rustflags = true
rustflags = [ "-C", "..." ]
```

## Profile `hint-mostly-unused` option
* Tracking Issue: [#15644](https://github.com/rust-lang/cargo/issues/15644)

This feature provides a new option in the `[profile]` section to enable the
rustc `hint-mostly-unused` option. This is primarily useful to enable for
specific dependencies:

```toml
[profile.dev.package.huge-mostly-unused-dependency]
hint-mostly-unused = true
```

To enable this feature, pass `-Zprofile-hint-mostly-unused`. However, since
this option is a hint, using it without passing `-Zprofile-hint-mostly-unused`
will only warn and ignore the profile option. Versions of Cargo prior to the
introduction of this feature will give an "unused manifest key" warning, but
will otherwise function without erroring. This allows using the hint in a
crate's `Cargo.toml` without mandating the use of a newer Cargo to build it.

## rustdoc-map
* Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296)

Expand Down
94 changes: 48 additions & 46 deletions tests/testsuite/cargo/z_help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,7 @@ fn all_profile_options() {
build_override: None,
rustflags: None,
trim_paths: None,
hint_mostly_unused: None,
};
let mut overrides = BTreeMap::new();
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());
Expand Down
Loading
Loading