Skip to content

Commit 7ac51b7

Browse files
committed
Auto merge of #7360 - phil-opp:zbuild-std-custom-test-frameworks, r=alexcrichton
[-Zbuild-std] Only build libtest when libstd is built Currently `libtest` is always compiled when a compilation unit uses a test harness. This implicitly adds builds the standard library too because `libtest` depends on it. This breaks the use of custom test frameworks in `no_std` crates as reported in #7216 (comment). This pull request fixes the issue by only building `libtest` if `libstd` is built. This makes sense in my opinion because when the user explicitly specified `-Zbuild-std=core`, they probably don't want to build the full standard library and rather get a compilation error when they accidentally use `libtest`.
2 parents be01054 + 6b4fd44 commit 7ac51b7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/cargo/ops/cargo_compile.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,10 @@ pub fn compile_ws<'a>(
398398
.iter()
399399
.any(|unit| unit.mode.is_rustc_test() && unit.target.harness())
400400
{
401-
crates.push("test".to_string());
401+
// Only build libtest when libstd is built (libtest depends on libstd)
402+
if crates.iter().any(|c| c == "std") {
403+
crates.push("test".to_string());
404+
}
402405
}
403406
standard_lib::generate_std_roots(&bcx, &crates, std_resolve.as_ref().unwrap())?
404407
} else {

tests/testsuite/standard_lib.rs

+42
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn std_lib() {
3737
hashbrown();
3838
libc();
3939
test();
40+
custom_test_framework();
4041
target_proc_macro();
4142
bench();
4243
doc();
@@ -186,6 +187,47 @@ fn test() {
186187
.run();
187188
}
188189

190+
fn custom_test_framework() {
191+
let p = project()
192+
.file(
193+
"src/lib.rs",
194+
r#"
195+
#![no_std]
196+
#![cfg_attr(test, no_main)]
197+
#![feature(custom_test_frameworks)]
198+
#![test_runner(crate::test_runner)]
199+
200+
pub fn test_runner(_tests: &[&dyn Fn()]) {}
201+
202+
#[panic_handler]
203+
fn panic(_info: &core::panic::PanicInfo) -> ! {
204+
loop {}
205+
}
206+
"#,
207+
)
208+
.file(
209+
"target.json",
210+
r#"
211+
{
212+
"llvm-target": "x86_64-unknown-none-gnu",
213+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
214+
"arch": "x86_64",
215+
"target-endian": "little",
216+
"target-pointer-width": "64",
217+
"target-c-int-width": "32",
218+
"os": "none",
219+
"linker-flavor": "ld.lld",
220+
"linker": "rust-lld",
221+
"executables": true,
222+
"panic-strategy": "abort"
223+
}
224+
"#,
225+
)
226+
.build();
227+
228+
cargo_build_std(&p, "test --target target.json --no-run -v", "core").run();
229+
}
230+
189231
fn target_proc_macro() {
190232
let p = project()
191233
.file(

0 commit comments

Comments
 (0)