From 014c6f208e59534cf36b11fdf43f8c90a304073f Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 14 Apr 2023 12:29:10 +0000 Subject: [PATCH] Use `ptr::Alignment` for extra coolness points --- compiler/rustc_data_structures/src/aligned.rs | 10 +++---- compiler/rustc_data_structures/src/lib.rs | 1 + .../rustc_data_structures/src/tagged_ptr.rs | 28 ++++++++----------- .../src/tagged_ptr/copy.rs | 4 +-- .../src/tagged_ptr/drop.rs | 2 +- compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/ty/list.rs | 6 ++-- 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() -> usize { +pub const fn align_of() -> Alignment { T::ALIGN } @@ -19,13 +19,13 @@ pub const fn align_of() -> usize { /// [`mem::align_of()`]: mem::align_of pub unsafe trait Aligned { /// Alignment of `Self`. - const ALIGN: usize; + const ALIGN: Alignment; } unsafe impl Aligned for T { - const ALIGN: usize = mem::align_of::(); + const ALIGN: Alignment = Alignment::of::(); } unsafe impl Aligned for [T] { - const ALIGN: usize = mem::align_of::(); + const ALIGN: Alignment = Alignment::of::(); } 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::<::Target>(); + /// const BITS: u32 = bits_for::<::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 Pointer for Box { - const BITS: usize = bits_for::(); + const BITS: u32 = bits_for::(); #[inline] fn into_ptr(self) -> NonNull { @@ -158,7 +158,7 @@ unsafe fn from_ptr(ptr: NonNull) -> Self { } unsafe impl Pointer for Rc { - const BITS: usize = bits_for::(); + const BITS: u32 = bits_for::(); #[inline] fn into_ptr(self) -> NonNull { @@ -174,7 +174,7 @@ unsafe fn from_ptr(ptr: NonNull) -> Self { } unsafe impl Pointer for Arc { - const BITS: usize = bits_for::(); + const BITS: u32 = bits_for::(); #[inline] fn into_ptr(self) -> NonNull { @@ -190,7 +190,7 @@ unsafe fn from_ptr(ptr: NonNull) -> Self { } unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T { - const BITS: usize = bits_for::(); + const BITS: u32 = bits_for::(); #[inline] fn into_ptr(self) -> NonNull { @@ -206,7 +206,7 @@ unsafe fn from_ptr(ptr: NonNull) -> Self { } unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T { - const BITS: usize = bits_for::(); + const BITS: u32 = bits_for::(); #[inline] fn into_ptr(self) -> NonNull { @@ -223,14 +223,8 @@ unsafe fn from_ptr(mut ptr: NonNull) -> Self { /// 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() -> usize { - let bits = crate::aligned::align_of::().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() -> u32 { + crate::aligned::align_of::().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 @@ pub fn set_tag(&mut self, tag: T) { 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 @@ unsafe impl Send for CopyTaggedPtr /// 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 @@ fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { /// 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 Sync for List {} // Layouts of `Equivalent` and `List` are the same, modulo opaque tail, // thus aligns of `Equivalent` and `List` must be the same. unsafe impl Aligned for List { - const ALIGN: usize = { + const ALIGN: ptr::Alignment = { #[repr(C)] struct Equivalent { _len: usize, _data: [T; 0], } - mem::align_of::>() + align_of::>() }; } 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 @@ fn into_usize(self) -> usize { Self { reveal: traits::Reveal::All, constness: hir::Constness::Const } => 3, } } + #[inline] unsafe fn from_usize(ptr: usize) -> Self { match ptr {