1 #![feature(slice_concat_ext)]
3 extern crate compiletest_rs as compiletest;
6 use std::slice::SliceConcatExt;
7 use std::path::{PathBuf, Path};
11 macro_rules! eprintln {
13 let stderr = std::io::stderr();
14 writeln!(stderr.lock(), $($arg)*).unwrap();
18 fn miri_path() -> PathBuf {
19 if rustc_test_suite().is_some() {
20 PathBuf::from(option_env!("MIRI_PATH").unwrap())
22 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
26 fn rustc_test_suite() -> Option<PathBuf> {
27 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
30 fn rustc_lib_path() -> PathBuf {
31 option_env!("RUSTC_LIB_PATH").unwrap().into()
34 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
36 "## Running compile-fail tests in {} against miri for target {}",
40 let mut config = compiletest::Config::default().tempdir();
41 config.mode = "compile-fail".parse().expect("Invalid mode");
42 config.rustc_path = miri_path();
43 let mut flags = Vec::new();
44 if rustc_test_suite().is_some() {
45 config.run_lib_path = rustc_lib_path();
46 config.compile_lib_path = rustc_lib_path();
48 // if we are building as part of the rustc test suite, we already have fullmir for everything
49 if fullmir && rustc_test_suite().is_none() {
51 // skip fullmir on nonhost
54 let sysroot = dirs::home_dir().unwrap()
57 flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
58 config.src_base = PathBuf::from(path.to_string());
60 flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
61 config.src_base = PathBuf::from(path.to_string());
63 flags.push("-Zmir-emit-validate=1".to_owned());
64 config.target_rustcflags = Some(flags.join(" "));
65 config.target = target.to_owned();
66 compiletest::run_tests(&config);
69 fn run_pass(path: &str) {
70 eprintln!("## Running run-pass tests in {} against rustc", path);
71 let mut config = compiletest::Config::default().tempdir();
72 config.mode = "run-pass".parse().expect("Invalid mode");
73 config.src_base = PathBuf::from(path);
74 if let Some(rustc_path) = rustc_test_suite() {
75 config.rustc_path = rustc_path;
76 config.run_lib_path = rustc_lib_path();
77 config.compile_lib_path = rustc_lib_path();
78 config.target_rustcflags = Some(format!("-Dwarnings --sysroot {}", get_sysroot().display()));
80 config.target_rustcflags = Some("-Dwarnings".to_owned());
82 config.host_rustcflags = Some("-Dwarnings".to_string());
83 compiletest::run_tests(&config);
86 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
87 let opt_str = if opt { " with optimizations" } else { "" };
89 "## Running run-pass tests in {} against miri for target {}{}",
94 let mut config = compiletest::Config::default().tempdir();
95 config.mode = "ui".parse().expect("Invalid mode");
96 config.src_base = PathBuf::from(path);
97 config.target = target.to_owned();
98 config.host = host.to_owned();
99 config.rustc_path = miri_path();
100 if rustc_test_suite().is_some() {
101 config.run_lib_path = rustc_lib_path();
102 config.compile_lib_path = rustc_lib_path();
104 let mut flags = Vec::new();
105 // Control miri logging. This is okay despite concurrent test execution as all tests
106 // will set this env var to the same value.
107 env::set_var("MIRI_LOG", "warn");
108 // if we are building as part of the rustc test suite, we already have fullmir for everything
109 if fullmir && rustc_test_suite().is_none() {
111 // skip fullmir on nonhost
114 let sysroot = dirs::home_dir().unwrap()
118 flags.push("-Zmiri-start-fn".to_owned());
119 flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
122 flags.push("-Zmir-opt-level=3".to_owned());
124 flags.push("-Zmir-opt-level=0".to_owned());
125 // For now, only validate without optimizations. Inlining breaks validation.
126 flags.push("-Zmir-emit-validate=1".to_owned());
128 config.target_rustcflags = Some(flags.join(" "));
129 compiletest::run_tests(&config);
132 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
133 let mut path = path.into();
135 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
138 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
139 let target_dir = sysroot.join("lib").join("rustlib");
140 for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
141 let entry = entry.unwrap();
142 if !is_target_dir(entry.path()) {
145 let target = entry.file_name().into_string().unwrap();
150 fn get_sysroot() -> PathBuf {
151 let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
152 let sysroot = std::process::Command::new("rustc")
156 .expect("rustc not found")
158 String::from_utf8(sysroot).expect("sysroot is not utf8")
160 PathBuf::from(sysroot.trim())
163 fn get_host() -> String {
164 let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
165 println!("using rustc at {}", rustc.display());
166 let host = std::process::Command::new(rustc)
169 .expect("rustc not found for -vV")
171 let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
172 let host = host.split("\nhost: ").nth(1).expect(
173 "no host: part in rustc -vV",
175 let host = host.split('\n').next().expect("no \n after host");
179 fn run_pass_miri(opt: bool) {
180 let sysroot = get_sysroot();
181 let host = get_host();
183 for_all_targets(&sysroot, |target| {
184 miri_pass("tests/run-pass", &target, &host, false, opt);
186 miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
190 fn run_pass_miri_noopt() {
191 run_pass_miri(false);
196 // FIXME: Disabled for now, as the optimizer is pretty broken and crashes...
197 // See https://github.com/rust-lang/rust/issues/50411
198 fn run_pass_miri_opt() {
203 fn run_pass_rustc() {
204 run_pass("tests/run-pass");
205 run_pass("tests/run-pass-fullmir");
209 fn compile_fail_miri() {
210 let sysroot = get_sysroot();
211 let host = get_host();
213 // FIXME: run tests for other targets, too
214 compile_fail(&sysroot, "tests/compile-fail", &host, &host, true);
215 //compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);