From 78314caf38fba2e59dd6e4e07ff07f400a1466d9 Mon Sep 17 00:00:00 2001
From: Alex Crichton <alex@alexcrichton.com>
Date: Thu, 16 Jul 2020 08:49:04 -0700
Subject: [PATCH] Ensure `unstable.build-std` works like `-Zbuild-std`

This fixes an issue where the deserializer for `-Zbuild-std` was a bit
fancier than the `unstable.build-std` directive.

cc #8393
---
 src/cargo/core/features.rs      | 15 +++++++++++
 tests/testsuite/standard_lib.rs | 46 ++++++++++++++++++++++++++-------
 2 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs
index 99a6ed3a7dd..9bad61ad2bd 100644
--- a/src/cargo/core/features.rs
+++ b/src/cargo/core/features.rs
@@ -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,
@@ -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() {
diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs
index 953bd66fef5..a06221d0e53 100644
--- a/tests/testsuite/standard_lib.rs
+++ b/tests/testsuite/standard_lib.rs
@@ -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();
@@ -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
@@ -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
     }
 
@@ -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();
+}