Uh oh!
There was an error while loading. Please reload this page.
turn mem::uninitialized into a constant function - #50150
Conversation
| match dest { | ||
| Place::Local { frame, local } => ecx.modify_local(frame, local, uninit)?, | ||
| Place::Ptr { | ||
| ptr, |
There was a problem hiding this comment.
I copy pasted this code from src/tools/miri. The miri version had ptr: PtrAndAlign { ptr, aligned: true } in the pattern, but that got lost in translation. How do I check that ptr is aligned in this context? Pointer API didn't have any method to check for alignment AFAICT.
There was a problem hiding this comment.
Did you use the old code? You could try looking again, now that miri has been updated.
oli-obk
commented
Apr 22, 2018
At the all-hands @nikomatsakis and @RalfJung mentioned that they want to eliminate the uninitiated intrinsic because it's not very good to reason about. Instead one should be using a union. Not sure in which way that should be happening though. |
@japaric Indeed you can implement Here's the RFC for the replacement: rust-lang/rfcs#1892. I'd rather see us not allow or encourage more uses of |
oli-obk
commented
Apr 22, 2018
Stability is not the issue at hand. We can make the methods on |
japaric
commented
Apr 23, 2018
@RalfJung Does #![feature(const_fn)]#![feature(untagged_unions)]use std::mem;constunsafefnzeroed<T>() -> T{unionU<T>{bytes:[u8; mem::size_of::<T>()],data:T,}U{bytes:[0; mem::size_of::<T>()],}.data} |
oli-obk
commented
Apr 24, 2018
@japaric I think Why do you need |
RalfJung
commented
Apr 24, 2018
@japaric It can be reimplemented in a sane way on top of impl<T>MaybeUninit<T>{pubfnzeroed() -> Self{letmut u = MaybeUninit::uninitialized();
ptr::write_bytes(&mut u as*mutMaybeUninit<T>,0u8,1);
u
}} |
bors
commented
Apr 27, 2018
☔ The latest upstream changes (presumably #50275) made this pull request unmergeable. Please resolve the merge conflicts. |
shepmaster
commented
May 6, 2018
oli-obk
commented
May 7, 2018
This won't be happening, as the function is supposed to get deprecated at some point. |
Uses a trick stolen from @japaric here: rust-lang/rust#50150 to avoid using `mem::uninitialized`, which isn't currently marked `const`
glandium
commented
Nov 22, 2018
I'll note that this would be useful even if the function is going to be deprecated, because MaybeUninit doesn't seem to come any time soon. The alternative horror I'm currently using is ... to define some statics as extern "C" and to (re)define them in a C file, uninitialized. |
RalfJung
commented
Nov 22, 2018
Useful, yes -- for about 2 or 3 releases or so. Then, for the rest of time, it's a liability. I don't think we should stabilize and support forever (!) a feature we know to be broken. Instead, we should focus on making progress on the proper way of expressing this. (And we are making progress.) I am sorry that you have to use horrible work-arounds. That's not great, and we should fix it. But introducing something half-broken is not "fixing" anything. Also, AFAIK, C zero-initializes statics by default... |
glandium
commented
Nov 22, 2018
Yes, and I'd totally be okay with std::mem::zeroed() being const. And rust-lang/rfcs#411 has been ignored in favor of MaybeUninit too. |
oli-obk
commented
Nov 22, 2018
You can work around this with a custom union, can you not? unionMaybeUninit<T:Copy>{uninit:(),init:T}staticmutFOO:MaybeUninit<u32> = MaybeUninit{uninit:()}; |
I was starting to wonder if I can't just copy/paste MaybeUninit. Edit: Ah, not because of const_fn. |
Uses a trick stolen from @japaric here: rust-lang/rust#50150 to avoid using `mem::uninitialized`, which isn't currently marked `const`
one can implement a
const fnversion ofmem::uninitializedusing unions (see below) so there's no real reason to not make the standard version const, IMO.r? @oli-obk
cc @rust-lang/libs