Is there any way to keep Box as a primitive type in MIR?

I want to do some analysis in MIR. I want to know is there any command(-C or -Z) to generate MIR where Box occurs as a primitive type?

For example,

let mut boxn = Box::new(Node { x : 100}); // Node is a struct with only one filed.
boxn.x = 100;

Then, the generated MIR looks like below.

_3 = std::boxed::Box::<Node>::new(Node {{ x: 14_i32 }})
_24 = (((_3.0: std::ptr::Unique<Node>).0: std::ptr::NonNull<Node>).0: *const Node);
((*_24).0: i32) = 100_i32;

As we can see, boxn.x is unwind as a Place(_24) expression in MIR. I wonder is there any way to remove the first assignment. The generated MIR looks like

((*_3).0 : i32) = 100_i32

In addition, we can write

let mut boxn = Box::new(Node { x : 100}); // Node is a struct with only one filed.
boxn.deref_mut().x = 100;

Thus, we can get

_3 = std::boxed::Box::<Node>::new(Node {{ x: 14_i32 }})
_24 = <std::boxed::Box<Node> as std::ops::DerefMut>::deref_mut(move _25)
((*_24).0: i32) = 100_i32;

Whatever, can we keep Box as a primitive type in MIR?

You'll want to get MIR from before the passes that do this transformation. -Zdump-mir=Underefer.before should give it to you (unless I wrote the pass name wrong)

If you're trying to write a program that analyzes MIR I'd recommend using rustc as a library instead of analyzing the text output. You'd be able to get help with that on internals.rust-lang.org or the zulip

What's your actual goal? The direction in general (see also things like RFC: No (opsem) Magic Boxes by chorman0773 · Pull Request #3712 · rust-lang/rfcs · GitHub ) is making Box less special, and thus especially in runtime MIR it would be entirely normal and expected for types like Box to be SRoA'd away.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.