Skip to content

Commit

Permalink
Ensure unstable.build-std works like -Zbuild-std
Browse files Browse the repository at this point in the history
This fixes an issue where the deserializer for `-Zbuild-std` was a bit
fancier than the `unstable.build-std` directive.

cc #8393
  • Loading branch information
alexcrichton committed Jul 16, 2020
1 parent 1bc6e45 commit 78314ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ pub struct CliUnstable {
pub mtime_on_use: bool,
pub named_profiles: bool,
pub binary_dep_depinfo: bool,
#[serde(deserialize_with = "deserialize_build_std")]
pub build_std: Option<Vec<String>>,
pub timings: Option<Vec<String>>,
pub doctest_xcompile: bool,
Expand All @@ -361,6 +362,20 @@ pub struct CliUnstable {
pub terminal_width: Option<Option<usize>>,
}

fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
D: serde::Deserializer<'de>,
{
let crates = match <Option<Vec<String>>>::deserialize(deserializer)? {
Some(list) => list,
None => return Ok(None),
};
let v = crates.join(",");
Ok(Some(
crate::core::compiler::standard_lib::parse_unstable_flag(Some(&v)),
))
}

impl CliUnstable {
pub fn parse(&mut self, flags: &[String]) -> CargoResult<()> {
if !flags.is_empty() && !nightly_features_allowed() {
Expand Down
46 changes: 37 additions & 9 deletions tests/testsuite/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn setup() -> Option<Setup> {
})
}

fn enable_build_std(e: &mut Execs, setup: &Setup, arg: Option<&str>) {
fn enable_build_std(e: &mut Execs, setup: &Setup) {
// First up, force Cargo to use our "mock sysroot" which mimics what
// libstd looks like upstream.
let root = paths::root();
Expand All @@ -142,12 +142,6 @@ fn enable_build_std(e: &mut Execs, setup: &Setup, arg: Option<&str>) {
.join("tests/testsuite/mock-std");
e.env("__CARGO_TESTS_ONLY_SRC_ROOT", &root);

// Actually enable `-Zbuild-std` for now
let arg = match arg {
Some(s) => format!("-Zbuild-std={}", s),
None => "-Zbuild-std".to_string(),
};
e.arg(arg);
e.masquerade_as_nightly_cargo();

// We do various shenanigans to ensure our "mock sysroot" actually links
Expand Down Expand Up @@ -181,12 +175,14 @@ trait BuildStd: Sized {

impl BuildStd for Execs {
fn build_std(&mut self, setup: &Setup) -> &mut Self {
enable_build_std(self, setup, None);
enable_build_std(self, setup);
self.arg("-Zbuild-std");
self
}

fn build_std_arg(&mut self, setup: &Setup, arg: &str) -> &mut Self {
enable_build_std(self, setup, Some(arg));
enable_build_std(self, setup);
self.arg(format!("-Zbuild-std={}", arg));
self
}

Expand Down Expand Up @@ -604,3 +600,35 @@ fn ignores_incremental() {
.unwrap()
.starts_with("foo-"));
}

#[cargo_test]
fn cargo_config_injects_compiler_builtins() {
let setup = match setup() {
Some(s) => s,
None => return,
};
let p = project()
.file(
"src/lib.rs",
r#"
#![no_std]
pub fn foo() {
assert_eq!(u8::MIN, 0);
}
"#,
)
.file(
".cargo/config.toml",
r#"
[unstable]
build-std = ['core']
"#,
)
.build();
let mut build = p.cargo("build -v --lib");
enable_build_std(&mut build, &setup);
build
.target_host()
.with_stderr_does_not_contain("[..]libstd[..]")
.run();
}

0 comments on commit 78314ca

Please sign in to comment.