Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,14 @@ impl<T> MaybeUninit<T> {
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[cfg(not(stage0))]
pub const fn zeroed() -> MaybeUninit<T> {
MaybeUninit { value: unsafe { intrinsics::init() } }
}

#[unstable(feature = "maybe_uninit", issue = "53491")]
#[cfg(stage0)]
/// Ceci n'est pas la documentation
pub fn zeroed() -> MaybeUninit<T> {
let mut u = MaybeUninit::<T>::uninitialized();
unsafe {
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
}
self.write_scalar(val, dest)?;
}
"init" => {
self.force_allocation(dest)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just copy the implementation from https://github.com/solson/miri/blob/master/src/intrinsic.rs#L238

Your current implementation is a nop, it literally does nothing ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang, that was awfully naive of me ha. Done.

Side question: is there any reason that entire file isn't just copied into here?

}
"transmute" => {
// Go through an allocation, to make sure the completely different layouts
// do not pose a problem. (When the user transmutes through a union,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
| "add_with_overflow"
| "sub_with_overflow"
| "mul_with_overflow"
| "init"
// no need to check feature gates, intrinsics are only callable from the
// libstd or with forever unstable feature gates
=> is_const_fn = true,
Expand Down
26 changes: 26 additions & 0 deletions src/test/run-pass/const-maybe-init-zeroed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(maybe_uninit)]

use std::mem;

fn main() {
const UNIT: mem::MaybeUninit<()> = mem::MaybeUninit::zeroed();
let bytes: [u8; 0] = unsafe { mem::transmute(UNIT) };
assert_eq!(bytes, [0u8; 0]);

const STRING: mem::MaybeUninit<String> = mem::MaybeUninit::zeroed();
let bytes: [u8; mem::size_of::<String>()] = unsafe { mem::transmute(STRING) };
assert_eq!(bytes, [0u8; mem::size_of::<String>()]);

const U8: mem::MaybeUninit<u8> = mem::MaybeUninit::zeroed();
let bytes: [u8; 1] = unsafe { mem::transmute(U8) };
assert_eq!(bytes, [0u8; 1]);
}