Skip to content

Commit

Permalink
fix(cargo-rustc): give trailing flags higher precedence
Browse files Browse the repository at this point in the history
Previously `cargo rustc -- <flags>` got a lower precedence than
some of the flags set by cargo internal.
This is a bit unintuitive as Cargo generally treats user-provided
CLI flags with the highest priority.

This commit changes `cargo rustc -- <flags>` to a higher precedence:
higher than most of flags set by Cargo, and only lower than
`build.rustflags` family.

Unsure if this affects people's workflow, so this behavior is only
enabled on nightly for collectin feedback. If everything goes well,
the nightly gate will be removed after a few of releases.

See discussion on https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/rustflags.20precendence.20of.20.60cargo.20rustc.60
  • Loading branch information
weihanglo committed Sep 24, 2024
1 parent 9311738 commit 0ccf610
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 19 additions & 4 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
base.inherit_jobserver(&build_runner.jobserver);
build_deps_args(&mut base, build_runner, unit)?;
add_cap_lints(build_runner.bcx, unit, &mut base);
if build_runner.bcx.gctx.nightly_features_allowed {
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
base.args(args);
}
}
base.args(&unit.rustflags);
if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
base.arg("-Z").arg("binary-dep-depinfo");
Expand Down Expand Up @@ -753,8 +758,11 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
}

rustdoc.args(unit.pkg.manifest().lint_rustflags());
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
rustdoc.args(args);

if !build_runner.bcx.gctx.nightly_features_allowed {
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
rustdoc.args(args);
}
}

let metadata = build_runner.metadata_for_doc_units[unit];
Expand Down Expand Up @@ -795,6 +803,11 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu

rustdoc::add_output_format(build_runner, unit, &mut rustdoc)?;

if build_runner.bcx.gctx.nightly_features_allowed {
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
rustdoc.args(args);
}
}
rustdoc.args(&unit.rustdocflags);

if !crate_version_flag_already_present(&rustdoc) {
Expand Down Expand Up @@ -1097,8 +1110,10 @@ fn build_base_args(

cmd.args(unit.pkg.manifest().lint_rustflags());
cmd.args(&profile_rustflags);
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
cmd.args(args);
if !build_runner.bcx.gctx.nightly_features_allowed {
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
cmd.args(args);
}
}

// `-C overflow-checks` is implied by the setting of `-C debug-assertions`,
Expand Down
9 changes: 4 additions & 5 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,13 +819,12 @@ fn precedence() {

p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
.env("RUSTFLAGS", "--cfg from_rustflags")
.with_stderr_data(
str![[r#"
.masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
[RUNNING] `rustc [..]-C strip=debuginfo [..]--cfg cargo_rustc -C strip=symbols --cfg from_rustflags`
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
"#]]
)
"#]])
.run();
}

0 comments on commit 0ccf610

Please sign in to comment.