-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
box syntax creates Box<T> where T is not yet initialized #97270
Comments
Ah fun times, we are actually generating a Maybe |
This is the same problem that also led me to exclude
However, the uninitialized box already has type |
@rust-lang/wg-mir-opt (is that the right ping group for MIR building questions?) Currently, this #![feature(rustc_attrs)]
pub fn new<T>(x: T) -> Box<T> {
#[rustc_box]
Box::new(x)
} creates MIR roughly like this fn new(_1: T) -> Box<T> {
let mut _0: std::boxed::Box<T>;
let mut _4: *mut u8;
_4 = alloc::alloc::exchange_malloc(...)
_0 = ShallowInitBox(move _4, T);
_5 = (((_0.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T);
(*_5) = move _1;
return;
} The problem with this is that we call Is it possible for MIR generation to only call This has probably been a problem since #89030, maybe longer than that. |
This issue is certainly not introduced by We marked the rvalue returned by |
That's an initialization tracker / drop elaboration thing, though, if I understand correctly. It is not reflected in the type and therefore the operational semantics of the program. |
I guess another possible fix would be to say that |
The shallow initialized box is not very different from a Box where its content have been moved. I guess the issue here is with uninhabited types there's no notion of a moved-out box of that type? Also, could you explain why a value of |
The problem is that operationally speaking, we are moving the shallow initialized box:
See rust-lang/unsafe-code-guidelines#413 (and note that |
I see. In this case I think making this a statement / non-diverging intrinsic probably makes sense. |
What about having |
The point of We could codegen as allocating a |
We could also have a |
To be clear, this is not something that must be fixed. But if we keep the status quo, that has a few consequences (none of them terrible):
The latter is my preferred resolution of rust-lang/unsafe-code-guidelines#412 (which applies to references and |
Yet another alternative would be to use |
It's difficult to ensure in-place codegen that way, IIRC there were a few attempts to remove special handling of Box::new but performance degradation was quite significant. |
Hm... @scottmcm did a bunch of work recently on making our MIR nicer before it is shipped to LLVM, I wonder if that could be applied to |
I tried this code:
miri reports UB on this code:
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: