diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs index 6b3af85c6..f2e15441a 100644 --- a/crates/cargo-test-support/src/paths.rs +++ b/crates/cargo-test-support/src/paths.rs @@ -6,6 +6,7 @@ use std::env; use std::fs; use std::io::{self, ErrorKind}; use std::path::{Path, PathBuf}; +use std::process::Command; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; @@ -252,3 +253,14 @@ pub fn get_lib_extension(kind: &str) -> &str { _ => unreachable!(), } } + +/// Returns the sysroot as queried from rustc. +pub fn sysroot() -> String { + let output = Command::new("rustc") + .arg("--print=sysroot") + .output() + .expect("rustc to run"); + assert!(output.status.success()); + let sysroot = String::from_utf8(output.stdout).unwrap(); + sysroot.trim().to_string() +} diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 6e2dbc030..345b16c9e 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -19,6 +19,8 @@ //! Otherwise the tests are skipped. use cargo_test_support::*; +use std::env; +use std::path::Path; fn enable_build_std(e: &mut Execs, arg: Option<&str>) { e.env_remove("CARGO_HOME"); @@ -174,7 +176,21 @@ fn custom_test_framework() { ) .build(); + // This is a bit of a hack to use the rust-lld that ships with most toolchains. + let sysroot = paths::sysroot(); + let sysroot = Path::new(&sysroot); + let sysroot_bin = sysroot + .join("lib") + .join("rustlib") + .join(rustc_host()) + .join("bin"); + let path = env::var_os("PATH").unwrap_or_default(); + let mut paths = env::split_paths(&path).collect::>(); + paths.insert(0, sysroot_bin); + let new_path = env::join_paths(paths).unwrap(); + p.cargo("test --target target.json --no-run -v") + .env("PATH", new_path) .build_std_arg("core") .run(); } diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 621e35f0a..6b3aaf0f4 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -2,7 +2,6 @@ use cargo_test_support::registry::{Dependency, Package}; use cargo_test_support::ProjectBuilder; use cargo_test_support::{is_nightly, paths, project, rustc_host, Execs}; use std::path::PathBuf; -use std::process::Command; struct Setup { rustc_wrapper: PathBuf, @@ -116,18 +115,9 @@ fn setup() -> Option { .build(); p.cargo("build").run(); - let output = Command::new("rustc") - .arg("--print") - .arg("sysroot") - .output() - .unwrap(); - assert!(output.status.success()); - let real_sysroot = String::from_utf8(output.stdout).unwrap(); - let real_sysroot = real_sysroot.trim(); - return Some(Setup { rustc_wrapper: p.bin("foo"), - real_sysroot: real_sysroot.to_string(), + real_sysroot: paths::sysroot(), }); } @@ -514,7 +504,7 @@ fn doctest() { r#" /// Doc /// ``` - /// assert_eq!(1, 1); + /// std::custom_api(); /// ``` pub fn f() {} "#, @@ -523,6 +513,59 @@ fn doctest() { p.cargo("test --doc -v") .build_std(&setup) + .with_stdout_contains("test src/lib.rs - f [..] ... ok") .target_host() .run(); } + +#[cargo_test] +fn no_implicit_alloc() { + // Demonstrate that alloc is not implicitly in scope. + let setup = match setup() { + Some(s) => s, + None => return, + }; + let p = project() + .file( + "src/lib.rs", + r#" + pub fn f() { + let _: Vec = alloc::vec::Vec::new(); + } + "#, + ) + .build(); + + p.cargo("build -v") + .build_std(&setup) + .target_host() + .with_stderr_contains("[..]use of undeclared [..]`alloc`") + .with_status(101) + .run(); +} + +#[cargo_test] +fn macro_expanded_shadow() { + // This tests a bug caused by the previous use of `--extern` to directly + // load sysroot crates. This necessitated the switch to `--sysroot` to + // retain existing behavior. See + // https://github.com/rust-lang/wg-cargo-std-aware/issues/40 for more + // detail. + let setup = match setup() { + Some(s) => s, + None => return, + }; + let p = project() + .file( + "src/lib.rs", + r#" + macro_rules! a { + () => (extern crate std as alloc;) + } + a!(); + "#, + ) + .build(); + + p.cargo("build -v").build_std(&setup).target_host().run(); +}