Skip to content

Commit af80477

Browse files
committed
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.
1 parent 055a27d commit af80477

23 files changed

+43
-42
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ where
256256
/// // Your code goes in here.
257257
/// # ControlFlow::Continue(())
258258
/// }
259-
/// # let args = vec!["--verbose".to_string()];
259+
/// # let args = &["--verbose".to_string()];
260260
/// let result = run!(args, analyze_code);
261261
/// # assert_eq!(result, Err(CompilerError::Skipped))
262262
/// # }
@@ -278,7 +278,7 @@ where
278278
/// // Your code goes in here.
279279
/// # ControlFlow::Continue(())
280280
/// }
281-
/// # let args = vec!["--verbose".to_string()];
281+
/// # let args = &["--verbose".to_string()];
282282
/// # let extra_args = vec![];
283283
/// let result = run!(args, || analyze_code(extra_args));
284284
/// # assert_eq!(result, Err(CompilerError::Skipped))
@@ -340,7 +340,6 @@ macro_rules! run_driver {
340340
C: Send,
341341
F: FnOnce($(optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
342342
{
343-
args: Vec<String>,
344343
callback: Option<F>,
345344
result: Option<ControlFlow<B, C>>,
346345
}
@@ -352,14 +351,14 @@ macro_rules! run_driver {
352351
F: FnOnce($(optional!($with_tcx TyCtxt))?) -> ControlFlow<B, C> + Send,
353352
{
354353
/// Creates a new `StableMir` instance, with given test_function and arguments.
355-
pub fn new(args: Vec<String>, callback: F) -> Self {
356-
StableMir { args, callback: Some(callback), result: None }
354+
pub fn new(callback: F) -> Self {
355+
StableMir { callback: Some(callback), result: None }
357356
}
358357

359358
/// Runs the compiler against given target and tests it with `test_function`
360-
pub fn run(&mut self) -> Result<C, CompilerError<B>> {
359+
pub fn run(&mut self, args: &[String]) -> Result<C, CompilerError<B>> {
361360
let compiler_result = rustc_driver::catch_fatal_errors(|| -> interface::Result::<()> {
362-
run_compiler(&self.args.clone(), self);
361+
run_compiler(&args, self);
363362
Ok(())
364363
});
365364
match (compiler_result, self.result.take()) {
@@ -409,7 +408,7 @@ macro_rules! run_driver {
409408
}
410409
}
411410

412-
StableMir::new($args, $callback).run()
411+
StableMir::new($callback).run($args)
413412
}};
414413
}
415414

tests/ui-fulldeps/stable-mir/check_abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn get_item<'a>(
145145
fn main() {
146146
let path = "alloc_input.rs";
147147
generate_input(&path).unwrap();
148-
let args = vec![
148+
let args = &[
149149
"rustc".to_string(),
150150
"--crate-type=lib".to_string(),
151151
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_allocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn get_item<'a>(
219219
fn main() {
220220
let path = "alloc_input.rs";
221221
generate_input(&path).unwrap();
222-
let args = vec![
222+
let args = &[
223223
"rustc".to_string(),
224224
"--edition=2021".to_string(),
225225
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_assoc_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn check_items<T: CrateDef>(items: &[T], expected: &[&str]) {
8585
fn main() {
8686
let path = "assoc_items.rs";
8787
generate_input(&path).unwrap();
88-
let args = vec![
88+
let args = &[
8989
"rustc".to_string(),
9090
"--crate-type=lib".to_string(),
9191
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn get_item<'a>(
5757
fn main() {
5858
let path = "attribute_input.rs";
5959
generate_input(&path).unwrap();
60-
let args = vec![
60+
let args = &[
6161
"rustc".to_string(),
6262
"--crate-type=lib".to_string(),
6363
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_binop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'a> MirVisitor for Visitor<'a> {
8181
fn main() {
8282
let path = "binop_input.rs";
8383
generate_input(&path).unwrap();
84-
let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
84+
let args = &["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
8585
run!(args, test_binops).unwrap();
8686
}
8787

tests/ui-fulldeps/stable-mir/check_crate_defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn contains<T: CrateDef + std::fmt::Debug>(items: &[T], expected: &[&str]) {
8484
fn main() {
8585
let path = "crate_definitions.rs";
8686
generate_input(&path).unwrap();
87-
let args = vec![
87+
let args = &[
8888
"rustc".to_string(),
8989
"--crate-type=lib".to_string(),
9090
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_def_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn check_fn_def(ty: Ty) {
7676
fn main() {
7777
let path = "defs_ty_input.rs";
7878
generate_input(&path).unwrap();
79-
let args = vec![
79+
let args = &[
8080
"rustc".to_string(),
8181
"-Cpanic=abort".to_string(),
8282
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn get_instances(body: mir::Body) -> Vec<Instance> {
112112
fn main() {
113113
let path = "defs_input.rs";
114114
generate_input(&path).unwrap();
115-
let args = vec![
115+
let args = &[
116116
"rustc".to_string(),
117117
"-Cpanic=abort".to_string(),
118118
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_foreign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn test_foreign() -> ControlFlow<()> {
5858
fn main() {
5959
let path = "foreign_input.rs";
6060
generate_input(&path).unwrap();
61-
let args = vec![
61+
let args = &[
6262
"rustc".to_string(),
6363
"-Cpanic=abort".to_string(),
6464
"--crate-type=lib".to_string(),

tests/ui-fulldeps/stable-mir/check_instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn test_body(body: mir::Body) {
8787
fn main() {
8888
let path = "instance_input.rs";
8989
generate_input(&path).unwrap();
90-
let args = vec![
90+
let args = &[
9191
"rustc".to_string(),
9292
"-Cpanic=abort".to_string(),
9393
"--crate-type=lib".to_string(),

tests/ui-fulldeps/stable-mir/check_intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'a> MirVisitor for CallsVisitor<'a> {
115115
fn main() {
116116
let path = "binop_input.rs";
117117
generate_input(&path).unwrap();
118-
let args = vec!["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
118+
let args = &["rustc".to_string(), "--crate-type=lib".to_string(), path.to_string()];
119119
run!(args, test_intrinsics).unwrap();
120120
}
121121

tests/ui-fulldeps/stable-mir/check_item_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn test_item_kind() -> ControlFlow<()> {
4747
fn main() {
4848
let path = "item_kind_input.rs";
4949
generate_input(&path).unwrap();
50-
let args = vec![
50+
let args = &[
5151
"rustc".to_string(),
5252
"-Cpanic=abort".to_string(),
5353
"--crate-type=lib".to_string(),

tests/ui-fulldeps/stable-mir/check_normalization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn check_ty(ty: Ty) {
6161
fn main() {
6262
let path = "normalization_input.rs";
6363
generate_input(&path).unwrap();
64-
let args = vec![
64+
let args = &[
6565
"rustc".to_string(),
6666
"-Cpanic=abort".to_string(),
6767
"--crate-type=lib".to_string(),

tests/ui-fulldeps/stable-mir/check_trait_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn assert_impl(impl_names: &HashSet<String>, target: &str) {
7272
fn main() {
7373
let path = "trait_queries.rs";
7474
generate_input(&path).unwrap();
75-
let args = vec![
75+
let args = &[
7676
"rustc".to_string(),
7777
"--crate-type=lib".to_string(),
7878
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn get_item<'a>(
120120
fn main() {
121121
let path = "transform_input.rs";
122122
generate_input(&path).unwrap();
123-
let args = vec![
123+
let args = &[
124124
"rustc".to_string(),
125125
"--crate-type=lib".to_string(),
126126
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/check_ty_fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'a> MirVisitor for PlaceVisitor<'a> {
7878
fn main() {
7979
let path = "ty_fold_input.rs";
8080
generate_input(&path).unwrap();
81-
let args = vec![
81+
let args = &[
8282
"rustc".to_string(),
8383
"-Cpanic=abort".to_string(),
8484
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/compilation-result.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,42 @@ use std::io::Write;
2525
fn main() {
2626
let path = "input_compilation_result_test.rs";
2727
generate_input(&path).unwrap();
28-
let args = vec!["rustc".to_string(), path.to_string()];
29-
test_continue(args.clone());
30-
test_break(args.clone());
31-
test_failed(args.clone());
32-
test_skipped(args.clone());
28+
let args = &["rustc".to_string(), path.to_string()];
29+
test_continue(args);
30+
test_break(args);
31+
test_failed(args);
32+
test_skipped(args);
3333
test_captured(args)
3434
}
3535

36-
fn test_continue(args: Vec<String>) {
36+
fn test_continue(args: &[String]) {
3737
let result = run!(args, || ControlFlow::Continue::<(), bool>(true));
3838
assert_eq!(result, Ok(true));
3939
}
4040

41-
fn test_break(args: Vec<String>) {
41+
fn test_break(args: &[String]) {
4242
let result = run!(args, || ControlFlow::Break::<bool, i32>(false));
4343
assert_eq!(result, Err(stable_mir::CompilerError::Interrupted(false)));
4444
}
4545

4646
#[allow(unreachable_code)]
47-
fn test_skipped(mut args: Vec<String>) {
47+
fn test_skipped(args: &[String]) {
48+
let mut args = args.to_vec();
4849
args.push("--version".to_string());
49-
let result = run!(args, || unreachable!() as ControlFlow<()>);
50+
let result = run!(&args, || unreachable!() as ControlFlow<()>);
5051
assert_eq!(result, Err(stable_mir::CompilerError::Skipped));
5152
}
5253

5354
#[allow(unreachable_code)]
54-
fn test_failed(mut args: Vec<String>) {
55+
fn test_failed(args: &[String]) {
56+
let mut args = args.to_vec();
5557
args.push("--cfg=broken".to_string());
56-
let result = run!(args, || unreachable!() as ControlFlow<()>);
58+
let result = run!(&args, || unreachable!() as ControlFlow<()>);
5759
assert_eq!(result, Err(stable_mir::CompilerError::Failed));
5860
}
5961

6062
/// Test that we are able to pass a closure and set the return according to the captured value.
61-
fn test_captured(args: Vec<String>) {
63+
fn test_captured(args: &[String]) {
6264
let captured = "10".to_string();
6365
let result = run!(args, || ControlFlow::Continue::<(), usize>(captured.len()));
6466
assert_eq!(result, Ok(captured.len()));

tests/ui-fulldeps/stable-mir/crate-info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn get_item<'a>(
186186
fn main() {
187187
let path = "input.rs";
188188
generate_input(&path).unwrap();
189-
let args = vec![
189+
let args = &[
190190
"rustc".to_string(),
191191
"--crate-type=lib".to_string(),
192192
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/projections.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn get_item<'a>(
146146
fn main() {
147147
let path = "input.rs";
148148
generate_input(&path).unwrap();
149-
let args = vec![
149+
let args = &[
150150
"rustc".to_string(),
151151
"--crate-type=lib".to_string(),
152152
"--crate-name".to_string(),

tests/ui-fulldeps/stable-mir/smir_internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn test_translation(tcx: TyCtxt<'_>) -> ControlFlow<()> {
4040
fn main() {
4141
let path = "internal_input.rs";
4242
generate_input(&path).unwrap();
43-
let args = vec![
43+
let args = &[
4444
"rustc".to_string(),
4545
"--crate-name".to_string(),
4646
CRATE_NAME.to_string(),

tests/ui-fulldeps/stable-mir/smir_serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn serialize_to_json(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
4646
fn main() {
4747
let path = "internal_input.rs";
4848
generate_input(&path).unwrap();
49-
let args = vec![
49+
let args = &[
5050
"rustc".to_string(),
5151
"--crate-name".to_string(),
5252
CRATE_NAME.to_string(),

tests/ui-fulldeps/stable-mir/smir_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ impl mir::MutMirVisitor for TestMutVisitor {
183183
fn main() {
184184
let path = "sim_visitor_input.rs";
185185
generate_input(&path).unwrap();
186-
let args = vec![
186+
let args = &[
187187
"rustc".to_string(),
188188
"-Cpanic=abort".to_string(),
189189
"--crate-name".to_string(),
190190
CRATE_NAME.to_string(),
191191
path.to_string(),
192192
];
193-
run!(args.clone(), test_visitor).unwrap();
193+
run!(args, test_visitor).unwrap();
194194
run!(args, test_mut_visitor).unwrap();
195195
}
196196

0 commit comments

Comments
 (0)