Skip to content

Commit 79611d9

Browse files
committed
Auto merge of rust-lang#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

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
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

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
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
}

tests/mir-opt/address_of.address_of_reborrow.SimplifyCfg-initial.after.mir

Lines changed: 6 additions & 6 deletions
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);

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

Lines changed: 4 additions & 4 deletions
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: {

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

Lines changed: 4 additions & 4 deletions
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: {

tests/mir-opt/array_index_is_temporary.rs

Lines changed: 1 addition & 1 deletion
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;

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

Lines changed: 1 addition & 1 deletion
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;

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

Lines changed: 1 addition & 1 deletion
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;

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

Lines changed: 1 addition & 1 deletion
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;

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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)