Skip to content

Commit 79611d9

Browse files
committed
Auto merge of #122551 - RayMuir:copy_fmt, r=saethlin
Added "copy" to Debug fmt for copy operands In MIR's debug mode (--emit mir) the printing for Operands is slightly inconsistent. The RValues - values on the right side of an Assign - are usually printed with their Operand when they are Places. Example: _2 = move _3 But for arguments, the operand is omitted. _2 = _1 I propose a change be made, to display the place with the operand. _2 = copy _1 Move and copy have different semantics, meaning this difference is important and helpful to the user. It also adds consistency to the pretty printing. -- EDIT -- Consider this example Rust program and its MIR output with the **updated pretty printer.** This was generated with the arguments --emit mir --crate-type lib -Zmir-opt-level=0 (Otherwise, it's optimised away since it's a junk program). ```rust fn main(foo: i32) { let v = 10; if v == 20 { foo; } else { v; } } ``` ```MIR // WARNING: This output format is intended for human consumers only // and is subject to change without notice. Knock yourself out. fn main(_1: i32) -> () { debug foo => _1; let mut _0: (); let _2: i32; let mut _3: bool; let mut _4: i32; let _5: i32; let _6: i32; scope 1 { debug v => _2; } bb0: { StorageLive(_2); _2 = const 10_i32; StorageLive(_3); StorageLive(_4); _4 = copy _2; _3 = Eq(move _4, const 20_i32); switchInt(move _3) -> [0: bb2, otherwise: bb1]; } bb1: { StorageDead(_4); StorageLive(_5); _5 = copy _1; StorageDead(_5); _0 = const (); goto -> bb3; } bb2: { StorageDead(_4); StorageLive(_6); _6 = copy _2; StorageDead(_6); _0 = const (); goto -> bb3; } bb3: { StorageDead(_3); StorageDead(_2); return; } } ``` In this example program, we can see that when we move a place, it is preceded by "move". e.g. ``` _3 = Eq(move _4, const 20_i32);```. However, when we copy a place such as ```_5 = _1;```, it is not preceded by the operand in the original printout. I propose to change the print to include the copy ```_5 = copy _1``` as in this example. Regarding the arguments part. When I originally submitted this PR, I was under the impression this only affected the print for arguments to a function, but actually, it affects anything that uses a copy. This is preferable anyway with regard to consistency. The PR is about making ```copy``` explicit.
2 parents 636d7ff + b9cffa7 commit 79611d9

File tree

725 files changed

+3926
-3924
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

725 files changed

+3926
-3924
lines changed

Diff for: .git-blame-ignore-revs

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ b2d2184edea578109a48ec3d8decbee5948e8f35
2525
ec2cc761bc7067712ecc7734502f703fe3b024c8
2626
# format use declarations
2727
84ac80f1921afc243d71fd0caaa4f2838c294102
28+
# bless mir-opt tests to add `copy`
29+
99cb0c6bc399fb94a0ddde7e9b38e9c00d523bad

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ impl<'tcx> Debug for Operand<'tcx> {
11591159
use self::Operand::*;
11601160
match *self {
11611161
Constant(ref a) => write!(fmt, "{a:?}"),
1162-
Copy(ref place) => write!(fmt, "{place:?}"),
1162+
Copy(ref place) => write!(fmt, "copy {place:?}"),
11631163
Move(ref place) => write!(fmt, "move {place:?}"),
11641164
}
11651165
}

Diff for: tests/mir-opt/address_of.address_of_reborrow.SimplifyCfg-initial.after.mir

+6-6
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn address_of_reborrow() -> () {
140140
StorageLive(_6);
141141
_6 = &raw const (*_1);
142142
AscribeUserType(_6, o, UserTypeProjection { base: UserType(0), projs: [] });
143-
_5 = _6;
143+
_5 = copy _6;
144144
StorageDead(_6);
145145
StorageDead(_5);
146146
StorageLive(_7);
@@ -153,7 +153,7 @@ fn address_of_reborrow() -> () {
153153
_9 = move _10 as *const dyn std::marker::Send (PointerCoercion(Unsize));
154154
StorageDead(_10);
155155
AscribeUserType(_9, o, UserTypeProjection { base: UserType(1), projs: [] });
156-
_8 = _9;
156+
_8 = copy _9;
157157
StorageDead(_9);
158158
StorageDead(_8);
159159
StorageLive(_11);
@@ -194,7 +194,7 @@ fn address_of_reborrow() -> () {
194194
StorageLive(_22);
195195
_22 = &raw const (*_3);
196196
AscribeUserType(_22, o, UserTypeProjection { base: UserType(10), projs: [] });
197-
_21 = _22;
197+
_21 = copy _22;
198198
StorageDead(_22);
199199
StorageDead(_21);
200200
StorageLive(_23);
@@ -207,7 +207,7 @@ fn address_of_reborrow() -> () {
207207
_25 = move _26 as *const dyn std::marker::Send (PointerCoercion(Unsize));
208208
StorageDead(_26);
209209
AscribeUserType(_25, o, UserTypeProjection { base: UserType(11), projs: [] });
210-
_24 = _25;
210+
_24 = copy _25;
211211
StorageDead(_25);
212212
StorageDead(_24);
213213
StorageLive(_27);
@@ -242,7 +242,7 @@ fn address_of_reborrow() -> () {
242242
StorageLive(_36);
243243
_36 = &raw mut (*_3);
244244
AscribeUserType(_36, o, UserTypeProjection { base: UserType(20), projs: [] });
245-
_35 = _36;
245+
_35 = copy _36;
246246
StorageDead(_36);
247247
StorageDead(_35);
248248
StorageLive(_37);
@@ -255,7 +255,7 @@ fn address_of_reborrow() -> () {
255255
_39 = move _40 as *mut dyn std::marker::Send (PointerCoercion(Unsize));
256256
StorageDead(_40);
257257
AscribeUserType(_39, o, UserTypeProjection { base: UserType(21), projs: [] });
258-
_38 = _39;
258+
_38 = copy _39;
259259
StorageDead(_39);
260260
StorageDead(_38);
261261
StorageLive(_41);

Diff for: tests/mir-opt/array_index_is_temporary.main.SimplifyCfg-pre-optimizations.after.panic-abort.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ fn main() -> () {
3333
StorageDead(_4);
3434
StorageLive(_5);
3535
StorageLive(_6);
36-
_6 = _3;
36+
_6 = copy _3;
3737
_5 = foo(move _6) -> [return: bb1, unwind unreachable];
3838
}
3939

4040
bb1: {
4141
StorageDead(_6);
4242
StorageLive(_7);
43-
_7 = _2;
43+
_7 = copy _2;
4444
_8 = Len(_1);
45-
_9 = Lt(_7, _8);
46-
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> [success: bb2, unwind unreachable];
45+
_9 = Lt(copy _7, copy _8);
46+
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, copy _7) -> [success: bb2, unwind unreachable];
4747
}
4848

4949
bb2: {

Diff for: tests/mir-opt/array_index_is_temporary.main.SimplifyCfg-pre-optimizations.after.panic-unwind.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ fn main() -> () {
3333
StorageDead(_4);
3434
StorageLive(_5);
3535
StorageLive(_6);
36-
_6 = _3;
36+
_6 = copy _3;
3737
_5 = foo(move _6) -> [return: bb1, unwind continue];
3838
}
3939

4040
bb1: {
4141
StorageDead(_6);
4242
StorageLive(_7);
43-
_7 = _2;
43+
_7 = copy _2;
4444
_8 = Len(_1);
45-
_9 = Lt(_7, _8);
46-
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> [success: bb2, unwind continue];
45+
_9 = Lt(copy _7, copy _8);
46+
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, copy _7) -> [success: bb2, unwind continue];
4747
}
4848

4949
bb2: {

Diff for: tests/mir-opt/array_index_is_temporary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
// CHECK: debug x => [[x:_.*]];
1616
// CHECK: debug y => [[y:_.*]];
1717
// CHECK: [[y]] = const 1_usize;
18-
// CHECK: [[tmp:_.*]] = [[y]];
18+
// CHECK: [[tmp:_.*]] = copy [[y]];
1919
// CHECK: [[x]][[[tmp]]] =
2020
let mut x = [42, 43, 44];
2121
let mut y = 1;

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#0}-{closure#0}.coroutine_by_move.0.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ yields ()
2222

2323
bb0: {
2424
StorageLive(_3);
25-
_3 = (_1.0: i32);
25+
_3 = copy (_1.0: i32);
2626
FakeRead(ForLet(None), _3);
2727
StorageLive(_4);
2828
_4 = &_3;

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#0}-{closure#0}.coroutine_by_move.0.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ yields ()
2222

2323
bb0: {
2424
StorageLive(_3);
25-
_3 = (_1.0: i32);
25+
_3 = copy (_1.0: i32);
2626
FakeRead(ForLet(None), _3);
2727
StorageLive(_4);
2828
_4 = &_3;

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#1}-{closure#0}.coroutine_by_move.0.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ yields ()
2222

2323
bb0: {
2424
StorageLive(_3);
25-
_3 = (_1.0: i32);
25+
_3 = copy (_1.0: i32);
2626
FakeRead(ForLet(None), _3);
2727
StorageLive(_4);
2828
_4 = &_3;

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#1}-{closure#0}.coroutine_by_move.0.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ yields ()
2222

2323
bb0: {
2424
StorageLive(_3);
25-
_3 = (_1.0: i32);
25+
_3 = copy (_1.0: i32);
2626
FakeRead(ForLet(None), _3);
2727
StorageLive(_4);
2828
_4 = &_3;

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#1}.coroutine_closure_by_ref.0.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main::{closure#0}::{closure#1}(_1: &{async closure@$DIR/async_closure_shims.r
44
let mut _0: {async closure body@$DIR/async_closure_shims.rs:62:48: 65:10};
55

66
bb0: {
7-
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: ((*_1).0: &i32) };
7+
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: copy ((*_1).0: &i32) };
88
return;
99
}
1010
}

Diff for: tests/mir-opt/async_closure_shims.main-{closure#0}-{closure#1}.coroutine_closure_by_ref.0.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main::{closure#0}::{closure#1}(_1: &{async closure@$DIR/async_closure_shims.r
44
let mut _0: {async closure body@$DIR/async_closure_shims.rs:62:48: 65:10};
55

66
bb0: {
7-
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: ((*_1).0: &i32) };
7+
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: copy ((*_1).0: &i32) };
88
return;
99
}
1010
}

Diff for: tests/mir-opt/basic_assignment.main.ElaborateDrops.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
_1 = const false;
2828
StorageLive(_2);
2929
StorageLive(_3);
30-
_3 = _1;
30+
_3 = copy _1;
3131
_2 = move _3;
3232
StorageDead(_3);
3333
StorageLive(_4);

Diff for: tests/mir-opt/basic_assignment.main.SimplifyCfg-initial.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() -> () {
3131
FakeRead(ForLet(None), _1);
3232
StorageLive(_2);
3333
StorageLive(_3);
34-
_3 = _1;
34+
_3 = copy _1;
3535
_2 = move _3;
3636
StorageDead(_3);
3737
StorageLive(_4);

Diff for: tests/mir-opt/box_expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
fn main() {
88
// CHECK-LABEL: fn main(
99
// CHECK: [[box:_.*]] = ShallowInitBox(
10-
// CHECK: [[ptr:_.*]] = ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S);
10+
// CHECK: [[ptr:_.*]] = copy ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S);
1111
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
1212
// CHECK: [[ret]]: {
1313
// CHECK: [[box2:_.*]] = move [[box]];

Diff for: tests/mir-opt/building/async_await.b-{closure#0}.coroutine_resume.0.mir

+6-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
151151
StorageLive(_13);
152152
StorageLive(_14);
153153
StorageLive(_15);
154-
_15 = _38;
154+
_15 = copy _38;
155155
_14 = move _15;
156156
goto -> bb6;
157157
}
@@ -194,8 +194,8 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
194194

195195
bb10: {
196196
StorageLive(_17);
197-
_17 = ((_9 as Ready).0: ());
198-
_3 = _17;
197+
_17 = copy ((_9 as Ready).0: ());
198+
_3 = copy _17;
199199
StorageDead(_17);
200200
StorageDead(_14);
201201
StorageDead(_12);
@@ -253,7 +253,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
253253
StorageLive(_29);
254254
StorageLive(_30);
255255
StorageLive(_31);
256-
_31 = _38;
256+
_31 = copy _38;
257257
_30 = move _31;
258258
goto -> bb18;
259259
}
@@ -291,8 +291,8 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
291291

292292
bb21: {
293293
StorageLive(_33);
294-
_33 = ((_25 as Ready).0: ());
295-
_37 = _33;
294+
_33 = copy ((_25 as Ready).0: ());
295+
_37 = copy _33;
296296
StorageDead(_33);
297297
StorageDead(_30);
298298
StorageDead(_28);

Diff for: tests/mir-opt/building/custom/aggregate_exprs.adt.built.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ fn adt() -> Onion {
99
bb0: {
1010
_1 = const 1_i32;
1111
_2 = Foo { a: const 1_i32, b: const 2_i32 };
12-
_3 = Bar::Foo(move _2, _1);
13-
_0 = Onion { neon: ((_3 as variant#0).1: i32) };
12+
_3 = Bar::Foo(move _2, copy _1);
13+
_0 = Onion { neon: copy ((_3 as variant#0).1: i32) };
1414
return;
1515
}
1616
}

Diff for: tests/mir-opt/building/custom/aggregate_exprs.array.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn array() -> [i32; 2] {
88
bb0: {
99
_1 = [const 42_i32, const 43_i32];
1010
_2 = const 1_i32;
11-
_1 = [_2, const 2_i32];
11+
_1 = [copy _2, const 2_i32];
1212
_0 = move _1;
1313
return;
1414
}

Diff for: tests/mir-opt/building/custom/arbitrary_let.arbitrary_let.built.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ fn arbitrary_let(_1: i32) -> i32 {
66
let mut _3: i32;
77

88
bb0: {
9-
_2 = _1;
9+
_2 = copy _1;
1010
goto -> bb2;
1111
}
1212

1313
bb1: {
14-
_0 = _3;
14+
_0 = copy _3;
1515
return;
1616
}
1717

1818
bb2: {
19-
_3 = _2;
19+
_3 = copy _2;
2020
goto -> bb1;
2121
}
2222
}

Diff for: tests/mir-opt/building/custom/arrays.arrays.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn arrays() -> usize {
88
bb0: {
99
_1 = [const 5_i32; C];
1010
_2 = Len(_1);
11-
_0 = _2;
11+
_0 = copy _2;
1212
return;
1313
}
1414
}

Diff for: tests/mir-opt/building/custom/as_cast.float_to_int.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn float_to_int(_1: f32) -> i32 {
44
let mut _0: i32;
55

66
bb0: {
7-
_0 = _1 as i32 (FloatToInt);
7+
_0 = copy _1 as i32 (FloatToInt);
88
return;
99
}
1010
}

Diff for: tests/mir-opt/building/custom/as_cast.int_to_int.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn int_to_int(_1: u32) -> i32 {
44
let mut _0: i32;
55

66
bb0: {
7-
_0 = _1 as i32 (IntToInt);
7+
_0 = copy _1 as i32 (IntToInt);
88
return;
99
}
1010
}

Diff for: tests/mir-opt/building/custom/as_cast.int_to_ptr.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn int_to_ptr(_1: usize) -> *const i32 {
44
let mut _0: *const i32;
55

66
bb0: {
7-
_0 = _1 as *const i32 (PointerWithExposedProvenance);
7+
_0 = copy _1 as *const i32 (PointerWithExposedProvenance);
88
return;
99
}
1010
}

Diff for: tests/mir-opt/building/custom/assume.assume_local.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn assume_local(_1: bool) -> () {
44
let mut _0: ();
55

66
bb0: {
7-
assume(_1);
7+
assume(copy _1);
88
return;
99
}
1010
}

Diff for: tests/mir-opt/building/custom/assume.assume_place.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn assume_place(_1: (bool, u8)) -> () {
44
let mut _0: ();
55

66
bb0: {
7-
assume((_1.0: bool));
7+
assume(copy (_1.0: bool));
88
return;
99
}
1010
}

Diff for: tests/mir-opt/building/custom/enums.switch_bool.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn switch_bool(_1: bool) -> u32 {
44
let mut _0: u32;
55

66
bb0: {
7-
switchInt(_1) -> [1: bb1, 0: bb2, otherwise: bb2];
7+
switchInt(copy _1) -> [1: bb1, 0: bb2, otherwise: bb2];
88
}
99

1010
bb1: {

Diff for: tests/mir-opt/building/custom/enums.switch_option.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn switch_option(_1: Option<()>) -> bool {
66

77
bb0: {
88
_2 = discriminant(_1);
9-
switchInt(_2) -> [0: bb1, 1: bb2, otherwise: bb2];
9+
switchInt(copy _2) -> [0: bb1, 1: bb2, otherwise: bb2];
1010
}
1111

1212
bb1: {

Diff for: tests/mir-opt/building/custom/enums.switch_option_repr.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn switch_option_repr(_1: Bool) -> bool {
66

77
bb0: {
88
_2 = discriminant(_1);
9-
switchInt(_2) -> [0: bb2, otherwise: bb1];
9+
switchInt(copy _2) -> [0: bb2, otherwise: bb1];
1010
}
1111

1212
bb1: {

0 commit comments

Comments
 (0)