Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl AllocFnFactory<'_, '_> {
}

fn ptr_alignment(&self) -> Box<Ty> {
let path = self.cx.std_path(&[sym::ptr, sym::Alignment]);
let path = self.cx.std_path(&[sym::mem, sym::Alignment]);
let path = self.cx.path(self.span, path);
self.cx.ty_path(path)
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_data_structures/src/aligned.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::marker::PointeeSized;
#[cfg(not(bootstrap))]
use std::mem::Alignment;
#[cfg(bootstrap)]
use std::ptr::Alignment;

/// Returns the ABI-required minimum alignment of a type in bytes.
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_data_structures/src/tagged_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ pub unsafe trait Tag: Copy {
/// Returns the number of bits available for use for tags in a pointer to `T`
/// (this is based on `T`'s alignment).
pub const fn bits_for<T: ?Sized + Aligned>() -> u32 {
crate::aligned::align_of::<T>().as_nonzero().trailing_zeros()
let alignment = crate::aligned::align_of::<T>();
#[cfg(bootstrap)]
let alignment = alignment.as_nonzero();
#[cfg(not(bootstrap))]
let alignment = alignment.as_nonzero_usize();
alignment.trailing_zeros()
}

/// Returns the correct [`Tag::BITS`] constant for a set of tag values.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/ty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ unsafe impl<H: DynSync, T: DynSync> DynSync for RawList<H, T> {}
// Layouts of `ListSkeleton<H, T>` and `RawList<H, T>` are the same, modulo opaque tail,
// thus aligns of `ListSkeleton<H, T>` and `RawList<H, T>` must be the same.
unsafe impl<H, T> Aligned for RawList<H, T> {
#[cfg(bootstrap)]
const ALIGN: ptr::Alignment = align_of::<ListSkeleton<H, T>>();
#[cfg(not(bootstrap))]
const ALIGN: mem::Alignment = align_of::<ListSkeleton<H, T>>();
}

/// A [`List`] that additionally stores type information inline to speed up
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ symbols! {
maybe_uninit,
maybe_uninit_uninit,
maybe_uninit_zeroed,
mem,
mem_align_const,
mem_discriminant,
mem_drop,
Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#[stable(feature = "alloc_module", since = "1.28.0")]
#[doc(inline)]
pub use core::alloc::*;
use core::ptr::{self, Alignment, NonNull};
use core::mem::Alignment;
use core::ptr::{self, NonNull};
use core::{cmp, hint};

unsafe extern "Rust" {
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/raw_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// run the tests. See the comment there for an explanation why this is the case.

use core::marker::{Destruct, PhantomData};
use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties};
use core::ptr::{self, Alignment, NonNull, Unique};
use core::mem::{Alignment, ManuallyDrop, MaybeUninit, SizedTypeProperties};
use core::ptr::{self, NonNull, Unique};
use core::{cmp, hint};

#[cfg(not(no_global_oom_handling))]
Expand Down Expand Up @@ -570,7 +570,7 @@ const impl<A: [const] Allocator + [const] Destruct> RawVecInner<A> {
impl<A: Allocator> RawVecInner<A> {
#[inline]
const fn new_in(alloc: A, align: Alignment) -> Self {
let ptr = Unique::from_non_null(NonNull::without_provenance(align.as_nonzero()));
let ptr = Unique::from_non_null(NonNull::without_provenance(align.as_nonzero_usize()));
// `cap: 0` means "unallocated". zero-sized types are ignored.
Self { ptr, cap: ZERO_CAP, alloc }
}
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ use core::intrinsics::abort;
#[cfg(not(no_global_oom_handling))]
use core::iter;
use core::marker::{PhantomData, Unsize};
use core::mem::{self, ManuallyDrop};
use core::mem::{self, Alignment, ManuallyDrop};
use core::num::NonZeroUsize;
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, LegacyReceiver};
#[cfg(not(no_global_oom_handling))]
Expand All @@ -261,7 +261,7 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
#[cfg(not(no_global_oom_handling))]
use core::pin::Pin;
use core::pin::PinCoerceUnsized;
use core::ptr::{self, Alignment, NonNull, drop_in_place};
use core::ptr::{self, NonNull, drop_in_place};
#[cfg(not(no_global_oom_handling))]
use core::slice::from_raw_parts_mut;
use core::{borrow, fmt, hint};
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use core::intrinsics::abort;
#[cfg(not(no_global_oom_handling))]
use core::iter;
use core::marker::{PhantomData, Unsize};
use core::mem::{self, ManuallyDrop};
use core::mem::{self, Alignment, ManuallyDrop};
use core::num::NonZeroUsize;
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, LegacyReceiver};
#[cfg(not(no_global_oom_handling))]
use core::ops::{Residual, Try};
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::pin::{Pin, PinCoerceUnsized};
use core::ptr::{self, Alignment, NonNull};
use core::ptr::{self, NonNull};
#[cfg(not(no_global_oom_handling))]
use core::slice::from_raw_parts_mut;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use crate::error::Error;
use crate::intrinsics::{unchecked_add, unchecked_mul, unchecked_sub};
use crate::mem::SizedTypeProperties;
use crate::ptr::{Alignment, NonNull};
use crate::mem::{Alignment, SizedTypeProperties};
use crate::ptr::NonNull;
use crate::{assert_unsafe_precondition, fmt, mem};

/// Layout of a block of memory.
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Layout {
#[must_use]
#[inline]
pub const fn dangling_ptr(&self) -> NonNull<u8> {
NonNull::without_provenance(self.align.as_nonzero())
NonNull::without_provenance(self.align.as_nonzero_usize())
}

/// Creates a layout describing the record that can hold a value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::ptr::Alignment;
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::MIN.as_usize(), 1);
/// ```
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::ptr::Alignment;
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::of_val(&5i32).as_usize(), 4);
/// ```
Expand Down Expand Up @@ -112,14 +112,13 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::ptr::Alignment;
/// use std::mem::Alignment;
///
/// assert_eq!(unsafe { Alignment::of_val_raw(&5i32) }.as_usize(), 4);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
// #[unstable(feature = "layout_for_ptr", issue = "69835")]
pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
// SAFETY: precondition propagated to the caller
let align = unsafe { mem::align_of_val_raw(val) };
Expand Down Expand Up @@ -169,16 +168,28 @@ impl Alignment {
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn as_usize(self) -> usize {
// Going through `as_nonzero` helps this be more clearly the inverse of
// Going through `as_nonzero_usize` helps this be more clearly the inverse of
// `new_unchecked`, letting MIR optimizations fold it away.

self.as_nonzero().get()
self.as_nonzero_usize().get()
}

/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[deprecated(
since = "CURRENT_RUSTC_VERSION",
note = "renamed to `as_nonzero_usize`",
suggestion = "as_nonzero_usize"
)]
#[inline]
pub const fn as_nonzero(self) -> NonZero<usize> {
self.as_nonzero_usize()
}

/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn as_nonzero_usize(self) -> NonZero<usize> {
// This transmutes directly to avoid the UbCheck in `NonZero::new_unchecked`
// since there's no way for the user to trip that check anyway -- the
// validity invariant of the type would have to have been broken earlier --
Expand All @@ -204,7 +215,7 @@ impl Alignment {
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn log2(self) -> u32 {
self.as_nonzero().trailing_zeros()
self.as_nonzero_usize().trailing_zeros()
}

/// Returns a bit mask that can be used to match this alignment.
Expand All @@ -214,9 +225,10 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// #![feature(ptr_mask)]
/// use std::ptr::{Alignment, NonNull};
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
/// use std::ptr::NonNull;
///
/// #[repr(align(1))] struct Align1(u8);
/// #[repr(align(2))] struct Align2(u16);
Expand Down Expand Up @@ -246,7 +258,7 @@ impl Alignment {
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl fmt::Debug for Alignment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
}
}

Expand Down Expand Up @@ -277,7 +289,7 @@ impl const TryFrom<usize> for Alignment {
impl const From<Alignment> for NonZero<usize> {
#[inline]
fn from(align: Alignment) -> NonZero<usize> {
align.as_nonzero()
align.as_nonzero_usize()
}
}

Expand All @@ -294,7 +306,7 @@ impl const From<Alignment> for usize {
impl cmp::Ord for Alignment {
#[inline]
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_nonzero().get().cmp(&other.as_nonzero().get())
self.as_nonzero_usize().cmp(&other.as_nonzero_usize())
}
}

Expand All @@ -310,7 +322,7 @@ impl cmp::PartialOrd for Alignment {
impl hash::Hash for Alignment {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.as_nonzero().hash(state)
self.as_nonzero_usize().hash(state)
}
}

Expand Down
5 changes: 4 additions & 1 deletion library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ use crate::alloc::Layout;
use crate::clone::TrivialClone;
use crate::marker::{Destruct, DiscriminantKind};
use crate::panic::const_assert;
use crate::ptr::Alignment;
use crate::{clone, cmp, fmt, hash, intrinsics, ptr};

mod alignment;
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
pub use alignment::Alignment;

mod manually_drop;
#[stable(feature = "manually_drop", since = "1.20.0")]
pub use manually_drop::ManuallyDrop;
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,10 @@ use crate::mem::{self, MaybeUninit, SizedTypeProperties};
use crate::num::NonZero;
use crate::{fmt, hash, intrinsics, ub_checks};

mod alignment;
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
pub use alignment::Alignment;
#[deprecated(since = "CURRENT_RUSTC_VERSION", note = "moved from `ptr` to `mem`")]
/// Deprecated re-export of [mem::Alignment].
pub type Alignment = mem::Alignment;

mod metadata;
#[unstable(feature = "ptr_metadata", issue = "81513")]
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl<T: Sized> NonNull<T> {
#[must_use]
#[inline]
pub const fn dangling() -> Self {
let align = crate::ptr::Alignment::of::<T>();
NonNull::without_provenance(align.as_nonzero())
let align = crate::mem::Alignment::of::<T>();
NonNull::without_provenance(align.as_nonzero_usize())
}

/// Converts an address back to a mutable pointer, picking up some previously 'exposed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
let mut _5: std::ptr::NonNull<[bool; 0]>;
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
scope 6 {
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
}
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
}
}
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
scope 7 (inlined std::mem::Alignment::of::<[bool; 0]>) {
}
}
}
Expand Down
Loading
Loading