Skip to content

Commit

Permalink
Make {align,size}_of_val const
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstatic-morse committed Jul 29, 2020
1 parent 8611e52 commit 9caf0b5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions library/core/src/intrinsics.rs
Expand Up @@ -1004,11 +1004,13 @@ extern "rust-intrinsic" {
///
/// The stabilized version of this intrinsic is
/// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
/// The required alignment of the referenced value.
///
/// The stabilized version of this intrinsic is
/// [`std::mem::align_of_val`](../../std/mem/fn.align_of_val.html).
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;

/// Gets a static string slice containing the name of a type.
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Expand Up @@ -88,6 +88,8 @@
#![feature(const_result)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
#![feature(const_size_of_val)]
#![feature(const_align_of_val)]
#![feature(const_type_name)]
#![feature(const_likely)]
#![feature(const_unreachable_unchecked)]
Expand Down
8 changes: 5 additions & 3 deletions library/core/src/mem/mod.rs
Expand Up @@ -332,7 +332,8 @@ pub const fn size_of<T>() -> usize {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
intrinsics::size_of_val(val)
}

Expand Down Expand Up @@ -466,9 +467,10 @@ pub const fn align_of<T>() -> usize {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
#[allow(deprecated)]
pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
min_align_of_val(val)
pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
intrinsics::min_align_of_val(val)
}

/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
Expand Down
15 changes: 15 additions & 0 deletions src/librustc_mir/interpret/intrinsics.rs
Expand Up @@ -120,6 +120,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.write_scalar(location.ptr, dest)?;
}

sym::min_align_of_val | sym::size_of_val => {
let place = self.deref_operand(args[0])?;
let (size, align) = self
.size_and_align_of(place.meta, place.layout)?
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;

let result = match intrinsic_name {
sym::min_align_of_val => align.bytes(),
sym::size_of_val => size.bytes(),
_ => bug!(),
};

self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
}

sym::min_align_of
| sym::pref_align_of
| sym::needs_drop
Expand Down

0 comments on commit 9caf0b5

Please sign in to comment.