Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure unstable.build-std works like -Zbuild-std #8491

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}