]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge pull request #488 from RalfJung/start-fn
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate colored;
5
6 use colored::*;
7
8 use std::slice::SliceConcatExt;
9 use std::path::{PathBuf, Path};
10 use std::io::Write;
11
12 macro_rules! eprintln {
13     ($($arg:tt)*) => {
14         let stderr = std::io::stderr();
15         writeln!(stderr.lock(), $($arg)*).unwrap();
16     }
17 }
18
19 fn miri_path() -> PathBuf {
20     if rustc_test_suite().is_some() {
21         PathBuf::from(option_env!("MIRI_PATH").unwrap())
22     } else {
23         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
24     }
25 }
26
27 fn rustc_test_suite() -> Option<PathBuf> {
28     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
29 }
30
31 fn rustc_lib_path() -> PathBuf {
32     option_env!("RUSTC_LIB_PATH").unwrap().into()
33 }
34
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()
38 }
39
40 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool) {
41     if need_fullmir && !have_fullmir() {
42         eprintln!("{}", format!(
43             "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
44             path,
45             target
46         ).yellow().bold());
47         return;
48     }
49
50     eprintln!("{}", format!(
51         "## Running compile-fail tests in {} against miri for target {}",
52         path,
53         target
54     ).green().bold());
55     let mut config = compiletest::Config::default().tempdir();
56     config.mode = "compile-fail".parse().expect("Invalid mode");
57     config.rustc_path = miri_path();
58     let mut flags = Vec::new();
59     if rustc_test_suite().is_some() {
60         config.run_lib_path = rustc_lib_path();
61         config.compile_lib_path = rustc_lib_path();
62     }
63     flags.push(format!("--sysroot {}", sysroot.display()));
64     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
65     config.src_base = PathBuf::from(path.to_string());
66     flags.push("-Zmir-emit-validate=1".to_owned());
67     config.target_rustcflags = Some(flags.join(" "));
68     config.target = target.to_owned();
69     config.host = host.to_owned();
70     compiletest::run_tests(&config);
71 }
72
73 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
74     if need_fullmir && !have_fullmir() {
75         eprintln!("{}", format!(
76             "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
77             path,
78             target
79         ).yellow().bold());
80         return;
81     }
82
83     let opt_str = if opt { " with optimizations" } else { "" };
84     eprintln!("{}", format!(
85         "## Running run-pass tests in {} against miri for target {}{}",
86         path,
87         target,
88         opt_str
89     ).green().bold());
90     let mut config = compiletest::Config::default().tempdir();
91     config.mode = "ui".parse().expect("Invalid mode");
92     config.src_base = PathBuf::from(path);
93     config.target = target.to_owned();
94     config.host = host.to_owned();
95     config.rustc_path = miri_path();
96     if rustc_test_suite().is_some() {
97         config.run_lib_path = rustc_lib_path();
98         config.compile_lib_path = rustc_lib_path();
99     }
100     let mut flags = Vec::new();
101     flags.push(format!("--sysroot {}", sysroot.display()));
102     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
103     if opt {
104         // FIXME: Using level 1 (instead of 3) for now, as the optimizer is pretty broken
105         // and crashes...
106         // Level 0 and 1 are not the same, so this still gives us *some* coverage.
107         // See https://github.com/rust-lang/rust/issues/50411
108         flags.push("-Zmir-opt-level=1".to_owned());
109     } else {
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());
113     }
114     config.target_rustcflags = Some(flags.join(" "));
115     compiletest::run_tests(&config);
116 }
117
118 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
119     let mut path = path.into();
120     path.push("lib");
121     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
122 }
123
124 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
125     let target_dir = sysroot.join("lib").join("rustlib");
126     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
127         let entry = entry.unwrap();
128         if !is_target_dir(entry.path()) {
129             continue;
130         }
131         let target = entry.file_name().into_string().unwrap();
132         f(target);
133     }
134 }
135
136 fn get_sysroot() -> PathBuf {
137     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
138         let sysroot = std::process::Command::new("rustc")
139             .arg("--print")
140             .arg("sysroot")
141             .output()
142             .expect("rustc not found")
143             .stdout;
144         String::from_utf8(sysroot).expect("sysroot is not utf8")
145     });
146     PathBuf::from(sysroot.trim())
147 }
148
149 fn get_host() -> String {
150     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
151     println!("using rustc at {}", rustc.display());
152     let host = std::process::Command::new(rustc)
153         .arg("-vV")
154         .output()
155         .expect("rustc not found for -vV")
156         .stdout;
157     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
158     let host = host.split("\nhost: ").nth(1).expect(
159         "no host: part in rustc -vV",
160     );
161     let host = host.split('\n').next().expect("no \n after host");
162     String::from(host)
163 }
164
165 fn run_pass_miri(opt: bool) {
166     let sysroot = get_sysroot();
167     let host = get_host();
168
169     for_all_targets(&sysroot, |target| {
170         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
171     });
172     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
173 }
174
175 fn compile_fail_miri() {
176     let sysroot = get_sysroot();
177     let host = get_host();
178
179     // FIXME: run tests for other targets, too
180     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false);
181     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
182 }
183
184 #[test]
185 fn test() {
186     // We put everything into a single test to avoid the parallelism `cargo test`
187     // introduces.  We still get parallelism within our tests because `compiletest`
188     // uses `libtest` which runs jobs in parallel.
189
190     run_pass_miri(false);
191     run_pass_miri(true);
192
193     compile_fail_miri();
194 }