From f70bfd3f4a92d269885ae66e9e720bdde1a7851b Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 26 Apr 2024 08:51:23 -0400 Subject: [PATCH 1/2] test: emit 1.77 syntax error only when msrv is incompatible --- tests/testsuite/build_script.rs | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 06fcde62037..1d1863acb97 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -5502,6 +5502,44 @@ for more information about build script outputs. .run(); } +#[cargo_test] +fn test_new_syntax_with_compatible_partial_msrv() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + edition = "2015" + build = "build.rs" + rust-version = "1.77" + "#, + ) + .file("src/lib.rs", "") + .file( + "build.rs", + r#" + fn main() { + println!("cargo::metadata=foo=bar"); + } + "#, + ) + .build(); + + p.cargo("check") + .with_status(101) + .with_stderr_contains( + "\ +[COMPILING] foo [..] +[ERROR] the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \ +but the minimum supported Rust version of `foo v0.0.0 ([ROOT]/foo)` is 1.77. +See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \ +for more information about build script outputs. +", + ) + .run(); +} + #[cargo_test] fn test_old_syntax_with_old_msrv() { let p = project() From ba5ec686f9242ad52a11180dde94cdb590d732c5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 26 Apr 2024 09:30:26 -0400 Subject: [PATCH 2/2] fix: emit 1.77 syntax error only when msrv is incompatible --- src/cargo/core/compiler/custom_build.rs | 5 ++--- tests/testsuite/build_script.rs | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 426d8f128c2..829177f5fbb 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -724,10 +724,9 @@ impl BuildOutput { pkg_descr: &str, msrv: &Option, ) -> CargoResult<()> { - let new_syntax_added_in = &RustVersion::from_str("1.77.0")?; - if let Some(msrv) = msrv { - if msrv < new_syntax_added_in { + let new_syntax_added_in = RustVersion::from_str("1.77.0")?; + if !new_syntax_added_in.is_compatible_with(msrv.as_partial()) { bail!( "the `cargo::` syntax for build script output instructions was added in \ Rust 1.77.0, but the minimum supported Rust version of `{pkg_descr}` is {msrv}.\n\ diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 1d1863acb97..d8fed645aec 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -5527,14 +5527,9 @@ fn test_new_syntax_with_compatible_partial_msrv() { .build(); p.cargo("check") - .with_status(101) .with_stderr_contains( "\ [COMPILING] foo [..] -[ERROR] the `cargo::` syntax for build script output instructions was added in Rust 1.77.0, \ -but the minimum supported Rust version of `foo v0.0.0 ([ROOT]/foo)` is 1.77. -See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \ -for more information about build script outputs. ", ) .run();