1 #![feature(slice_concat_ext)]
3 extern crate compiletest_rs as compiletest;
8 use std::slice::SliceConcatExt;
9 use std::path::{PathBuf, Path};
12 macro_rules! eprintln {
14 let stderr = std::io::stderr();
15 writeln!(stderr.lock(), $($arg)*).unwrap();
19 fn miri_path() -> PathBuf {
20 if rustc_test_suite().is_some() {
21 PathBuf::from(option_env!("MIRI_PATH").unwrap())
23 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
27 fn rustc_test_suite() -> Option<PathBuf> {
28 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
31 fn rustc_lib_path() -> PathBuf {
32 option_env!("RUSTC_LIB_PATH").unwrap().into()
35 fn have_fullmir() -> bool {
36 // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
37 std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
40 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
41 if need_fullmir && !have_fullmir() {
42 eprintln!("{}", format!(
43 "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
50 let opt_str = if opt { " with optimizations" } else { "" };
51 eprintln!("{}", format!(
52 "## Running compile-fail tests in {} against miri for target {}{}",
58 let mut flags = Vec::new();
59 flags.push(format!("--sysroot {}", sysroot.display()));
60 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
61 flags.push("-Zmir-emit-validate=1".to_owned());
63 // Optimizing too aggressivley makes UB detection harder, but test at least
65 flags.push("-Zmir-opt-level=1".to_owned());
67 flags.push("-Zmir-opt-level=0".to_owned());
70 let mut config = compiletest::Config::default().tempdir();
71 config.mode = "compile-fail".parse().expect("Invalid mode");
72 config.rustc_path = miri_path();
73 if rustc_test_suite().is_some() {
74 config.run_lib_path = rustc_lib_path();
75 config.compile_lib_path = rustc_lib_path();
77 config.src_base = PathBuf::from(path.to_string());
78 config.target_rustcflags = Some(flags.join(" "));
79 config.target = target.to_owned();
80 config.host = host.to_owned();
81 compiletest::run_tests(&config);
84 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
85 if need_fullmir && !have_fullmir() {
86 eprintln!("{}", format!(
87 "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
94 let opt_str = if opt { " with optimizations" } else { "" };
95 eprintln!("{}", format!(
96 "## Running run-pass tests in {} against miri for target {}{}",
102 let mut flags = Vec::new();
103 flags.push(format!("--sysroot {}", sysroot.display()));
104 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
105 flags.push("-Zmir-emit-validate=1".to_owned());
107 // FIXME: Using level 1 (instead of 3) for now, as the optimizer is pretty broken
109 // Level 0 and 1 are not the same, so this still gives us *some* coverage.
110 // See https://github.com/rust-lang/rust/issues/50411
111 flags.push("-Zmir-opt-level=1".to_owned());
113 flags.push("-Zmir-opt-level=0".to_owned());
116 let mut config = compiletest::Config::default().tempdir();
117 config.mode = "ui".parse().expect("Invalid mode");
118 config.src_base = PathBuf::from(path);
119 config.target = target.to_owned();
120 config.host = host.to_owned();
121 config.rustc_path = miri_path();
122 if rustc_test_suite().is_some() {
123 config.run_lib_path = rustc_lib_path();
124 config.compile_lib_path = rustc_lib_path();
126 config.target_rustcflags = Some(flags.join(" "));
127 compiletest::run_tests(&config);
130 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
131 let mut path = path.into();
133 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
136 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
137 let target_dir = sysroot.join("lib").join("rustlib");
138 for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
139 let entry = entry.unwrap();
140 if !is_target_dir(entry.path()) {
143 let target = entry.file_name().into_string().unwrap();
148 fn get_sysroot() -> PathBuf {
149 let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
150 let sysroot = std::process::Command::new("rustc")
154 .expect("rustc not found")
156 String::from_utf8(sysroot).expect("sysroot is not utf8")
158 PathBuf::from(sysroot.trim())
161 fn get_host() -> String {
162 let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
163 println!("using rustc at {}", rustc.display());
164 let host = std::process::Command::new(rustc)
167 .expect("rustc not found for -vV")
169 let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
170 let host = host.split("\nhost: ").nth(1).expect(
171 "no host: part in rustc -vV",
173 let host = host.split('\n').next().expect("no \n after host");
177 fn run_pass_miri(opt: bool) {
178 let sysroot = get_sysroot();
179 let host = get_host();
181 for_all_targets(&sysroot, |target| {
182 miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
184 miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
187 fn compile_fail_miri(opt: bool) {
188 let sysroot = get_sysroot();
189 let host = get_host();
191 // FIXME: run tests for other targets, too
192 compile_fail(&sysroot, "tests/compile-fail", &host, &host, false, opt);
193 compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true, opt);
198 // We put everything into a single test to avoid the parallelism `cargo test`
199 // introduces. We still get parallelism within our tests because `compiletest`
200 // uses `libtest` which runs jobs in parallel.
202 run_pass_miri(false);
205 compile_fail_miri(false);
206 compile_fail_miri(true);