Skip to content

Commit ba1e06a

Browse files
committed
core::mem::Alignemnt: rename as_nonzero to as_nonzero_usize
1 parent f8d658a commit ba1e06a

14 files changed

Lines changed: 37 additions & 20 deletions

compiler/rustc_data_structures/src/tagged_ptr.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ pub unsafe trait Tag: Copy {
5555
/// Returns the number of bits available for use for tags in a pointer to `T`
5656
/// (this is based on `T`'s alignment).
5757
pub const fn bits_for<T: ?Sized + Aligned>() -> u32 {
58-
crate::aligned::align_of::<T>().as_nonzero().trailing_zeros()
58+
let alignment = crate::aligned::align_of::<T>();
59+
#[cfg(bootstrap)]
60+
let alignment = alignment.as_nonzero();
61+
#[cfg(not(bootstrap))]
62+
let alignment = alignment.as_nonzero_usize();
63+
alignment.trailing_zeros()
5964
}
6065

6166
/// Returns the correct [`Tag::BITS`] constant for a set of tag values.

library/alloc/src/raw_vec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ const impl<A: [const] Allocator + [const] Destruct> RawVecInner<A> {
570570
impl<A: Allocator> RawVecInner<A> {
571571
#[inline]
572572
const fn new_in(alloc: A, align: Alignment) -> Self {
573-
let ptr = Unique::from_non_null(NonNull::without_provenance(align.as_nonzero()));
573+
let ptr = Unique::from_non_null(NonNull::without_provenance(align.as_nonzero_usize()));
574574
// `cap: 0` means "unallocated". zero-sized types are ignored.
575575
Self { ptr, cap: ZERO_CAP, alloc }
576576
}

library/core/src/alloc/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl Layout {
268268
#[must_use]
269269
#[inline]
270270
pub const fn dangling_ptr(&self) -> NonNull<u8> {
271-
NonNull::without_provenance(self.align.as_nonzero())
271+
NonNull::without_provenance(self.align.as_nonzero_usize())
272272
}
273273

274274
/// Creates a layout describing the record that can hold a value

library/core/src/mem/alignment.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,28 @@ impl Alignment {
168168
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
169169
#[inline]
170170
pub const fn as_usize(self) -> usize {
171-
// Going through `as_nonzero` helps this be more clearly the inverse of
171+
// Going through `as_nonzero_usize` helps this be more clearly the inverse of
172172
// `new_unchecked`, letting MIR optimizations fold it away.
173173

174-
self.as_nonzero().get()
174+
self.as_nonzero_usize().get()
175175
}
176176

177177
/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
178178
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
179+
#[deprecated(
180+
since = "CURRENT_RUSTC_VERSION",
181+
note = "renamed to `as_nonzero_usize`",
182+
suggestion = "as_nonzero_usize"
183+
)]
179184
#[inline]
180185
pub const fn as_nonzero(self) -> NonZero<usize> {
186+
self.as_nonzero_usize()
187+
}
188+
189+
/// Returns the alignment as a <code>[NonZero]<[usize]></code>.
190+
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
191+
#[inline]
192+
pub const fn as_nonzero_usize(self) -> NonZero<usize> {
181193
// This transmutes directly to avoid the UbCheck in `NonZero::new_unchecked`
182194
// since there's no way for the user to trip that check anyway -- the
183195
// validity invariant of the type would have to have been broken earlier --
@@ -203,7 +215,7 @@ impl Alignment {
203215
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
204216
#[inline]
205217
pub const fn log2(self) -> u32 {
206-
self.as_nonzero().trailing_zeros()
218+
self.as_nonzero_usize().trailing_zeros()
207219
}
208220

209221
/// Returns a bit mask that can be used to match this alignment.
@@ -246,7 +258,7 @@ impl Alignment {
246258
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
247259
impl fmt::Debug for Alignment {
248260
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249-
write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
261+
write!(f, "{:?} (1 << {:?})", self.as_nonzero_usize(), self.log2())
250262
}
251263
}
252264

@@ -277,7 +289,7 @@ impl const TryFrom<usize> for Alignment {
277289
impl const From<Alignment> for NonZero<usize> {
278290
#[inline]
279291
fn from(align: Alignment) -> NonZero<usize> {
280-
align.as_nonzero()
292+
align.as_nonzero_usize()
281293
}
282294
}
283295

@@ -294,7 +306,7 @@ impl const From<Alignment> for usize {
294306
impl cmp::Ord for Alignment {
295307
#[inline]
296308
fn cmp(&self, other: &Self) -> cmp::Ordering {
297-
self.as_nonzero().cmp(&other.as_nonzero())
309+
self.as_nonzero_usize().cmp(&other.as_nonzero_usize())
298310
}
299311
}
300312

@@ -310,7 +322,7 @@ impl cmp::PartialOrd for Alignment {
310322
impl hash::Hash for Alignment {
311323
#[inline]
312324
fn hash<H: hash::Hasher>(&self, state: &mut H) {
313-
self.as_nonzero().hash(state)
325+
self.as_nonzero_usize().hash(state)
314326
}
315327
}
316328

library/core/src/ptr/non_null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<T: Sized> NonNull<T> {
129129
#[inline]
130130
pub const fn dangling() -> Self {
131131
let align = crate::mem::Alignment::of::<T>();
132-
NonNull::without_provenance(align.as_nonzero())
132+
NonNull::without_provenance(align.as_nonzero_usize())
133133
}
134134

135135
/// Converts an address back to a mutable pointer, picking up some previously 'exposed'

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.32bit.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let mut _5: std::ptr::NonNull<[bool; 0]>;
1818
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
1919
scope 6 {
20-
scope 8 (inlined std::mem::Alignment::as_nonzero) {
20+
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
2323
}

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.32bit.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let mut _5: std::ptr::NonNull<[bool; 0]>;
1818
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
1919
scope 6 {
20-
scope 8 (inlined std::mem::Alignment::as_nonzero) {
20+
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
2323
}

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let mut _5: std::ptr::NonNull<[bool; 0]>;
1818
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
1919
scope 6 {
20-
scope 8 (inlined std::mem::Alignment::as_nonzero) {
20+
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
2323
}

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let mut _5: std::ptr::NonNull<[bool; 0]>;
1818
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
1919
scope 6 {
20-
scope 8 (inlined std::mem::Alignment::as_nonzero) {
20+
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
2323
}

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.32bit.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let mut _5: std::ptr::NonNull<[bool; 0]>;
1818
scope 5 (inlined NonNull::<[bool; 0]>::dangling) {
1919
scope 6 {
20-
scope 8 (inlined std::mem::Alignment::as_nonzero) {
20+
scope 8 (inlined std::mem::Alignment::as_nonzero_usize) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
2323
}

0 commit comments

Comments
 (0)