Skip to content

Commit 96ac2b4

Browse files
committed
Use ui_tests
1 parent acf8fec commit 96ac2b4

File tree

6 files changed

+107
-38
lines changed

6 files changed

+107
-38
lines changed

.github/scripts/run_rustc_tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ fi
2323
pushd "${RUST_REPO}"
2424
SUITES=(
2525
# Match https://github.com/rust-lang/rust/blob/master/src/bootstrap/test.rs for now
26-
"tests/ui/cfg ui"
26+
"tests/ui/cfg yolo"
2727
)
2828
for suite_cfg in "${SUITES[@]}"; do
2929
# Hack to work on older bash like the ones on MacOS.
3030
suite_pair=($suite_cfg)
3131
suite=${suite_pair[0]}
3232
mode=${suite_pair[1]}
3333
echo "${suite_cfg} pair: $suite_pair mode: $mode"
34-
compiletest --driver-path="${TOOLS_BIN}/test-drive" --mode=${mode} --src-base="${suite}" --output-path "${RUST_REPO}/build"
34+
compiletest --driver-path="${TOOLS_BIN}/test-drive" --mode=${mode} --src-base="${suite}" --output-dir="${RUST_REPO}/build"
3535
done

tools/compiletest/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ version = "0.0.0"
55
edition = "2021"
66

77
[dependencies]
8-
compiletest_rs = { version = "0.10.0", features = [ "rustc" ] }
8+
ui_test = "0.20.0"
99
clap = { version = "4.1.3", features = ["derive"] }
1010

1111
[package.metadata.rust-analyzer]

tools/compiletest/src/args.rs

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
use compiletest_rs::Config;
1+
/// Create our own parser and build the Config from it.
22
use std::fmt::Debug;
33
use std::path::PathBuf;
4+
use ui_test::{CommandBuilder, Config, OutputConflictHandling, RustfixMode};
5+
6+
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
7+
/// Decides what is expected of each test's exit status.
8+
pub enum Mode {
9+
/// The test passes a full execution of the rustc driver
10+
Pass,
11+
/// The test produces an executable binary that can get executed on the host
12+
Run,
13+
/// The rustc driver panicked
14+
Panic,
15+
/// The rustc driver emitted an error
16+
Fail,
17+
/// Run the tests, but always pass them as long as all annotations are satisfied and stderr files match.
18+
Yolo,
19+
}
420

521
#[derive(Debug, clap::Parser)]
622
#[command(version, name = "compiletest")]
@@ -9,32 +25,46 @@ pub struct Args {
925
#[arg(long)]
1026
src_base: PathBuf,
1127

12-
/// The mode according to compiletest modes.
13-
#[arg(long)]
14-
mode: String,
28+
/// The mode according to ui_test modes.
29+
#[arg(long, default_value="yolo")]
30+
mode: Mode,
1531

1632
/// Path for the stable-mir driver.
1733
#[arg(long)]
1834
driver_path: PathBuf,
1935

2036
/// Path for where the output should be stored.
2137
#[arg(long)]
22-
output_path: PathBuf,
38+
output_dir: PathBuf,
2339

2440
#[arg(long)]
25-
verbose: bool,
41+
pub verbose: bool,
42+
}
43+
44+
impl From<Mode> for ui_test::Mode {
45+
/// Use rustc configuration as default but override arguments to fit our use case.
46+
fn from(mode: Mode) -> ui_test::Mode {
47+
match mode {
48+
Mode::Pass => { ui_test::Mode::Pass }
49+
Mode::Run => { ui_test::Mode::Run { exit_code: 0 }}
50+
Mode::Panic => { ui_test::Mode::Panic }
51+
Mode::Fail => { ui_test::Mode::Fail { require_patterns: false, rustfix: RustfixMode::Disabled }}
52+
Mode::Yolo => { ui_test::Mode::Yolo { rustfix: RustfixMode::Disabled }}
53+
}
54+
}
2655
}
2756

2857
impl From<Args> for Config {
58+
/// Use rustc configuration as default but override arguments to fit our use case.
2959
fn from(args: Args) -> Config {
30-
let mut config = Config::default();
31-
config.mode = args.mode.parse().expect("Invalid mode");
32-
config.src_base = args.src_base;
33-
config.rustc_path = args.driver_path;
34-
config.build_base = args.output_path;
35-
config.verbose = args.verbose;
36-
config.run_lib_path = PathBuf::from(env!("RUSTC_LIB_PATH"));
37-
config.link_deps();
60+
let mut config = Config::rustc(args.src_base);
61+
config.program = CommandBuilder::rustc();
62+
config.program.program = args.driver_path;
63+
config.program.args.push("--check-smir".into());
64+
config.mode = ui_test::Mode::from(args.mode);
65+
config.output_conflict_handling = OutputConflictHandling::Error("Should Fail".to_string());
66+
config.out_dir = args.output_dir;
67+
//config.run_lib_path = PathBuf::from(env!("RUSTC_LIB_PATH"));
3868
config
3969
}
4070
}

tools/compiletest/src/main.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
//! Run compiletest on a given folder.
22
33
mod args;
4+
5+
use std::process::ExitCode;
46
use clap::Parser;
5-
use compiletest_rs::Config;
7+
use ui_test::{status_emitter, Config};
68

7-
fn main() {
9+
fn main() -> ExitCode {
810
let args = args::Args::parse();
9-
println!("args: ${args:?}");
10-
let cfg = Config::from(args);
11-
compiletest_rs::run_tests(&cfg);
11+
let verbose = args.verbose;
12+
if verbose {
13+
println!("args: ${args:?}");
14+
}
15+
let config = Config::from(args);
16+
if verbose {
17+
println!("Compiler: {}", config.program.display());
18+
}
19+
20+
let name = config.root_dir.display().to_string();
21+
22+
let text = if verbose {
23+
status_emitter::Text::verbose()
24+
} else {
25+
status_emitter::Text::quiet()
26+
};
27+
28+
let result = ui_test::run_tests_generic(
29+
vec![config],
30+
ui_test::default_file_filter,
31+
ui_test::default_per_file_config,
32+
(text, status_emitter::Gha::<true> { name }),
33+
);
34+
if result.is_ok() { ExitCode::SUCCESS } else { ExitCode::FAILURE }
1235
}

tools/test-drive/src/main.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ extern crate rustc_middle;
1010
extern crate rustc_smir;
1111

1212
use rustc_middle::ty::TyCtxt;
13-
use rustc_smir::{rustc_internal, stable_mir};
13+
use rustc_smir::rustc_internal;
14+
use rustc_smir::stable_mir::CompilerError;
15+
use std::sync::atomic::{AtomicBool, Ordering};
16+
use std::ops::ControlFlow;
1417
use std::panic::{catch_unwind, AssertUnwindSafe};
1518
use std::process::ExitCode;
1619

1720
const CHECK_ARG: &str = "--check-smir";
21+
const VERBOSE_ARG: &str = "--verbose";
22+
// Use a static variable for simplicity.
23+
static VERBOSE: AtomicBool = AtomicBool::new(false);
1824

1925
type TestResult = Result<(), String>;
2026

@@ -32,12 +38,15 @@ fn main() -> ExitCode {
3238
!is_check_arg
3339
})
3440
.collect();
35-
36-
37-
let callback = if check_smir { test_stable_mir } else { |_: TyCtxt| ExitCode::SUCCESS };
38-
let result = rustc_internal::StableMir::new(args, callback).continue_compilation().run();
39-
if let Ok(test_result) = result {
40-
test_result
41+
VERBOSE.store(args.iter().any(|arg| &*arg == VERBOSE_ARG), Ordering::Relaxed);
42+
let callback = if check_smir {
43+
test_stable_mir
44+
} else {
45+
|_: TyCtxt| ControlFlow::<()>::Continue(())
46+
};
47+
let result = rustc_internal::StableMir::new(args, callback).run();
48+
if result.is_ok() || matches!(result, Err(CompilerError::Skipped)) {
49+
ExitCode::SUCCESS
4150
} else {
4251
ExitCode::FAILURE
4352
}
@@ -51,26 +60,32 @@ macro_rules! run_tests {
5160
};
5261
}
5362

63+
fn info(msg: String) {
64+
if VERBOSE.load(Ordering::Relaxed) {
65+
println!("{}", msg);
66+
}
67+
}
68+
5469
/// This function invoke other tests and process their results.
5570
/// Tests should avoid panic,
56-
fn test_stable_mir(tcx: TyCtxt<'_>) -> ExitCode {
71+
fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
5772
let results = run_tests![
5873
sanity_checks::test_entry_fn,
5974
sanity_checks::test_all_fns,
6075
sanity_checks::test_traits,
6176
sanity_checks::test_crates
6277
];
6378
let (success, failure): (Vec<_>, Vec<_>) = results.iter().partition(|r| r.is_ok());
64-
println!(
79+
info(format!(
6580
"Ran {} tests. {} succeeded. {} failed",
6681
results.len(),
6782
success.len(),
6883
failure.len()
69-
);
84+
));
7085
if failure.is_empty() {
71-
ExitCode::SUCCESS
86+
ControlFlow::<()>::Continue(())
7287
} else {
73-
ExitCode::FAILURE
88+
ControlFlow::<()>::Break(())
7489
}
7590
}
7691

@@ -79,10 +94,10 @@ fn run_test<F: FnOnce() -> TestResult>(name: &str, f: F) -> TestResult {
7994
Err(_) => Err("Panic: {}".to_string()),
8095
Ok(result) => result,
8196
};
82-
println!(
97+
info(format!(
8398
"Test {}: {}",
8499
name,
85100
result.as_ref().err().unwrap_or(&"Ok".to_string())
86-
);
101+
));
87102
result
88103
}

tools/test-drive/src/sanity_checks.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
//! These checks should only depend on StableMIR APIs. See other modules for tests that compare
55
//! the result between StableMIR and internal APIs.
66
use crate::TestResult;
7-
use rustc_middle::ty::TyCtxt;
87
use rustc_smir::stable_mir;
9-
use std::collections::HashSet;
108
use std::fmt::Debug;
119
use std::hint::black_box;
1210

@@ -36,6 +34,7 @@ pub fn check(val: bool, msg: String) -> TestResult {
3634
pub fn test_entry_fn() -> TestResult {
3735
let entry_fn = stable_mir::entry_fn();
3836
entry_fn.map_or(Ok(()), |entry_fn| {
37+
check_body(entry_fn.body());
3938
let all_items = stable_mir::all_local_items();
4039
check(
4140
all_items.contains(&entry_fn),
@@ -52,6 +51,8 @@ pub fn test_all_fns() -> TestResult {
5251
"Failed to find any local item".to_string(),
5352
)?;
5453

54+
// Not sure which API to use to make sure this is a function that has a body.
55+
#[cfg(skip)]
5556
for item in all_items {
5657
// Get body and iterate over items
5758
let body = item.body();

0 commit comments

Comments
 (0)