Skip to content

Commit 014c6f2

Browse files
committed
Use ptr::Alignment for extra coolness points
1 parent 36f5918 commit 014c6f2

File tree

8 files changed

+27
-29
lines changed

8 files changed

+27
-29
lines changed

Diff for: compiler/rustc_data_structures/src/aligned.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::mem;
1+
use std::ptr::Alignment;
22

33
/// Returns the ABI-required minimum alignment of a type in bytes.
44
///
55
/// This is equivalent to [`mem::align_of`], but also works for some unsized
66
/// types (e.g. slices or rustc's `List`s).
7-
pub const fn align_of<T: ?Sized + Aligned>() -> usize {
7+
pub const fn align_of<T: ?Sized + Aligned>() -> Alignment {
88
T::ALIGN
99
}
1010

@@ -19,13 +19,13 @@ pub const fn align_of<T: ?Sized + Aligned>() -> usize {
1919
/// [`mem::align_of<Self>()`]: mem::align_of
2020
pub unsafe trait Aligned {
2121
/// Alignment of `Self`.
22-
const ALIGN: usize;
22+
const ALIGN: Alignment;
2323
}
2424

2525
unsafe impl<T> Aligned for T {
26-
const ALIGN: usize = mem::align_of::<Self>();
26+
const ALIGN: Alignment = Alignment::of::<Self>();
2727
}
2828

2929
unsafe impl<T> Aligned for [T] {
30-
const ALIGN: usize = mem::align_of::<T>();
30+
const ALIGN: Alignment = Alignment::of::<T>();
3131
}

Diff for: compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(lint_reasons)]
3131
#![feature(unwrap_infallible)]
3232
#![feature(strict_provenance)]
33+
#![feature(ptr_alignment_type)]
3334
#![allow(rustc::default_hash_types)]
3435
#![allow(rustc::potential_query_instability)]
3536
#![deny(rustc::untranslatable_diagnostic)]

Diff for: compiler/rustc_data_structures/src/tagged_ptr.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ pub unsafe trait Pointer: Deref {
7070
/// # struct T;
7171
/// # impl Deref for T { type Target = u8; fn deref(&self) -> &u8 { &0 } }
7272
/// # impl T {
73-
/// const BITS: usize = bits_for::<<Self as Deref>::Target>();
73+
/// const BITS: u32 = bits_for::<<Self as Deref>::Target>();
7474
/// # }
7575
/// ```
7676
///
7777
/// [`BITS`]: Pointer::BITS
7878
/// [`Self::Target`]: Deref::Target
79-
const BITS: usize;
79+
const BITS: u32;
8080

8181
/// Turns this pointer into a raw, non-null pointer.
8282
///
@@ -118,7 +118,7 @@ pub unsafe trait Tag: Copy {
118118
/// value.
119119
///
120120
/// [`into_usize`]: Tag::into_usize
121-
const BITS: usize;
121+
const BITS: u32;
122122

123123
/// Turns this tag into an integer.
124124
///
@@ -142,7 +142,7 @@ pub unsafe trait Tag: Copy {
142142
}
143143

144144
unsafe impl<T: ?Sized + Aligned> Pointer for Box<T> {
145-
const BITS: usize = bits_for::<Self::Target>();
145+
const BITS: u32 = bits_for::<Self::Target>();
146146

147147
#[inline]
148148
fn into_ptr(self) -> NonNull<T> {
@@ -158,7 +158,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Box<T> {
158158
}
159159

160160
unsafe impl<T: ?Sized + Aligned> Pointer for Rc<T> {
161-
const BITS: usize = bits_for::<Self::Target>();
161+
const BITS: u32 = bits_for::<Self::Target>();
162162

163163
#[inline]
164164
fn into_ptr(self) -> NonNull<T> {
@@ -174,7 +174,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Rc<T> {
174174
}
175175

176176
unsafe impl<T: ?Sized + Aligned> Pointer for Arc<T> {
177-
const BITS: usize = bits_for::<Self::Target>();
177+
const BITS: u32 = bits_for::<Self::Target>();
178178

179179
#[inline]
180180
fn into_ptr(self) -> NonNull<T> {
@@ -190,7 +190,7 @@ unsafe impl<T: ?Sized + Aligned> Pointer for Arc<T> {
190190
}
191191

192192
unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T {
193-
const BITS: usize = bits_for::<Self::Target>();
193+
const BITS: u32 = bits_for::<Self::Target>();
194194

195195
#[inline]
196196
fn into_ptr(self) -> NonNull<T> {
@@ -206,7 +206,7 @@ unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a T {
206206
}
207207

208208
unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T {
209-
const BITS: usize = bits_for::<Self::Target>();
209+
const BITS: u32 = bits_for::<Self::Target>();
210210

211211
#[inline]
212212
fn into_ptr(self) -> NonNull<T> {
@@ -223,14 +223,8 @@ unsafe impl<'a, T: 'a + ?Sized + Aligned> Pointer for &'a mut T {
223223

224224
/// Returns the number of bits available for use for tags in a pointer to `T`
225225
/// (this is based on `T`'s alignment).
226-
pub const fn bits_for<T: ?Sized + Aligned>() -> usize {
227-
let bits = crate::aligned::align_of::<T>().trailing_zeros();
228-
229-
// This is a replacement for `.try_into().unwrap()` unavailable in `const`
230-
// (it's fine to make an assert here, since this is only called in compile time)
231-
assert!((bits as u128) < usize::MAX as u128);
232-
233-
bits as usize
226+
pub const fn bits_for<T: ?Sized + Aligned>() -> u32 {
227+
crate::aligned::align_of::<T>().as_nonzero().trailing_zeros()
234228
}
235229

236230
/// A tag type used in [`CopyTaggedPtr`] and [`TaggedPtr`] tests.
@@ -245,7 +239,7 @@ enum Tag2 {
245239

246240
#[cfg(test)]
247241
unsafe impl Tag for Tag2 {
248-
const BITS: usize = 2;
242+
const BITS: u32 = 2;
249243

250244
fn into_usize(self) -> usize {
251245
self as _

Diff for: compiler/rustc_data_structures/src/tagged_ptr/copy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ where
116116
self.packed = Self::pack(self.pointer_raw(), tag);
117117
}
118118

119-
const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS;
119+
const TAG_BIT_SHIFT: u32 = usize::BITS - T::BITS;
120120
const ASSERTION: () = { assert!(T::BITS <= P::BITS) };
121121

122122
/// Pack pointer `ptr` that comes from [`P::into_ptr`] with a `tag`,
@@ -298,7 +298,7 @@ where
298298
/// enum Tag2 { B00 = 0b00, B01 = 0b01, B10 = 0b10, B11 = 0b11 };
299299
///
300300
/// unsafe impl Tag for Tag2 {
301-
/// const BITS: usize = 2;
301+
/// const BITS: u32 = 2;
302302
///
303303
/// fn into_usize(self) -> usize { todo!() }
304304
/// unsafe fn from_usize(tag: usize) -> Self { todo!() }

Diff for: compiler/rustc_data_structures/src/tagged_ptr/drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ where
150150
/// enum Tag2 { B00 = 0b00, B01 = 0b01, B10 = 0b10, B11 = 0b11 };
151151
///
152152
/// unsafe impl Tag for Tag2 {
153-
/// const BITS: usize = 2;
153+
/// const BITS: u32 = 2;
154154
///
155155
/// fn into_usize(self) -> usize { todo!() }
156156
/// unsafe fn from_usize(tag: usize) -> Self { todo!() }

Diff for: compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![feature(result_option_inspect)]
6060
#![feature(const_option)]
6161
#![feature(trait_alias)]
62+
#![feature(ptr_alignment_type)]
6263
#![recursion_limit = "512"]
6364
#![allow(rustc::potential_query_instability)]
6465

Diff for: compiler/rustc_middle/src/ty/list.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::arena::Arena;
2-
use rustc_data_structures::aligned::Aligned;
2+
use rustc_data_structures::aligned::{align_of, Aligned};
33
use rustc_serialize::{Encodable, Encoder};
44
use std::alloc::Layout;
55
use std::cmp::Ordering;
@@ -203,13 +203,13 @@ unsafe impl<T: Sync> Sync for List<T> {}
203203
// Layouts of `Equivalent<T>` and `List<T>` are the same, modulo opaque tail,
204204
// thus aligns of `Equivalent<T>` and `List<T>` must be the same.
205205
unsafe impl<T> Aligned for List<T> {
206-
const ALIGN: usize = {
206+
const ALIGN: ptr::Alignment = {
207207
#[repr(C)]
208208
struct Equivalent<T> {
209209
_len: usize,
210210
_data: [T; 0],
211211
}
212212

213-
mem::align_of::<Equivalent<T>>()
213+
align_of::<Equivalent<T>>()
214214
};
215215
}

Diff for: compiler/rustc_middle/src/ty/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ struct ParamTag {
16261626
}
16271627

16281628
unsafe impl rustc_data_structures::tagged_ptr::Tag for ParamTag {
1629-
const BITS: usize = 2;
1629+
const BITS: u32 = 2;
1630+
16301631
#[inline]
16311632
fn into_usize(self) -> usize {
16321633
match self {
@@ -1636,6 +1637,7 @@ unsafe impl rustc_data_structures::tagged_ptr::Tag for ParamTag {
16361637
Self { reveal: traits::Reveal::All, constness: hir::Constness::Const } => 3,
16371638
}
16381639
}
1640+
16391641
#[inline]
16401642
unsafe fn from_usize(ptr: usize) -> Self {
16411643
match ptr {

0 commit comments

Comments
 (0)