Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upuninitialized/zeroed statics/consts #411
Comments
thestinger
referenced this issue
Oct 24, 2014
Closed
no way to make uninitialized global variables #17571
This comment has been minimized.
This comment has been minimized.
|
All-bits-zero isn't special in Rust (up to dynamic drops, which are being removed). Allowing some kind of |
This comment has been minimized.
This comment has been minimized.
|
Sounds like CTFE. See #322 for another proposal that suggested limited CTFE for intrinsic functions. |
This comment has been minimized.
This comment has been minimized.
|
We do have a limited version of CTFE. Currently it is limited to arithmetic and constructors. However, adding I'm talking about stuff like: trait Tr { fn doit(&self); }
impl Tr for [uint, ..std::mem::size_of::<uint>()] {
fn doit(&self) { println!("called uint"); }
}
impl Tr for [uint, ..12-std::mem::size_of::<uint>()] {
fn doit(&self) { println!("called 12-uint"); }
}
fn main() {
let s = [0,1,2,3u];
s.doit(); // which .doit is called depends on architecture
}We could get around this by treating such values as "abstract values", so that they don't interfere with type-checking, and only expand them in trans. Of course this would create 2 kinds of constexprs, but otherwise we would be getting into what seems to me like a pretty deep dependent type hole. C gets away with this because it "executes" declarations one-by-one. |
This comment has been minimized.
This comment has been minimized.
|
@arielb1 you can already do that, using |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
AFAIK, the size calculations are done by LLVM. We could have a LLVM context during type-checking, is there anything problematic with that? We should do that anyway instead of the hacks we've thrown around transmute. |
This comment has been minimized.
This comment has been minimized.
|
They are done via LLVM and a big part of trans. I'll prefer keeping them separate. Actually, if we allow calling trait methods in constexprs then we'll have a total dependent type mess, and I strongly prefer not to go there, so we need to stop somewhere. |
This comment has been minimized.
This comment has been minimized.
|
There is also the problem of struct -> constexpr recursion. Because there are only finitely many constants we can handle this with a DFS, but if we allow functions this could get more complicated. |
This comment has been minimized.
This comment has been minimized.
|
A big part of trans? I find it's quite isolated, compared to anything which deals with
Nobody said anything about that. You couldn't even use a |
petrochenkov
added
T-lang
T-compiler
labels
Jan 19, 2018
This comment has been minimized.
This comment has been minimized.
|
Would it be possible to make |
This comment has been minimized.
This comment has been minimized.
|
@gnzlbg once rust-lang/rust#46882 is merged, that is a trivial addition! (imo even uncontroversial, since you can write an union Foo<T> {
t: T,
u: (),
}
unsafe const fn uninitialized<T>() -> T {
Foo { u: () }.t
} |
This comment has been minimized.
This comment has been minimized.
|
@oli-obk congrats on the merge of that PR :) Any news about this? This issue comes up every now and then in |
gnzlbg
referenced this issue
Mar 27, 2018
Closed
There's seemingly no way to construct a __m128i in a const fn #403
This comment has been minimized.
This comment has been minimized.
|
My feeling is that someone should just open a PR and we FCP it, because you can do zeroed/uninitialized with horrible hacks in constants, so why not allow the nice obvious ways, too!? Implementation guide: grep for |
This comment has been minimized.
This comment has been minimized.
derekdreery
commented
Jun 10, 2018
|
@oli-obk can you give some more hints? I can't find any code for "uninitialized" in miri that looks like the const eval stuff in librustc_mir/interpret/const_eval. Also, would I need to add uninitialized/zeroed to the intrinsics? |
This comment has been minimized.
This comment has been minimized.
|
So... I've been told that these intrinsics should not exist due to various unsafety rules not playing nice with them. Instead of creating a static/const with one of these intrinsics, your static/const should be of Union type with a zst variant that you use to "initialize" it. I'm not sure this issue can be acted on. Maybe we should close it? cc @RalfJung |
This comment has been minimized.
This comment has been minimized.
|
Seems like essentially a duplicate of rust-lang/rust#50150 to me. Instead, we should strive to stabilize |
This comment has been minimized.
This comment has been minimized.
derekdreery
commented
Jun 11, 2018
|
I've now read the other issue & the MaybeUninit RFC and it does seem like this is a duplicate of the other issue. Maybe close this so no-one tries to implement it? |
XOSplicer
referenced this issue
Aug 16, 2018
Open
Show a constant's virtual memory on validation errors #53325
glandium
referenced this issue
Nov 22, 2018
Closed
turn `mem::uninitialized` into a constant function #50150
This comment has been minimized.
This comment has been minimized.
|
Closing as ~"completed". |
Zoxc commentedOct 24, 2014
There should be a way to create consts, statics and static muts which are partially or fully uninitialized or zeroed.
One way to do this could be to create built-in unsafe generic constants ZEROED and UNDEF.
@eddyb suggested to make the existing intrinsics usable in statics
A crude solution could be to allow the initializer in a static mut to be left out, which should cause zeroing all it's memory.