-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Option containing heap drop types in static mut acts strangley #46089
Comments
Creating a To explain the specific weirdness you're seeing: The non-nullness is used to store the |
I'm going to close as not a bug; let us know if you feel differently and we can reopen. |
I was under the impression that only reading from a zeroed String was UB, so I thought using the Option state without reading was fine. But you're saying creating one whatsoever is UB? Is that documented anywhere? |
My apologies, you''ve stumbled upon a corner of the language that was (and still is to some degree) fuzzily-defined and contradictory. I'm pretty sure we're moving towards even just creating the invalid value being UB (and as part of that, deprecating You make a good point about the docs of zeroed and uninitialized, they should be updated. I'll file an issue for that. |
The following code use the recently stabilized, (but not in the current stable release yet,) feature
drop_types_in_const
, so it needs a recent nightly version to be compiled.This prints out "None", instead of the expected "Some(Foo)". However, if I change
FOO = Some(std::mem::zeroed());
toFOO = Some(std::mem::uninitialized());
then it prints "Some(Foo)", (at least on my system, but that could clearly be just "luck".) The assignment also works as expected if I explicitly initializeFOO
, that is, something likeFOO = Some("Hello World!".to_string());
, or if I use a non-drop type. If I use a different built-in drop type thanString
, likeVec<u32>
orBox<u32>
then it still prints "None". However, if I define my own stack-allocated drop type like so:and make
bar
an zeroed instance of that type then it prints "Some(Foo)".The text was updated successfully, but these errors were encountered: