Skip to content

Remove unnecessary clones #140232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
nnethercote committed Apr 24, 2025
commit af8047789dded51803be6cba1a708cddb1618020
15 changes: 7 additions & 8 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
/// # }
Expand All @@ -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))
Expand Down Expand Up @@ -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>>,
}
Expand All @@ -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()) {
Expand Down Expand Up @@ -409,7 +408,7 @@ macro_rules! run_driver {
}
}

StableMir::new($args, $callback).run()
StableMir::new($callback).run($args)
}};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_assoc_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_crate_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_def_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_item_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_trait_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/check_ty_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
26 changes: 14 additions & 12 deletions tests/ui-fulldeps/stable-mir/compilation-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/crate-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/smir_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/stable-mir/smir_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-fulldeps/stable-mir/smir_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading