-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Make core::mem::zeroed() a const fn #62061
Comments
(Apologies if this shouldn't have a dedicated issue, it didn't seem to be covered by any of the existing |
I copied
So there's more work here than simply marking it |
Ah, source diving it seems that message simply means that support for the intrisinc hasn't been added to the MIR interpreter: https://github.com/rust-lang/rust/blob/master/src/librustc_mir/interpret/intrinsics.rs#L44 Does adding support for these need their own bugs, or can they just be covered here? |
After source diving a bit, I'd be happy to provide a PR to implement |
Cc @oli-obk
What should become a |
All of these intrinsics are already implemented in Miri at https://github.com/rust-lang/miri/blob/master/src/intrinsic.rs. But so far they deliberately were not moved into rustc proper. |
We could make just the Or we can try getting union Foo<T> {
a: [u8; std::mem::size_of::<T>()],
b: T,
} to work, but that's lazy normalization, so... 😆 |
I'm in full agreement, the |
My concern is with the implementation of the What's missing to make |
But given that people have been asking for this for quite a while... do you think it is realistic to add an implementation of the |
Oh, you basically suggested that above.^^ Sorry. So I think I wouldn't block that plan:
If we want to avoid seeing |
Let's do that first, yes Couldn't we also make The rest of the plan sounds good to me. We can also mark the |
We could. |
Just to be clear, the proposal is first a PR that does: diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs
index 770d1ca8e7..487785b878 100644
--- a/src/libcore/mem/mod.rs
+++ b/src/libcore/mem/mod.rs
@@ -450,8 +450,7 @@ pub const fn needs_drop<T>() -> bool {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn zeroed<T>() -> T {
- intrinsics::panic_if_uninhabited::<T>();
- intrinsics::init()
+ MaybeUninit::zeroed().assert_init()
}
/// Bypasses Rust's normal memory-initialization checks by pretending to
@@ -476,8 +475,7 @@ pub unsafe fn zeroed<T>() -> T {
#[rustc_deprecated(since = "1.38.0", reason = "use `mem::MaybeUninit` instead")]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn uninitialized<T>() -> T {
- intrinsics::panic_if_uninhabited::<T>();
- intrinsics::uninit()
+ MaybeUninit::uninit().assert_init()
}
/// Swaps the values at two mutable locations, without deinitializing either one. |
that and completely remove the |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Cool, I'll hopefully have a patch up for that shortly. |
As discussed in the old PR (#54832 (comment)) we could also allow |
What would it take to allow mutable references in |
Oh, I mean enable them in all consts. I'm not sure about the const qualif implications right now, but we could start by adding them behind a WIP feature gate without having to worry about accidental stabilizations. Wherever a cc #57349 |
…g,oli-obk,Centril Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
…g,oli-obk,Centril Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
…g,oli-obk,Centril Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
Implement mem::{zeroed,uninitialized} in terms of MaybeUninit. Refs rust-lang#62061 r? @oli-obk
Okay,
Also @oli-obk is on vacation so we should probably wait until they are back.^^ |
I'm strongly in favor of killing |
For some prior art, see #54832, where I tried to make I would love to revive #54832 if people are okay with using
I get that, and to a degree I agree, but to be honest I don't think it's working. We have people who could really use a |
I can guide you through implementing mutable references in const fn if you want. I believe it's not much work and stabilization wise it's just an extension of |
Status update: that PR was reverted because it broke some programs. Those programs all had UB, but we reverted to give them more time to fix their stuff. But the main blocker for this issue anyway is |
Hi Ralf, here is the related PR: #66606 |
What's the status on this? It's quite problematic that static mut STOP_SEM: UnsafeCell<MaybeUninit<libc::sem_t>> = UnsafeCell::new(MaybeUninit::uninit());
fn sem_ptr(sem: &UnsafeCell<MaybeUninit<libc::sem_t>>) -> *mut libc::sem_t {
sem.get() as *mut libc::sem_t
} Which is way more complicated and error-prone than it should be... |
still blocked on #57349 for which I have a PR open. We just need a bit of time to figure out all the edge cases. |
Until this issue is closed, anyone who wants a const |
Max, you're truly doing Ferris' work. |
This has been a const fn since Rust 1.75.0. |
Right now this isn't callable from
const fn
s. This would be useful for usingconst fn
s in combination with FFI.The text was updated successfully, but these errors were encountered: