]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
run all run-pass tests with intrptrcast. makes many of them fail!
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext, custom_test_frameworks)]
2 // Custom test runner, to avoid libtest being wrapped around compiletest which wraps libtest.
3 #![test_runner(test_runner)]
4
5 use std::slice::SliceConcatExt;
6 use std::path::PathBuf;
7 use std::env;
8
9 use compiletest_rs as compiletest;
10 use colored::*;
11
12 fn miri_path() -> PathBuf {
13     if rustc_test_suite().is_some() {
14         PathBuf::from(option_env!("MIRI_PATH").unwrap())
15     } else {
16         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
17     }
18 }
19
20 fn rustc_test_suite() -> Option<PathBuf> {
21     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
22 }
23
24 fn rustc_lib_path() -> PathBuf {
25     option_env!("RUSTC_LIB_PATH").unwrap().into()
26 }
27
28 fn run_tests(mode: &str, path: &str, target: &str, mut flags: Vec<String>) {
29     // Some flags we always want.
30     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
31     flags.push("--edition 2018".to_owned());
32     if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
33         flags.push(format!("--sysroot {}", sysroot));
34     }
35
36     // The rest of the configuration.
37     let mut config = compiletest::Config::default().tempdir();
38     config.mode = mode.parse().expect("Invalid mode");
39     config.rustc_path = miri_path();
40     if rustc_test_suite().is_some() {
41         config.run_lib_path = rustc_lib_path();
42         config.compile_lib_path = rustc_lib_path();
43     }
44     config.filter = env::args().nth(1);
45     config.host = get_host();
46     config.src_base = PathBuf::from(path);
47     config.target = target.to_owned();
48     config.target_rustcflags = Some(flags.join(" "));
49     compiletest::run_tests(&config);
50 }
51
52 fn compile_fail(path: &str, target: &str, opt: bool) {
53     let opt_str = if opt { " with optimizations" } else { "" };
54     eprintln!("{}", format!(
55         "## Running compile-fail tests in {} against miri for target {}{}",
56         path,
57         target,
58         opt_str
59     ).green().bold());
60
61     let mut flags = Vec::new();
62     if opt {
63         // Optimizing too aggressivley makes UB detection harder, but test at least
64         // the default value.
65         // FIXME: Opt level 3 ICEs during stack trace generation.
66         flags.push("-Zmir-opt-level=1".to_owned());
67     }
68
69     run_tests("compile-fail", path, target, flags);
70 }
71
72 fn miri_pass(path: &str, target: &str, opt: bool) {
73     let opt_str = if opt { " with optimizations" } else { "" };
74     eprintln!("{}", format!(
75         "## Running run-pass tests in {} against miri for target {}{}",
76         path,
77         target,
78         opt_str
79     ).green().bold());
80
81     let mut flags = Vec::new();
82     if opt {
83         flags.push("-Zmir-opt-level=3".to_owned());
84     } else {
85         // Run with intptrcast.  Avoid test matrix explosion by doing either this or opt-level=3.
86         flags.push("-Zmiri-seed=".to_owned());
87     }
88
89     run_tests("ui", path, target, flags);
90 }
91
92 fn get_host() -> String {
93     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
94     let rustc_version = std::process::Command::new(rustc)
95         .arg("-vV")
96         .output()
97         .expect("rustc not found for -vV")
98         .stdout;
99     let rustc_version = std::str::from_utf8(&rustc_version).expect("rustc -vV is not utf8");
100     let version_meta = rustc_version::version_meta_for(&rustc_version)
101         .expect("failed to parse rustc version info");
102     version_meta.host
103 }
104
105 fn get_target() -> String {
106     std::env::var("MIRI_TEST_TARGET").unwrap_or_else(|_| get_host())
107 }
108
109 fn run_pass_miri(opt: bool) {
110     miri_pass("tests/run-pass", &get_target(), opt);
111 }
112
113 fn compile_fail_miri(opt: bool) {
114     compile_fail("tests/compile-fail", &get_target(), opt);
115 }
116
117 fn test_runner(_tests: &[&()]) {
118     run_pass_miri(false);
119     run_pass_miri(true);
120
121     compile_fail_miri(false);
122     compile_fail_miri(true);
123 }