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_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![allow(internal_features)]
#![allow(rustc::default_hash_types)]
#![allow(rustc::potential_query_instability)]
#![cfg_attr(bootstrap, feature(ptr_alignment_type))]
#![cfg_attr(test, feature(test))]
#![deny(unsafe_op_in_unsafe_fn)]
#![feature(allocator_api)]
Expand All @@ -27,7 +28,6 @@
#![feature(never_type)]
#![feature(pattern_type_macro)]
#![feature(pattern_types)]
#![feature(ptr_alignment_type)]
#![feature(rustc_attrs)]
#![feature(sized_hierarchy)]
#![feature(thread_id_value)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// tidy-alphabetical-start
#![allow(internal_features)]
#![allow(rustc::direct_use_of_rustc_type_ir)]
#![cfg_attr(bootstrap, feature(ptr_alignment_type))]
#![cfg_attr(doc, feature(intra_doc_pointers))]
#![feature(allocator_api)]
#![feature(associated_type_defaults)]
Expand All @@ -45,7 +46,6 @@
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(ptr_alignment_type)]
#![feature(range_bounds_is_empty)]
#![feature(rustc_attrs)]
#![feature(sized_hierarchy)]
Expand Down
49 changes: 26 additions & 23 deletions library/core/src/mem/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{cmp, fmt, hash, mem, num};
///
/// Note that particularly large alignments, while representable in this type,
/// are likely not to be supported by actual allocators and linkers.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[derive(Copy)]
#[derive_const(Clone, PartialEq, Eq)]
#[repr(transparent)]
Expand All @@ -37,19 +37,19 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::MIN.as_usize(), 1);
/// ```
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
pub const MIN: Self = Self::new(1).unwrap();

/// Returns the alignment for a type.
///
/// This provides the same numerical value as [`align_of`],
/// but in an `Alignment` instead of a `usize`.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[must_use]
pub const fn of<T>() -> Self {
Expand All @@ -65,14 +65,14 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::of_val(&5i32).as_usize(), 4);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
pub const fn of_val<T: MetaSized>(val: &T) -> Self {
let align = mem::align_of_val(val);
// SAFETY: `align_of_val` returns valid alignment
Expand Down Expand Up @@ -112,14 +112,14 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// #![feature(layout_for_ptr)]
/// 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 All @@ -131,7 +131,8 @@ impl Alignment {
/// not a power of two.
///
/// Note that `0` is not a power of two, nor a valid alignment.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn new(align: usize) -> Option<Self> {
if align.is_power_of_two() {
Expand All @@ -150,7 +151,8 @@ impl Alignment {
///
/// Equivalently, it must be `1 << exp` for some `exp` in `0..usize::BITS`.
/// It must *not* be zero.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(align: usize) -> Self {
Expand All @@ -166,7 +168,8 @@ impl Alignment {
}

/// Returns the alignment as a [`usize`].
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn as_usize(self) -> usize {
// Going through `as_nonzero_usize` helps this be more clearly the inverse of
Expand All @@ -188,7 +191,8 @@ impl Alignment {
}

/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn as_nonzero_usize(self) -> NonZero<usize> {
// This transmutes directly to avoid the UbCheck in `NonZero::new_unchecked`
Expand Down Expand Up @@ -227,7 +231,6 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_mask)]
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
/// use std::ptr::NonNull;
///
Expand All @@ -243,7 +246,7 @@ impl Alignment {
/// assert_eq!(four.mask(Alignment::of::<Align4>().mask()), four);
/// assert_ne!(one.mask(Alignment::of::<Align4>().mask()), one);
/// ```
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[unstable(feature = "ptr_mask", issue = "98290")]
#[inline]
pub const fn mask(self) -> usize {
// SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
Expand All @@ -256,14 +259,14 @@ impl Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
impl fmt::Debug for Alignment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const TryFrom<NonZero<usize>> for Alignment {
type Error = num::TryFromIntError;
Expand All @@ -274,7 +277,7 @@ impl const TryFrom<NonZero<usize>> for Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const TryFrom<usize> for Alignment {
type Error = num::TryFromIntError;
Expand All @@ -285,7 +288,7 @@ impl const TryFrom<usize> for Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const From<Alignment> for NonZero<usize> {
#[inline]
Expand All @@ -294,7 +297,7 @@ impl const From<Alignment> for NonZero<usize> {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const From<Alignment> for usize {
#[inline]
Expand All @@ -303,7 +306,7 @@ impl const From<Alignment> for usize {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl const cmp::Ord for Alignment {
#[inline]
Expand All @@ -312,7 +315,7 @@ impl const cmp::Ord for Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl const cmp::PartialOrd for Alignment {
#[inline]
Expand All @@ -321,7 +324,7 @@ impl const cmp::PartialOrd for Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
impl hash::Hash for Alignment {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
Expand All @@ -330,7 +333,7 @@ impl hash::Hash for Alignment {
}

/// Returns [`Alignment::MIN`], which is valid for any type.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
impl const Default for Alignment {
fn default() -> Alignment {
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::panic::const_assert;
use crate::{clone, cmp, fmt, hash, intrinsics, ptr};

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

mod manually_drop;
Expand Down Expand Up @@ -1287,7 +1287,7 @@ pub trait SizedTypeProperties: Sized {
const ALIGN: usize = intrinsics::align_of::<Self>();

#[doc(hidden)]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[unstable(feature = "sized_type_properties", issue = "none")]
const ALIGNMENT: Alignment = {
// This can't panic since type alignment is always a power of two.
Alignment::new(Self::ALIGN).unwrap()
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/precondition-checks/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
//@ error-pattern: unsafe precondition(s) violated: Alignment::new_unchecked requires

#![feature(ptr_alignment_type)]

fn main() {
unsafe {
std::mem::Alignment::new_unchecked(0);
Expand Down
1 change: 0 additions & 1 deletion tests/ui/traits/const-traits/const-traits-core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const_clone,
const_default,
const_trait_impl,
ptr_alignment_type,
ascii_char,
f16,
f128,
Expand Down
Loading