]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
don't run all compile-fail tests with full MIR
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate dirs;
5
6 use std::slice::SliceConcatExt;
7 use std::path::{PathBuf, Path};
8 use std::io::Write;
9 use std::env;
10
11 macro_rules! eprintln {
12     ($($arg:tt)*) => {
13         let stderr = std::io::stderr();
14         writeln!(stderr.lock(), $($arg)*).unwrap();
15     }
16 }
17
18 fn miri_path() -> PathBuf {
19     if rustc_test_suite().is_some() {
20         PathBuf::from(option_env!("MIRI_PATH").unwrap())
21     } else {
22         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
23     }
24 }
25
26 fn rustc_test_suite() -> Option<PathBuf> {
27     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
28 }
29
30 fn rustc_lib_path() -> PathBuf {
31     option_env!("RUSTC_LIB_PATH").unwrap().into()
32 }
33
34 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
35     eprintln!(
36         "## Running compile-fail tests in {} against miri for target {}",
37         path,
38         target
39     );
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();
47     }
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() {
50         if host != target {
51             // skip fullmir on nonhost
52             return;
53         }
54         let sysroot = dirs::home_dir().unwrap()
55             .join(".xargo")
56             .join("HOST");
57         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
58         config.src_base = PathBuf::from(path.to_string());
59     } else {
60         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
61         config.src_base = PathBuf::from(path.to_string());
62     }
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);
67 }
68
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()));
79     } else {
80         config.target_rustcflags = Some("-Dwarnings".to_owned());
81     }
82     config.host_rustcflags = Some("-Dwarnings".to_string());
83     compiletest::run_tests(&config);
84 }
85
86 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
87     let opt_str = if opt { " with optimizations" } else { "" };
88     eprintln!(
89         "## Running run-pass tests in {} against miri for target {}{}",
90         path,
91         target,
92         opt_str
93     );
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();
103     }
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() {
110         if host != target {
111             // skip fullmir on nonhost
112             return;
113         }
114         let sysroot = dirs::home_dir().unwrap()
115             .join(".xargo")
116             .join("HOST");
117
118         flags.push("-Zmiri-start-fn".to_owned());
119         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
120     }
121     if opt {
122         flags.push("-Zmir-opt-level=3".to_owned());
123     } else {
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());
127     }
128     config.target_rustcflags = Some(flags.join(" "));
129     compiletest::run_tests(&config);
130 }
131
132 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
133     let mut path = path.into();
134     path.push("lib");
135     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
136 }
137
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()) {
143             continue;
144         }
145         let target = entry.file_name().into_string().unwrap();
146         f(target);
147     }
148 }
149
150 fn get_sysroot() -> PathBuf {
151     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
152         let sysroot = std::process::Command::new("rustc")
153             .arg("--print")
154             .arg("sysroot")
155             .output()
156             .expect("rustc not found")
157             .stdout;
158         String::from_utf8(sysroot).expect("sysroot is not utf8")
159     });
160     PathBuf::from(sysroot.trim())
161 }
162
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)
167         .arg("-vV")
168         .output()
169         .expect("rustc not found for -vV")
170         .stdout;
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",
174     );
175     let host = host.split('\n').next().expect("no \n after host");
176     String::from(host)
177 }
178
179 fn run_pass_miri(opt: bool) {
180     let sysroot = get_sysroot();
181     let host = get_host();
182
183     for_all_targets(&sysroot, |target| {
184         miri_pass("tests/run-pass", &target, &host, false, opt);
185     });
186     miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
187 }
188
189 #[test]
190 fn run_pass_miri_noopt() {
191     run_pass_miri(false);
192 }
193
194 #[test]
195 #[ignore]
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() {
199     run_pass_miri(true);
200 }
201
202 #[test]
203 fn run_pass_rustc() {
204     run_pass("tests/run-pass");
205     run_pass("tests/run-pass-fullmir");
206 }
207
208 #[test]
209 fn compile_fail_miri() {
210     let sysroot = get_sysroot();
211     let host = get_host();
212
213     // FIXME: run tests for other targets, too
214     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false);
215     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
216 }