diff options
| -rw-r--r-- | compiler/rustc_data_structures/src/aligned.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/tagged_ptr.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/tagged_ptr/copy.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/tagged_ptr/drop.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/list.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 4 |
8 files changed, 27 insertions, 29 deletions
diff --git a/compiler/rustc_data_structures/src/aligned.rs b/compiler/rustc_data_structures/src/aligned.rs index 2d0adfe2ae3..7ac073539fb 100644 --- a/compiler/rustc_data_structures/src/aligned.rs +++ b/compiler/rustc_data_structures/src/aligned.rs @@ -1,10 +1,10 @@ -use std::mem; +use std::ptr::Alignment; /// Returns the ABI-required minimum alignment of a type in bytes. /// /// This is equivalent to [`mem::align_of`], but also works for some unsized /// types (e.g. slices or rustc's `List`s). -pub const fn align_of<T: ?Sized + Aligned>() -> usize { +pub const fn align_of<T: ?Sized + Aligned>() -> Alignment { T::ALIGN } @@ -19,13 +19,13 @@ pub const fn align_of<T: ?Sized + Aligned>() -> usize { /// [`mem::align_of<Self>()`]: mem::align_of pub unsafe trait Aligned { /// Alignment of `Self`. - const ALIGN: usize; + const ALIGN: Alignment; } unsafe impl<T> Aligned for T { - const ALIGN: usize = mem::align_of::<Self>(); + const ALIGN: Alignment = Alignment::of::<Self>(); } unsafe impl<T> Aligned for [T] { - const ALIGN: usize = mem::align_of::<T>(); + const ALIGN: Alignment = Alignment::of::<T>(); } diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index ea1f71d7115..7768e0fdeb1 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -30,6 +30,7 @@ #![feature(lint_reasons)] #![feature(unwrap_infallible)] #![feature(strict_provenance)] +#![feature(ptr_alignment_type)] #![allow(rustc::default_hash_types)] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_data_structures/src/tagged_ptr.rs b/compiler/rustc_data_structures/src/tagged_ptr.rs index 1fc5dad1d95..c26bffac678 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr.rs @@ -70,13 +70,13 @@ pub unsafe trait Pointer: Deref { /// # struct T; /// # impl Deref for T { type Target = u8; fn deref(&self) -> &u8 { &0 } } /// # impl T { - /// const BITS: usize = bits_for::<<Self as Deref>::Target>(); + /// const BITS: u32 = bits_for::<<Self as Deref>::Target>(); /// # } /// ``` /// /// [`BITS`]: Pointer::BITS /// [`Self::Target`]: Deref::Target - const BITS: usize; + const BITS: u32; /// Turns this pointer into a raw, non-null pointer. /// @@ -118,7 +118,7 @@ pub unsafe trait Tag: Copy { /// value. /// /// [`into_usize`]: Tag::into_usize - const BITS: usize; + const BITS: u32; /// Turns this tag into an integer. /// @@ -142,7 +142,7 @@ pub unsafe trait Tag: Copy { } unsafe impl<T: ?Sized + Aligned> Pointer for Box<T> { - const BITS: usize = bits_for::<Self::Target>(); + const BITS: u32 = bits_for::<Self::Target>(); #[inline] fn into_ptr(self) -> NonNull<T> { @@ -158,7 +158,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Box<T> { } unsafe impl<T: ?Sized + Aligned> Pointer for Rc<T> { - const BITS: usize = bits_for::<Self::Target>(); + const BITS: u32 = bits_for::<Self::Target>(); #[inline] fn into_ptr(self) -> NonNull<T> { @@ -174,7 +174,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Rc<T> { } unsafe impl<T: ?Sized + Aligned> Pointer for Arc<T> { - const BITS: usize = bits_for::<Self::Target>(); + const BITS: u32 = bits_for::<Self::Target>(); #[inline] fn into_ptr(self) -> NonNull<T> { @@ -190,7 +190,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Arc<T> { } unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T { - const BITS: usize = bits_for::<Self::Target>(); + const BITS: u32 = bits_for::<Self::Target>(); #[inline] fn into_ptr(self) -> NonNull<T> { @@ -206,7 +206,7 @@ unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T { } unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T { - const BITS: usize = bits_for::<Self::Target>(); + const BITS: u32 = bits_for::<Self::Target>(); #[inline] fn into_ptr(self) -> NonNull<T> { @@ -223,14 +223,8 @@ unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T { /// 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>() -> usize { - let bits = crate::aligned::align_of::<T>().trailing_zeros(); - - // This is a replacement for `.try_into().unwrap()` unavailable in `const` - // (it's fine to make an assert here, since this is only called in compile time) - assert!((bits as u128) < usize::MAX as u128); - - bits as usize +pub const fn bits_for<T: ?Sized + Aligned>() -> u32 { + crate::aligned::align_of::<T>().as_nonzero().trailing_zeros() } /// A tag type used in [`CopyTaggedPtr`] and [`TaggedPtr`] tests. @@ -245,7 +239,7 @@ enum Tag2 { #[cfg(test)] unsafe impl Tag for Tag2 { - const BITS: usize = 2; + const BITS: u32 = 2; fn into_usize(self) -> usize { self as _ diff --git a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs index 83dfdb0bd87..691e92f196a 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs @@ -116,7 +116,7 @@ where self.packed = Self::pack(self.pointer_raw(), tag); } - const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS; + const TAG_BIT_SHIFT: u32 = usize::BITS - T::BITS; const ASSERTION: () = { assert!(T::BITS <= P::BITS) }; /// Pack pointer `ptr` that comes from [`P::into_ptr`] with a `tag`, @@ -298,7 +298,7 @@ where /// enum Tag2 { B00 = 0b00, B01 = 0b01, B10 = 0b10, B11 = 0b11 }; /// /// unsafe impl Tag for Tag2 { -/// const BITS: usize = 2; +/// const BITS: u32 = 2; /// /// fn into_usize(self) -> usize { todo!() } /// unsafe fn from_usize(tag: usize) -> Self { todo!() } diff --git a/compiler/rustc_data_structures/src/tagged_ptr/drop.rs b/compiler/rustc_data_structures/src/tagged_ptr/drop.rs index 6ca6c7d1283..d418c06b7eb 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/drop.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/drop.rs @@ -150,7 +150,7 @@ where /// enum Tag2 { B00 = 0b00, B01 = 0b01, B10 = 0b10, B11 = 0b11 }; /// /// unsafe impl Tag for Tag2 { -/// const BITS: usize = 2; +/// const BITS: u32 = 2; /// /// fn into_usize(self) -> usize { todo!() } /// unsafe fn from_usize(tag: usize) -> Self { todo!() } diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index b4edb02f6c4..51ed66dbafc 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -59,6 +59,7 @@ #![feature(result_option_inspect)] #![feature(const_option)] #![feature(trait_alias)] +#![feature(ptr_alignment_type)] #![recursion_limit = "512"] #![allow(rustc::potential_query_instability)] diff --git a/compiler/rustc_middle/src/ty/list.rs b/compiler/rustc_middle/src/ty/list.rs index 590beef7f7d..30f036e471c 100644 --- a/compiler/rustc_middle/src/ty/list.rs +++ b/compiler/rustc_middle/src/ty/list.rs @@ -1,5 +1,5 @@ use crate::arena::Arena; -use rustc_data_structures::aligned::Aligned; +use rustc_data_structures::aligned::{align_of, Aligned}; use rustc_serialize::{Encodable, Encoder}; use std::alloc::Layout; use std::cmp::Ordering; @@ -203,13 +203,13 @@ unsafe impl<T: Sync> Sync for List<T> {} // Layouts of `Equivalent<T>` and `List<T>` are the same, modulo opaque tail, // thus aligns of `Equivalent<T>` and `List<T>` must be the same. unsafe impl<T> Aligned for List<T> { - const ALIGN: usize = { + const ALIGN: ptr::Alignment = { #[repr(C)] struct Equivalent<T> { _len: usize, _data: [T; 0], } - mem::align_of::<Equivalent<T>>() + align_of::<Equivalent<T>>() }; } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index c856bb25e14..3d47f56d031 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1626,7 +1626,8 @@ struct ParamTag { } unsafe impl rustc_data_structures::tagged_ptr::Tag for ParamTag { - const BITS: usize = 2; + const BITS: u32 = 2; + #[inline] fn into_usize(self) -> usize { match self { @@ -1636,6 +1637,7 @@ unsafe impl rustc_data_structures::tagged_ptr::Tag for ParamTag { Self { reveal: traits::Reveal::All, constness: hir::Constness::Const } => 3, } } + #[inline] unsafe fn from_usize(ptr: usize) -> Self { match ptr { |
