1 #![feature(slice_concat_ext)]
3 extern crate compiletest_rs as compiletest;
8 use std::slice::SliceConcatExt;
9 use std::path::{PathBuf, Path};
13 macro_rules! eprintln {
15 let stderr = std::io::stderr();
16 writeln!(stderr.lock(), $($arg)*).unwrap();
20 fn miri_path() -> PathBuf {
21 if rustc_test_suite().is_some() {
22 PathBuf::from(option_env!("MIRI_PATH").unwrap())
24 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
28 fn rustc_test_suite() -> Option<PathBuf> {
29 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
32 fn rustc_lib_path() -> PathBuf {
33 option_env!("RUSTC_LIB_PATH").unwrap().into()
36 fn have_fullmir() -> bool {
37 // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
38 std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
41 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool) {
42 if need_fullmir && !have_fullmir() {
43 eprintln!("{}", format!(
44 "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
51 eprintln!("{}", format!(
52 "## Running compile-fail tests in {} against miri for target {}",
56 let mut config = compiletest::Config::default().tempdir();
57 config.mode = "compile-fail".parse().expect("Invalid mode");
58 config.rustc_path = miri_path();
59 let mut flags = Vec::new();
60 if rustc_test_suite().is_some() {
61 config.run_lib_path = rustc_lib_path();
62 config.compile_lib_path = rustc_lib_path();
64 flags.push(format!("--sysroot {}", sysroot.display()));
65 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
66 config.src_base = PathBuf::from(path.to_string());
67 flags.push("-Zmir-emit-validate=1".to_owned());
68 config.target_rustcflags = Some(flags.join(" "));
69 config.target = target.to_owned();
70 config.host = host.to_owned();
71 compiletest::run_tests(&config);
74 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
75 if need_fullmir && !have_fullmir() {
76 eprintln!("{}", format!(
77 "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
84 let opt_str = if opt { " with optimizations" } else { "" };
85 eprintln!("{}", format!(
86 "## Running run-pass tests in {} against miri for target {}{}",
91 let mut config = compiletest::Config::default().tempdir();
92 config.mode = "ui".parse().expect("Invalid mode");
93 config.src_base = PathBuf::from(path);
94 config.target = target.to_owned();
95 config.host = host.to_owned();
96 config.rustc_path = miri_path();
97 if rustc_test_suite().is_some() {
98 config.run_lib_path = rustc_lib_path();
99 config.compile_lib_path = rustc_lib_path();
101 let mut flags = Vec::new();
102 flags.push(format!("--sysroot {}", sysroot.display()));
103 flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
105 flags.push("-Zmiri-start-fn".to_owned());
108 flags.push("-Zmir-opt-level=3".to_owned());
110 flags.push("-Zmir-opt-level=0".to_owned());
111 // For now, only validate without optimizations. Inlining breaks validation.
112 flags.push("-Zmir-emit-validate=1".to_owned());
114 // Control miri logging. This is okay despite concurrent test execution as all tests
115 // will set this env var to the same value.
116 env::set_var("MIRI_LOG", "warn");
117 config.target_rustcflags = Some(flags.join(" "));
118 compiletest::run_tests(&config);
121 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
122 let mut path = path.into();
124 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
127 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
128 let target_dir = sysroot.join("lib").join("rustlib");
129 for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
130 let entry = entry.unwrap();
131 if !is_target_dir(entry.path()) {
134 let target = entry.file_name().into_string().unwrap();
139 fn get_sysroot() -> PathBuf {
140 let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
141 let sysroot = std::process::Command::new("rustc")
145 .expect("rustc not found")
147 String::from_utf8(sysroot).expect("sysroot is not utf8")
149 PathBuf::from(sysroot.trim())
152 fn get_host() -> String {
153 let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
154 println!("using rustc at {}", rustc.display());
155 let host = std::process::Command::new(rustc)
158 .expect("rustc not found for -vV")
160 let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
161 let host = host.split("\nhost: ").nth(1).expect(
162 "no host: part in rustc -vV",
164 let host = host.split('\n').next().expect("no \n after host");
168 fn run_pass_miri(opt: bool) {
169 let sysroot = get_sysroot();
170 let host = get_host();
172 for_all_targets(&sysroot, |target| {
173 miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
175 miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
178 fn compile_fail_miri() {
179 let sysroot = get_sysroot();
180 let host = get_host();
182 // FIXME: run tests for other targets, too
183 compile_fail(&sysroot, "tests/compile-fail", &host, &host, false);
184 compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
189 // We put everything into a single test to avoid the parallelism `cargo test`
190 // introduces. We still get parallelism within our tests because `compiletest`
191 // uses `libtest` which runs jobs in parallel.
193 run_pass_miri(false);
194 // FIXME: Disabled for now, as the optimizer is pretty broken and crashes...
195 // See https://github.com/rust-lang/rust/issues/50411
196 //run_pass_miri(true);