What does #[rustc_box] mean?

After reading the source code of rust, I saw #[rustc_box], and when I went to trace the code, I ended up with no luck, what does #[rustc_box] do at a deep level, and what is the underlying implementation mechanism?

In the version before rust is used box x, now is used #[rustc_box], what is their deep-seated difference?

1 Like

For future reference, please place code within Markdown backticks rather than turning it into a screenshot. Especially on smaller displays that quickly becomes unreadable.


To my understanding, the box x syntax was a never-stabilized value creation and destructuring syntax for values of type Box<T>. It was special because Box<T> itself is special, as the fundamental type in Rust that can do heap allocation (from a Rustacean's POV at least). Since the language needs some way to do the actual allocation in a portable way, the role of box x was essentially a kind of compiler intrinsic that did just that.

The syntax was never stabilized, and in fact was phased out due to issues with placement allocation IIRC.

But the removal of box x posed a problem: what to replace it with for usage within the compiler itself? That's what the #[rust_box] attribute looks like it's addressing. Note that without the attribute on the Box::new(x) expr, it's a self-defeating fn, because all the containing new method does is recursively call itself, which can only lead to an immediate stack overflow. The attribute will modify that behavior to something that's actually usable.

Relevant:

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.