Skip to content

Commit 0ccf610

Browse files
committed
fix(cargo-rustc): give trailing flags higher precedence
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
1 parent 9311738 commit 0ccf610

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
694694
base.inherit_jobserver(&build_runner.jobserver);
695695
build_deps_args(&mut base, build_runner, unit)?;
696696
add_cap_lints(build_runner.bcx, unit, &mut base);
697+
if build_runner.bcx.gctx.nightly_features_allowed {
698+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
699+
base.args(args);
700+
}
701+
}
697702
base.args(&unit.rustflags);
698703
if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
699704
base.arg("-Z").arg("binary-dep-depinfo");
@@ -753,8 +758,11 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
753758
}
754759

755760
rustdoc.args(unit.pkg.manifest().lint_rustflags());
756-
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
757-
rustdoc.args(args);
761+
762+
if !build_runner.bcx.gctx.nightly_features_allowed {
763+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
764+
rustdoc.args(args);
765+
}
758766
}
759767

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

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

806+
if build_runner.bcx.gctx.nightly_features_allowed {
807+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
808+
rustdoc.args(args);
809+
}
810+
}
798811
rustdoc.args(&unit.rustdocflags);
799812

800813
if !crate_version_flag_already_present(&rustdoc) {
@@ -1097,8 +1110,10 @@ fn build_base_args(
10971110

10981111
cmd.args(unit.pkg.manifest().lint_rustflags());
10991112
cmd.args(&profile_rustflags);
1100-
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
1101-
cmd.args(args);
1113+
if !build_runner.bcx.gctx.nightly_features_allowed {
1114+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
1115+
cmd.args(args);
1116+
}
11021117
}
11031118

11041119
// `-C overflow-checks` is implied by the setting of `-C debug-assertions`,

tests/testsuite/rustc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,12 @@ fn precedence() {
819819

820820
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
821821
.env("RUSTFLAGS", "--cfg from_rustflags")
822-
.with_stderr_data(
823-
str![[r#"
822+
.masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
823+
.with_stderr_data(str![[r#"
824824
[COMPILING] foo v0.0.0 ([ROOT]/foo)
825-
[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
825+
[RUNNING] `rustc [..]-C strip=debuginfo [..]--cfg cargo_rustc -C strip=symbols --cfg from_rustflags`
826826
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
827827
828-
"#]]
829-
)
828+
"#]])
830829
.run();
831830
}

0 commit comments

Comments
 (0)