1 #![feature(slice_concat_ext, custom_test_frameworks)]
2 // Custom test runner, to avoid libtest being wrapped around compiletest which wraps libtest.
3 #![test_runner(test_runner)]
5 use std::slice::SliceConcatExt;
6 use std::path::{PathBuf, Path};
9 use compiletest_rs as compiletest;
12 fn miri_path() -> PathBuf {
13 if rustc_test_suite().is_some() {
14 PathBuf::from(option_env!("MIRI_PATH").unwrap())
16 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
20 fn rustc_test_suite() -> Option<PathBuf> {
21 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
24 fn rustc_lib_path() -> PathBuf {
25 option_env!("RUSTC_LIB_PATH").unwrap().into()
28 fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp {
29 let mut config = compiletest::Config::default().tempdir();
30 config.mode = mode.parse().expect("Invalid mode");
31 config.rustc_path = miri_path();
32 if rustc_test_suite().is_some() {
33 config.run_lib_path = rustc_lib_path();
34 config.compile_lib_path = rustc_lib_path();
36 config.filter = env::args().nth(1);
37 config.host = get_host();
41 fn compile_fail(path: &str, target: &str, opt: bool) {
42 let opt_str = if opt { " with optimizations" } else { "" };
43 eprintln!("{}", format!(
44 "## Running compile-fail tests in {} against miri for target {}{}",
50 let mut flags = Vec::new();
51 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
52 flags.push("--edition 2018".to_owned());
54 // Optimizing too aggressivley makes UB detection harder, but test at least
56 // FIXME: Opt level 3 ICEs during stack trace generation.
57 flags.push("-Zmir-opt-level=1".to_owned());
60 let mut config = mk_config("compile-fail");
61 config.src_base = PathBuf::from(path);
62 config.target = target.to_owned();
63 config.target_rustcflags = Some(flags.join(" "));
64 compiletest::run_tests(&config);
67 fn miri_pass(path: &str, target: &str, opt: bool) {
68 let opt_str = if opt { " with optimizations" } else { "" };
69 eprintln!("{}", format!(
70 "## Running run-pass tests in {} against miri for target {}{}",
76 let mut flags = Vec::new();
77 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
78 flags.push("--edition 2018".to_owned());
80 // FIXME: We use opt level 1 because MIR inlining defeats the validation
82 flags.push("-Zmir-opt-level=1".to_owned());
85 let mut config = mk_config("ui");
86 config.src_base = PathBuf::from(path);
87 config.target = target.to_owned();
88 config.target_rustcflags = Some(flags.join(" "));
89 compiletest::run_tests(&config);
92 /// Make sure the MIRI_SYSROOT env var is set
94 if std::env::var("MIRI_SYSROOT").is_ok() {
98 let sysroot = std::process::Command::new("rustc")
102 .expect("rustc not found")
104 let sysroot = String::from_utf8(sysroot).expect("sysroot is not utf8");
105 std::env::set_var("MIRI_SYSROOT", sysroot.trim());
108 fn get_host() -> String {
109 let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
110 let rustc_version = std::process::Command::new(rustc)
113 .expect("rustc not found for -vV")
115 let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
116 let version_meta = rustc_version::version_meta_for(&rustc_version)
117 .expect("failed to parse rustc version info");
121 fn get_target() -> String {
122 std::env::var("MIRI_TARGET").unwrap_or_else(|_| get_host())
125 fn run_pass_miri(opt: bool) {
126 miri_pass("tests/run-pass", &get_target(), opt);
129 fn compile_fail_miri(opt: bool) {
130 compile_fail("tests/compile-fail", &get_target(), opt);
133 fn test_runner(_tests: &[&()]) {
136 run_pass_miri(false);
139 compile_fail_miri(false);
140 compile_fail_miri(true);