-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Unstably constify ptr::drop_in_place
and related methods
#146187
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
base: master
Are you sure you want to change the base?
Conversation
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
This comment has been minimized.
This comment has been minimized.
Was hoping just the destruct bound was the issue, but apparently not. Will have to dig deeper into this. Until then, @rustbot author |
Reminder, once the PR becomes ready for a review, use |
a03d08a
to
4b9d7b6
Compare
The Miri subtree was changed cc @rust-lang/miri |
Oh, the scary-looking errors were extremely simple to fix now that I actually investigated them. They were just copying the function signature in the error, which had obviously changed. Hopefully that's all. |
That was all, it turns out. @rustbot ready |
Please add a test that actually invokes |
4b9d7b6
to
db531fb
Compare
Thank you not only for reminding me to write that test, but also for helping me notice that I somehow missed Destructor tests are weird to write, but what I have should be reasonable, I think. |
db531fb
to
07d3e92
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This code compiles with this PR. Is this intended? #![feature(const_trait_impl, const_destruct, const_drop_in_place)]
use std::ptr::drop_in_place;
struct Thing(i32);
impl const Drop for Thing {
fn drop(&mut self) {}
}
static mut X: Thing = {
unsafe {
drop_in_place(&raw mut X);
}
Thing(1)
}; This also compiles: #![feature(const_trait_impl, const_destruct, const_drop_in_place)]
use std::ptr::drop_in_place;
struct Thing(i32);
impl const Drop for Thing {
fn drop(&mut self) {}
}
static mut Y: Thing = Thing(2);
const X: i32 = {
unsafe {
drop_in_place(&raw mut Y);
}
1
}; This also compiles: #![feature(const_trait_impl, const_destruct, const_drop_in_place)]
use std::ptr::drop_in_place;
struct Thing(i32);
impl const Drop for Thing {
fn drop(&mut self) {}
}
static Y: Thing = Thing(2);
const X: i32 = {
unsafe {
drop_in_place(&raw const Y as *mut Thing);
}
1
}; |
This has... interesting interaction with packed structs. The following code compiles: #![feature(const_trait_impl, const_destruct, const_drop_in_place)]
use std::ptr::drop_in_place;
struct Thing(i32);
impl const Drop for Thing {
fn drop(&mut self) {
self.0 = 2;
}
}
#[repr(packed)]
struct Packed(Thing);
static Y: Packed = Packed(Thing(1));
const X: () = {
unsafe {
drop_in_place((&raw const Y).cast_mut());
}
}; See also #143411 |
I mean, you've certainly pointed out a lot of compelling reasons why we shouldn't stabilise this feature right now, but also, we can't really test that all those cases aren't occurring without having the unstable constness in the first place. It's working, just… well, we now have new cases of UB to track and prevent. This is also why we explicitly track which unstable const features are used in stable items, to ensure that we don't accidentally stabilise something unsound. It would, for example, prevent us from properly stabilising const |
Tracking: #109342
Supercedes: #145725
Makes methods const:
core::ptr::drop_in_place
core::mem::ManuallyDrop::drop
core::mem::MaybeUninit::assume_init_drop
<[core::mem::MaybeUninit<_>]>::assume_init_drop
<*mut _>::drop_in_place
core::ptr::NonNull::drop_in_place