Refactor StableMir to avoid some clones.

Pass `args` to `run` instead of storing it in a field. This avoids the
need to clone it within `run`.

Also, change `args` from `Vec<String>` to `&[String]`, avoiding the need
for some vecs and clones.
This commit is contained in:
Nicholas Nethercote
2025-04-24 08:50:11 +10:00
parent 055a27da2a
commit af8047789d
23 changed files with 43 additions and 42 deletions

View File

@@ -256,7 +256,7 @@ where
/// // Your code goes in here.
/// # ControlFlow::Continue(())
/// }
/// # let args = vec!["--verbose".to_string()];
/// # let args = &["--verbose".to_string()];
/// let result = run!(args, analyze_code);
/// # assert_eq!(result, Err(CompilerError::Skipped))
/// # }
@@ -278,7 +278,7 @@ where
/// // Your code goes in here.
/// # ControlFlow::Continue(())
/// }
/// # let args = vec!["--verbose".to_string()];
/// # let args = &["--verbose".to_string()];
/// # let extra_args = vec![];
/// let result = run!(args, || analyze_code(extra_args));
/// # assert_eq!(result, Err(CompilerError::Skipped))
@@ -340,7 +340,6 @@ macro_rules! run_driver {
C: Send,
F: FnOnce($(optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
{
args: Vec<String>,
callback: Option<F>,
result: Option<ControlFlow<B, C>>,
}
@@ -352,14 +351,14 @@ macro_rules! run_driver {
F: FnOnce($(optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
{
/// Creates a new `StableMir` instance, with given test_function and arguments.
pub fn new(args: Vec<String>, callback: F) -> Self {
StableMir { args, callback: Some(callback), result: None }
pub fn new(callback: F) -> Self {
StableMir { callback: Some(callback), result: None }
}
/// Runs the compiler against given target and tests it with `test_function`
pub fn run(&mut self) -> Result<C, CompilerError<B>> {
pub fn run(&mut self, args: &[String]) -> Result<C, CompilerError<B>> {
let compiler_result = rustc_driver::catch_fatal_errors(|| -> interface::Result::<()> {
run_compiler(&self.args.clone(), self);
run_compiler(&args, self);
Ok(())
});
match (compiler_result, self.result.take()) {
@@ -409,7 +408,7 @@ macro_rules! run_driver {
}
}
StableMir::new($args, $callback).run()
StableMir::new($callback).run($args)
}};
}

View File

@@ -145,7 +145,7 @@ fn get_item<'a>(
fn main() {
let path = "alloc_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -219,7 +219,7 @@ fn get_item<'a>(
fn main() {
let path = "alloc_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--edition=2021".to_string(),
"--crate-name".to_string(),

View File

@@ -85,7 +85,7 @@ fn check_items<T: CrateDef>(items: &[T], expected: &[&str]) {
fn main() {
let path = "assoc_items.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -57,7 +57,7 @@ fn get_item<'a>(
fn main() {
let path = "attribute_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -81,7 +81,7 @@ impl<'a> MirVisitor for Visitor<'a> {
fn main() {
let path = "binop_input.rs";
generate_input(&path).unwrap();
let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
let args = &["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
run!(args, test_binops).unwrap();
}

View File

@@ -84,7 +84,7 @@ fn contains<T: CrateDef + std::fmt::Debug>(items: &[T], expected: &[&str]) {
fn main() {
let path = "crate_definitions.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -76,7 +76,7 @@ fn check_fn_def(ty: Ty) {
fn main() {
let path = "defs_ty_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-name".to_string(),

View File

@@ -112,7 +112,7 @@ fn get_instances(body: mir::Body) -> Vec<Instance> {
fn main() {
let path = "defs_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-name".to_string(),

View File

@@ -58,7 +58,7 @@ fn test_foreign() -> ControlFlow<()> {
fn main() {
let path = "foreign_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-type=lib".to_string(),

View File

@@ -87,7 +87,7 @@ fn test_body(body: mir::Body) {
fn main() {
let path = "instance_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-type=lib".to_string(),

View File

@@ -115,7 +115,7 @@ impl<'a> MirVisitor for CallsVisitor<'a> {
fn main() {
let path = "binop_input.rs";
generate_input(&path).unwrap();
let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
let args = &["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
run!(args, test_intrinsics).unwrap();
}

View File

@@ -47,7 +47,7 @@ fn test_item_kind() -> ControlFlow<()> {
fn main() {
let path = "item_kind_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-type=lib".to_string(),

View File

@@ -61,7 +61,7 @@ fn check_ty(ty: Ty) {
fn main() {
let path = "normalization_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-type=lib".to_string(),

View File

@@ -72,7 +72,7 @@ fn assert_impl(impl_names: &HashSet<String>, target: &str) {
fn main() {
let path = "trait_queries.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -120,7 +120,7 @@ fn get_item<'a>(
fn main() {
let path = "transform_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -78,7 +78,7 @@ impl<'a> MirVisitor for PlaceVisitor<'a> {
fn main() {
let path = "ty_fold_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-name".to_string(),

View File

@@ -25,40 +25,42 @@ use std::io::Write;
fn main() {
let path = "input_compilation_result_test.rs";
generate_input(&path).unwrap();
let args = vec!["rustc".to_string(), path.to_string()];
test_continue(args.clone());
test_break(args.clone());
test_failed(args.clone());
test_skipped(args.clone());
let args = &["rustc".to_string(), path.to_string()];
test_continue(args);
test_break(args);
test_failed(args);
test_skipped(args);
test_captured(args)
}
fn test_continue(args: Vec<String>) {
fn test_continue(args: &[String]) {
let result = run!(args, || ControlFlow::Continue::<(), bool>(true));
assert_eq!(result, Ok(true));
}
fn test_break(args: Vec<String>) {
fn test_break(args: &[String]) {
let result = run!(args, || ControlFlow::Break::<bool, i32>(false));
assert_eq!(result, Err(stable_mir::CompilerError::Interrupted(false)));
}
#[allow(unreachable_code)]
fn test_skipped(mut args: Vec<String>) {
fn test_skipped(args: &[String]) {
let mut args = args.to_vec();
args.push("--version".to_string());
let result = run!(args, || unreachable!() as ControlFlow<()>);
let result = run!(&args, || unreachable!() as ControlFlow<()>);
assert_eq!(result, Err(stable_mir::CompilerError::Skipped));
}
#[allow(unreachable_code)]
fn test_failed(mut args: Vec<String>) {
fn test_failed(args: &[String]) {
let mut args = args.to_vec();
args.push("--cfg=broken".to_string());
let result = run!(args, || unreachable!() as ControlFlow<()>);
let result = run!(&args, || unreachable!() as ControlFlow<()>);
assert_eq!(result, Err(stable_mir::CompilerError::Failed));
}
/// Test that we are able to pass a closure and set the return according to the captured value.
fn test_captured(args: Vec<String>) {
fn test_captured(args: &[String]) {
let captured = "10".to_string();
let result = run!(args, || ControlFlow::Continue::<(), usize>(captured.len()));
assert_eq!(result, Ok(captured.len()));

View File

@@ -186,7 +186,7 @@ fn get_item<'a>(
fn main() {
let path = "input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -146,7 +146,7 @@ fn get_item<'a>(
fn main() {
let path = "input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-type=lib".to_string(),
"--crate-name".to_string(),

View File

@@ -40,7 +40,7 @@ fn test_translation(tcx: TyCtxt<'_>) -> ControlFlow<()> {
fn main() {
let path = "internal_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-name".to_string(),
CRATE_NAME.to_string(),

View File

@@ -46,7 +46,7 @@ fn serialize_to_json(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
fn main() {
let path = "internal_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"--crate-name".to_string(),
CRATE_NAME.to_string(),

View File

@@ -183,14 +183,14 @@ impl mir::MutMirVisitor for TestMutVisitor {
fn main() {
let path = "sim_visitor_input.rs";
generate_input(&path).unwrap();
let args = vec![
let args = &[
"rustc".to_string(),
"-Cpanic=abort".to_string(),
"--crate-name".to_string(),
CRATE_NAME.to_string(),
path.to_string(),
];
run!(args.clone(), test_visitor).unwrap();
run!(args, test_visitor).unwrap();
run!(args, test_mut_visitor).unwrap();
}