Skip to content

Commit

Permalink
Auto merge of rust-lang#122551 - RayMuir:copy_fmt, r=saethlin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 19, 2024
2 parents 4fe1e2b + b9cffa7 commit b7e4e42
Show file tree
Hide file tree
Showing 725 changed files with 3,926 additions and 3,924 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ b2d2184edea578109a48ec3d8decbee5948e8f35
ec2cc761bc7067712ecc7734502f703fe3b024c8
# format use declarations
84ac80f1921afc243d71fd0caaa4f2838c294102
# bless mir-opt tests to add `copy`
99cb0c6bc399fb94a0ddde7e9b38e9c00d523bad
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ impl<'tcx> Debug for Operand<'tcx> {
use self::Operand::*;
match *self {
Constant(ref a) => write!(fmt, "{a:?}"),
Copy(ref place) => write!(fmt, "{place:?}"),
Copy(ref place) => write!(fmt, "copy {place:?}"),
Move(ref place) => write!(fmt, "move {place:?}"),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn address_of_reborrow() -> () {
StorageLive(_6);
_6 = &raw const (*_1);
AscribeUserType(_6, o, UserTypeProjection { base: UserType(0), projs: [] });
_5 = _6;
_5 = copy _6;
StorageDead(_6);
StorageDead(_5);
StorageLive(_7);
Expand All @@ -153,7 +153,7 @@ fn address_of_reborrow() -> () {
_9 = move _10 as *const dyn std::marker::Send (PointerCoercion(Unsize));
StorageDead(_10);
AscribeUserType(_9, o, UserTypeProjection { base: UserType(1), projs: [] });
_8 = _9;
_8 = copy _9;
StorageDead(_9);
StorageDead(_8);
StorageLive(_11);
Expand Down Expand Up @@ -194,7 +194,7 @@ fn address_of_reborrow() -> () {
StorageLive(_22);
_22 = &raw const (*_3);
AscribeUserType(_22, o, UserTypeProjection { base: UserType(10), projs: [] });
_21 = _22;
_21 = copy _22;
StorageDead(_22);
StorageDead(_21);
StorageLive(_23);
Expand All @@ -207,7 +207,7 @@ fn address_of_reborrow() -> () {
_25 = move _26 as *const dyn std::marker::Send (PointerCoercion(Unsize));
StorageDead(_26);
AscribeUserType(_25, o, UserTypeProjection { base: UserType(11), projs: [] });
_24 = _25;
_24 = copy _25;
StorageDead(_25);
StorageDead(_24);
StorageLive(_27);
Expand Down Expand Up @@ -242,7 +242,7 @@ fn address_of_reborrow() -> () {
StorageLive(_36);
_36 = &raw mut (*_3);
AscribeUserType(_36, o, UserTypeProjection { base: UserType(20), projs: [] });
_35 = _36;
_35 = copy _36;
StorageDead(_36);
StorageDead(_35);
StorageLive(_37);
Expand All @@ -255,7 +255,7 @@ fn address_of_reborrow() -> () {
_39 = move _40 as *mut dyn std::marker::Send (PointerCoercion(Unsize));
StorageDead(_40);
AscribeUserType(_39, o, UserTypeProjection { base: UserType(21), projs: [] });
_38 = _39;
_38 = copy _39;
StorageDead(_39);
StorageDead(_38);
StorageLive(_41);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ fn main() -> () {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
_6 = _3;
_6 = copy _3;
_5 = foo(move _6) -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_6);
StorageLive(_7);
_7 = _2;
_7 = copy _2;
_8 = Len(_1);
_9 = Lt(_7, _8);
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> [success: bb2, unwind unreachable];
_9 = Lt(copy _7, copy _8);
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, copy _7) -> [success: bb2, unwind unreachable];
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ fn main() -> () {
StorageDead(_4);
StorageLive(_5);
StorageLive(_6);
_6 = _3;
_6 = copy _3;
_5 = foo(move _6) -> [return: bb1, unwind continue];
}

bb1: {
StorageDead(_6);
StorageLive(_7);
_7 = _2;
_7 = copy _2;
_8 = Len(_1);
_9 = Lt(_7, _8);
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> [success: bb2, unwind continue];
_9 = Lt(copy _7, copy _8);
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, copy _7) -> [success: bb2, unwind continue];
}

bb2: {
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/array_index_is_temporary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
// CHECK: debug x => [[x:_.*]];
// CHECK: debug y => [[y:_.*]];
// CHECK: [[y]] = const 1_usize;
// CHECK: [[tmp:_.*]] = [[y]];
// CHECK: [[tmp:_.*]] = copy [[y]];
// CHECK: [[x]][[[tmp]]] =
let mut x = [42, 43, 44];
let mut y = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ yields ()

bb0: {
StorageLive(_3);
_3 = (_1.0: i32);
_3 = copy (_1.0: i32);
FakeRead(ForLet(None), _3);
StorageLive(_4);
_4 = &_3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ yields ()

bb0: {
StorageLive(_3);
_3 = (_1.0: i32);
_3 = copy (_1.0: i32);
FakeRead(ForLet(None), _3);
StorageLive(_4);
_4 = &_3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ yields ()

bb0: {
StorageLive(_3);
_3 = (_1.0: i32);
_3 = copy (_1.0: i32);
FakeRead(ForLet(None), _3);
StorageLive(_4);
_4 = &_3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ yields ()

bb0: {
StorageLive(_3);
_3 = (_1.0: i32);
_3 = copy (_1.0: i32);
FakeRead(ForLet(None), _3);
StorageLive(_4);
_4 = &_3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main::{closure#0}::{closure#1}(_1: &{async closure@$DIR/async_closure_shims.r
let mut _0: {async closure body@$DIR/async_closure_shims.rs:62:48: 65:10};

bb0: {
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: ((*_1).0: &i32) };
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: copy ((*_1).0: &i32) };
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main::{closure#0}::{closure#1}(_1: &{async closure@$DIR/async_closure_shims.r
let mut _0: {async closure body@$DIR/async_closure_shims.rs:62:48: 65:10};

bb0: {
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: ((*_1).0: &i32) };
_0 = {coroutine@$DIR/async_closure_shims.rs:62:48: 65:10 (#0)} { a: move _2, b: copy ((*_1).0: &i32) };
return;
}
}
2 changes: 1 addition & 1 deletion tests/mir-opt/basic_assignment.main.ElaborateDrops.diff
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_1 = const false;
StorageLive(_2);
StorageLive(_3);
_3 = _1;
_3 = copy _1;
_2 = move _3;
StorageDead(_3);
StorageLive(_4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> () {
FakeRead(ForLet(None), _1);
StorageLive(_2);
StorageLive(_3);
_3 = _1;
_3 = copy _1;
_2 = move _3;
StorageDead(_3);
StorageLive(_4);
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/box_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
fn main() {
// CHECK-LABEL: fn main(
// CHECK: [[box:_.*]] = ShallowInitBox(
// CHECK: [[ptr:_.*]] = ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S);
// CHECK: [[ptr:_.*]] = copy ((([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>).0: *const S);
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
// CHECK: [[ret]]: {
// CHECK: [[box2:_.*]] = move [[box]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
StorageLive(_13);
StorageLive(_14);
StorageLive(_15);
_15 = _38;
_15 = copy _38;
_14 = move _15;
goto -> bb6;
}
Expand Down Expand Up @@ -194,8 +194,8 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->

bb10: {
StorageLive(_17);
_17 = ((_9 as Ready).0: ());
_3 = _17;
_17 = copy ((_9 as Ready).0: ());
_3 = copy _17;
StorageDead(_17);
StorageDead(_14);
StorageDead(_12);
Expand Down Expand Up @@ -253,7 +253,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
StorageLive(_29);
StorageLive(_30);
StorageLive(_31);
_31 = _38;
_31 = copy _38;
_30 = move _31;
goto -> bb18;
}
Expand Down Expand Up @@ -291,8 +291,8 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->

bb21: {
StorageLive(_33);
_33 = ((_25 as Ready).0: ());
_37 = _33;
_33 = copy ((_25 as Ready).0: ());
_37 = copy _33;
StorageDead(_33);
StorageDead(_30);
StorageDead(_28);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ fn adt() -> Onion {
bb0: {
_1 = const 1_i32;
_2 = Foo { a: const 1_i32, b: const 2_i32 };
_3 = Bar::Foo(move _2, _1);
_0 = Onion { neon: ((_3 as variant#0).1: i32) };
_3 = Bar::Foo(move _2, copy _1);
_0 = Onion { neon: copy ((_3 as variant#0).1: i32) };
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn array() -> [i32; 2] {
bb0: {
_1 = [const 42_i32, const 43_i32];
_2 = const 1_i32;
_1 = [_2, const 2_i32];
_1 = [copy _2, const 2_i32];
_0 = move _1;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ fn arbitrary_let(_1: i32) -> i32 {
let mut _3: i32;

bb0: {
_2 = _1;
_2 = copy _1;
goto -> bb2;
}

bb1: {
_0 = _3;
_0 = copy _3;
return;
}

bb2: {
_3 = _2;
_3 = copy _2;
goto -> bb1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn arrays() -> usize {
bb0: {
_1 = [const 5_i32; C];
_2 = Len(_1);
_0 = _2;
_0 = copy _2;
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn float_to_int(_1: f32) -> i32 {
let mut _0: i32;

bb0: {
_0 = _1 as i32 (FloatToInt);
_0 = copy _1 as i32 (FloatToInt);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn int_to_int(_1: u32) -> i32 {
let mut _0: i32;

bb0: {
_0 = _1 as i32 (IntToInt);
_0 = copy _1 as i32 (IntToInt);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn int_to_ptr(_1: usize) -> *const i32 {
let mut _0: *const i32;

bb0: {
_0 = _1 as *const i32 (PointerWithExposedProvenance);
_0 = copy _1 as *const i32 (PointerWithExposedProvenance);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn assume_local(_1: bool) -> () {
let mut _0: ();

bb0: {
assume(_1);
assume(copy _1);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn assume_place(_1: (bool, u8)) -> () {
let mut _0: ();

bb0: {
assume((_1.0: bool));
assume(copy (_1.0: bool));
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn switch_bool(_1: bool) -> u32 {
let mut _0: u32;

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

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn switch_option(_1: Option<()>) -> bool {

bb0: {
_2 = discriminant(_1);
switchInt(_2) -> [0: bb1, 1: bb2, otherwise: bb2];
switchInt(copy _2) -> [0: bb1, 1: bb2, otherwise: bb2];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn switch_option_repr(_1: Bool) -> bool {

bb0: {
_2 = discriminant(_1);
switchInt(_2) -> [0: bb2, otherwise: bb1];
switchInt(copy _2) -> [0: bb2, otherwise: bb1];
}

bb1: {
Expand Down
Loading

0 comments on commit b7e4e42

Please sign in to comment.