Skip to content

Commit

Permalink
fix(cargo-rustc): stabilize higher precedence trailing flags (#14900)
Browse files Browse the repository at this point in the history
### What does this PR try to resolve?

This was always enabled on nightly since 1.83-nightly (2024-09).
We have no feedback since then, so assume it is a low-impact change.

This stabilization is targeted at 1.85 (2025-02-20)

Fixes #14346

### How should we test and review this PR?

Let's do an FCP.
  • Loading branch information
epage authored Dec 18, 2024
2 parents 99dff6d + 1d74d95 commit 72ffd28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
39 changes: 4 additions & 35 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,8 @@ 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 cargo_rustc_higher_args_precedence(build_runner) {
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
base.args(args);
}
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 {
Expand Down Expand Up @@ -754,12 +752,6 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu

rustdoc.args(unit.pkg.manifest().lint_rustflags());

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

let metadata = build_runner.metadata_for_doc_units[unit];
rustdoc
.arg("-C")
Expand Down Expand Up @@ -800,10 +792,8 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu

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

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

Expand Down Expand Up @@ -1107,11 +1097,6 @@ fn build_base_args(

cmd.args(unit.pkg.manifest().lint_rustflags());
cmd.args(&profile_rustflags);
if !cargo_rustc_higher_args_precedence(build_runner) {
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`,
// so we only need to provide `-C overflow-checks` if it differs from
Expand Down Expand Up @@ -2007,19 +1992,3 @@ fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoR
.outputs(unit)
.map(|outputs| outputs[0].path.clone())
}

/// Provides a way to change the precedence of `cargo rustc -- <flags>`.
///
/// This is intended to be a short-live function.
///
/// See <https://github.com/rust-lang/cargo/issues/14346>
fn cargo_rustc_higher_args_precedence(build_runner: &BuildRunner<'_, '_>) -> bool {
build_runner.bcx.gctx.nightly_features_allowed
&& build_runner
.bcx
.gctx
.get_env("__CARGO_RUSTC_ORIG_ARGS_PRIO")
.ok()
.as_deref()
!= Some("1")
}
47 changes: 8 additions & 39 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn lib() {
p.cargo("rustc --lib -v -- -C debug-assertions=off")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --emit=[..]link[..]-C debuginfo=2 [..]-C debug-assertions=off[..]-C metadata=[..] [..]--out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps`
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --emit=[..]link[..]-C debuginfo=2 [..]-C metadata=[..] [..]--out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps[..]-C debug-assertions=off[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand All @@ -46,7 +46,7 @@ fn build_main_and_allow_unstable_options() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --emit=[..]link[..]-C debuginfo=2 [..]-C metadata=[..] --out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps`
[RUNNING] `rustc --crate-name foo --edition=2015 src/main.rs [..]--crate-type bin --emit=[..]link[..]-C debuginfo=2 [..]-C debug-assertions[..]-C metadata=[..] --out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps --extern foo=[ROOT]/foo/target/debug/deps/libfoo-[HASH].rlib`
[RUNNING] `rustc --crate-name foo --edition=2015 src/main.rs [..]--crate-type bin --emit=[..]link[..]-C debuginfo=2 [..]-C metadata=[..] --out-dir [ROOT]/foo/target/debug/deps -L dependency=[ROOT]/foo/target/debug/deps --extern foo=[ROOT]/foo/target/debug/deps/libfoo-[HASH].rlib[..]-C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -83,7 +83,7 @@ fn build_with_args_to_one_of_multiple_binaries() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --emit=[..]link[..]-C debuginfo=2 [..]-C metadata=[..] --out-dir [..]`
[RUNNING] `rustc --crate-name bar --edition=2015 src/bin/bar.rs [..]--crate-type bin --emit=[..]link[..]-C debuginfo=2 [..]-C debug-assertions [..]`
[RUNNING] `rustc --crate-name bar --edition=2015 src/bin/bar.rs [..]--crate-type bin --emit=[..]link[..]-C debuginfo=2 [..]-C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -358,7 +358,7 @@ fn build_with_args_to_one_of_multiple_tests() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --emit=[..]link[..]-C debuginfo=2 [..]-C metadata=[..] --out-dir [..]`
[RUNNING] `rustc --crate-name bar --edition=2015 tests/bar.rs [..]--emit=[..]link[..]-C debuginfo=2 [..]-C debug-assertions --test[..]`
[RUNNING] `rustc --crate-name bar --edition=2015 tests/bar.rs [..]--emit=[..]link[..]-C debuginfo=2 [..]--test[..]-C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -395,7 +395,7 @@ fn build_foo_with_bar_dependency() {
[COMPILING] bar v0.1.0 ([ROOT]/bar)
[RUNNING] `rustc --crate-name bar [..] -C debuginfo=2[..]`
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo [..] -C debuginfo=2 [..]-C debug-assertions [..]`
[RUNNING] `rustc --crate-name foo [..] -C debuginfo=2 [..]-C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -430,7 +430,7 @@ fn build_only_bar_dependency() {
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[COMPILING] bar v0.1.0 ([ROOT]/bar)
[RUNNING] `rustc --crate-name bar [..]--crate-type lib [..] -C debug-assertions [..]`
[RUNNING] `rustc --crate-name bar [..]--crate-type lib [..] -C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -599,7 +599,7 @@ fn rustc_fingerprint() {
p.cargo("rustc -v -- -C debug-assertions")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo [..]-C debug-assertions [..]`
[RUNNING] `rustc --crate-name foo [..]-C debug-assertions[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -657,7 +657,7 @@ fn rustc_test_with_implicit_bin() {
.with_stderr_data(
str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name test1 --edition=2015 tests/test1.rs [..] --cfg foo [..]`
[RUNNING] `rustc --crate-name test1 --edition=2015 tests/test1.rs [..] --cfg foo[..]`
[RUNNING] `rustc --crate-name foo --edition=2015 src/main.rs [..]`
...
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
Expand Down Expand Up @@ -816,42 +816,11 @@ fn precedence() {

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

// Ensure the short-live env var to work
p.cargo("clean").run();
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
.env("RUSTFLAGS", "--cfg from_rustflags")
.env("__CARGO_RUSTC_ORIG_ARGS_PRIO", "1")
.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`
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
"#]]
)
.run();

// Ensure non-nightly to work as before
p.cargo("clean").run();
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
.env("RUSTFLAGS", "--cfg from_rustflags")
.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`
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
"#]]
)
.run();
}
8 changes: 4 additions & 4 deletions tests/testsuite/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn rustdoc_args() {
p.cargo("rustdoc -v -- --cfg=foo")
.with_stderr_data(str![[r#"
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]`
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..]-C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]--cfg=foo[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[GENERATED] [ROOT]/foo/target/doc/foo/index.html
Expand Down Expand Up @@ -159,7 +159,7 @@ fn rustdoc_foo_with_bar_dependency() {
[CHECKING] bar v0.0.1 ([ROOT]/bar)
[RUNNING] `rustc [..] [ROOT]/bar/src/lib.rs [..]`
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps --extern [..]`
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..]-C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps --extern [..]--cfg=foo[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[GENERATED] [ROOT]/foo/target/doc/foo/index.html
Expand Down Expand Up @@ -195,7 +195,7 @@ fn rustdoc_only_bar_dependency() {
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[DOCUMENTING] bar v0.0.1 ([ROOT]/bar)
[RUNNING] `rustdoc [..] --crate-name bar [ROOT]/bar/src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]`
[RUNNING] `rustdoc [..] --crate-name bar [ROOT]/bar/src/lib.rs -o [ROOT]/foo/target/doc [..]-C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]--cfg=foo[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[GENERATED] [ROOT]/foo/target/doc/bar/index.html
Expand All @@ -213,7 +213,7 @@ fn rustdoc_same_name_documents_lib() {
p.cargo("rustdoc -v -- --cfg=foo")
.with_stderr_data(str![[r#"
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..] --cfg=foo -C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]`
[RUNNING] `rustdoc [..] --crate-name foo src/lib.rs -o [ROOT]/foo/target/doc [..]-C metadata=[..] -L dependency=[ROOT]/foo/target/debug/deps [..]--cfg=foo[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[GENERATED] [ROOT]/foo/target/doc/foo/index.html
Expand Down

0 comments on commit 72ffd28

Please sign in to comment.