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(bootstrap, feature(unwrap_infallible))]
#![cfg_attr(test, feature(test))]
#![deny(unsafe_op_in_unsafe_fn)]
Expand All @@ -28,7 +29,6 @@
#![feature(nonzero_internals)]
#![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 @@ -28,6 +28,7 @@
#![allow(internal_features)]
#![allow(rustc::direct_use_of_rustc_type_ir)]
#![cfg_attr(bootstrap, feature(debug_closure_helpers))]
#![cfg_attr(bootstrap, feature(ptr_alignment_type))]
#![cfg_attr(doc, feature(intra_doc_pointers))]
#![feature(allocator_ext)]
#![feature(associated_type_defaults)]
Expand All @@ -45,7 +46,6 @@
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(option_into_flat_iter)]
#![feature(ptr_alignment_type)]
#![feature(range_bounds_is_empty)]
#![feature(rustc_attrs)]
#![feature(sized_hierarchy)]
Expand Down
69 changes: 29 additions & 40 deletions library/core/src/mem/alignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(clippy::enum_clike_unportable_variant)]

use crate::marker::MetaSized;
use crate::num::NonZero;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{cmp, fmt, hash, mem, num};
Expand All @@ -10,7 +9,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 +36,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 @@ -71,7 +70,6 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::of_val(&5i32).as_usize(), 4);
Expand All @@ -84,7 +82,6 @@ impl Alignment {
/// `Alignment::of_val()` can be used to learn those alignments:
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
///
/// let a: &dyn ToString = &1234u16;
Expand All @@ -97,8 +94,9 @@ impl Alignment {
/// [type-layout]: ../../reference/type-layout.html#r-layout.primitive
#[inline]
#[must_use]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
pub const fn of_val<T: MetaSized>(val: &T) -> Self {
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
pub const fn of_val<T: ?Sized>(val: &T) -> Self {
let align = mem::align_of_val(val);
// SAFETY: `align_of_val` returns valid alignment
unsafe { Alignment::new_unchecked(align) }
Expand Down Expand Up @@ -141,7 +139,6 @@ impl Alignment {
/// # Examples
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
///
/// assert_eq!(unsafe { Alignment::of_val_raw(&5i32) }.as_usize(), 4);
Expand All @@ -153,8 +150,9 @@ impl Alignment {
/// [type-layout]: ../../reference/type-layout.html#r-layout.primitive
#[inline]
#[must_use]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
pub const unsafe fn of_val_raw<T: MetaSized>(val: *const T) -> Self {
#[stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_stable(feature = "alignment_type", since = "CURRENT_RUSTC_VERSION")]
pub const unsafe fn of_val_raw<T: ?Sized>(val: *const T) -> Self {
// SAFETY: precondition propagated to the caller
let align = unsafe { mem::align_of_val_raw(val) };
// SAFETY: `align_of_val_raw` returns valid alignment
Expand All @@ -165,7 +163,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 @@ -184,7 +183,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 @@ -200,7 +200,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 @@ -210,19 +211,8 @@ impl Alignment {
}

/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[deprecated(
since = "1.96.0",
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")]
#[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 All @@ -242,7 +232,7 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_alignment_type)]
/// use std::ptr::Alignment;
/// use std::mem::Alignment;
///
/// assert_eq!(Alignment::of::<u8>().log2(), 0);
/// assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
Expand All @@ -261,7 +251,6 @@ impl Alignment {
///
/// ```
/// #![feature(ptr_mask)]
/// #![feature(ptr_alignment_type)]
/// use std::mem::Alignment;
/// use std::ptr::NonNull;
///
Expand All @@ -277,7 +266,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 @@ -290,14 +279,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")]
const impl TryFrom<NonZero<usize>> for Alignment {
type Error = num::TryFromIntError;
Expand All @@ -308,7 +297,7 @@ const impl 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")]
const impl TryFrom<usize> for Alignment {
type Error = num::TryFromIntError;
Expand All @@ -319,7 +308,7 @@ const impl 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")]
const impl From<Alignment> for NonZero<usize> {
#[inline]
Expand All @@ -328,7 +317,7 @@ const impl 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")]
const impl From<Alignment> for usize {
#[inline]
Expand All @@ -337,7 +326,7 @@ const impl 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")]
const impl cmp::Ord for Alignment {
#[inline]
Expand All @@ -346,7 +335,7 @@ const impl 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")]
const impl cmp::PartialOrd for Alignment {
#[inline]
Expand All @@ -355,7 +344,7 @@ const impl 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 @@ -364,7 +353,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")]
const impl 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 @@ -39,7 +39,7 @@ use crate::ub_checks::assert_unsafe_precondition;
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 @@ -1477,7 +1477,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
5 changes: 0 additions & 5 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,6 @@ use crate::num::NonZero;
use crate::ops::FnPtr;
use crate::{fmt, hash, intrinsics, ub_checks};

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[deprecated(since = "1.96.0", 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")]
pub use metadata::{DynMetadata, Pointee, Thin, from_raw_parts, from_raw_parts_mut, metadata};
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/fail/alloc/too_large.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(rustc_attrs)]
#![feature(ptr_alignment_type)]

extern "Rust" {
#[rustc_std_internal_symbol]
Expand All @@ -9,6 +8,6 @@ extern "Rust" {
fn main() {
let bytes = isize::MAX as usize + 1;
unsafe {
__rust_alloc(bytes, 1usize.try_into().unwrap()); //~ERROR: larger than half the address space
__rust_alloc(bytes, core::mem::Alignment::MIN); //~ERROR: larger than half the address space
}
}
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/alloc/too_large.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: creating an allocation larger than half the address space
--> tests/fail/alloc/too_large.rs:LL:CC
|
LL | __rust_alloc(bytes, 1usize.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
LL | __rust_alloc(bytes, core::mem::Alignment::MIN);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// https://github.com/rust-lang/miri/issues/3687

#![feature(rustc_attrs)]
#![feature(ptr_alignment_type)]

extern "Rust" {
#[rustc_std_internal_symbol]
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//@compile-flags: -Zmiri-deterministic-concurrency -Zmiri-disable-stacked-borrows

#![feature(rustc_attrs)]
#![feature(ptr_alignment_type)]

use std::thread::spawn;

Expand Down Expand Up @@ -34,7 +33,7 @@ fn main() {
//~^ ERROR: Data race detected between (1) non-atomic read on thread `unnamed-1` and (2) deallocation on thread `unnamed-2`
ptr.0 as *mut _,
std::mem::size_of::<usize>(),
std::mem::align_of::<usize>().try_into().unwrap(),
std::mem::Alignment::of::<usize>(),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | / ... __rust_dealloc(
LL | | ...
LL | | ... ptr.0 as *mut _,
LL | | ... std::mem::size_of::<usize>(),
LL | | ... std::mem::align_of::<usize>().try_into().unwrap(),
LL | | ... std::mem::Alignment::of::<usize>(),
LL | | ... );
| |_______^ (2) just happened here
|
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//@compile-flags: -Zmiri-deterministic-concurrency -Zmiri-disable-stacked-borrows

#![feature(rustc_attrs)]
#![feature(ptr_alignment_type)]

use std::thread::spawn;

Expand All @@ -28,7 +27,7 @@ fn main() {
__rust_dealloc(
ptr.0 as *mut _,
std::mem::size_of::<usize>(),
std::mem::align_of::<usize>().try_into().unwrap(),
std::mem::Alignment::of::<usize>(),
)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ help: ALLOC was deallocated here:
LL | / __rust_dealloc(
LL | | ptr.0 as *mut _,
LL | | std::mem::size_of::<usize>(),
LL | | std::mem::align_of::<usize>().try_into().unwrap(),
LL | | std::mem::Alignment::of::<usize>(),
LL | | )
| |_____________^
= note: this is on thread `unnamed-ID`
Expand Down
Loading
Loading