Skip to content

Less as * in library/core #109255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {

fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
if slice.len() == N {
let ptr = slice.as_ptr() as *const [T; N];
let ptr = slice.as_ptr().cast::<[T; N]>();
// SAFETY: ok because we just checked that the length fits
unsafe { Ok(&*ptr) }
} else {
Expand All @@ -280,7 +280,7 @@ impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {

fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
if slice.len() == N {
let ptr = slice.as_mut_ptr() as *mut [T; N];
let ptr = slice.as_mut_ptr().cast::<[T; N]>();
// SAFETY: ok because we just checked that the length fits
unsafe { Ok(&mut *ptr) }
} else {
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ impl<T, const N: usize> Cell<[T; N]> {
#[unstable(feature = "as_array_of_cells", issue = "88248")]
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
// SAFETY: `Cell<T>` has the same memory layout as `T`.
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
unsafe { &*ptr::from_ref(self).cast::<[Cell<T>; N]>() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ mod impls {
#[inline]
fn hash_slice<H: ~const Hasher>(data: &[$ty], state: &mut H) {
let newlen = mem::size_of_val(data);
let ptr = data.as_ptr() as *const u8;
let ptr = data.as_ptr().cast::<u8>();
// SAFETY: `ptr` is valid and aligned, as this macro is only used
// for numeric primitives which have no padding. The new slice only
// spans across `data` and is never mutated, and its total size is the
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/iter/adapters/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ where
unsafe {
ptr::copy_nonoverlapping(
self.as_ref().as_ptr(),
raw_array.as_mut_ptr() as *mut T,
raw_array.as_mut_ptr().cast::<T>(),
len,
);
let _ = self.advance_by(len);
Expand All @@ -234,7 +234,7 @@ where
// SAFETY: `len` is larger than the array size. Copy a fixed amount here to fully initialize
// the array.
unsafe {
ptr::copy_nonoverlapping(self.as_ref().as_ptr(), raw_array.as_mut_ptr() as *mut T, N);
ptr::copy_nonoverlapping(self.as_ref().as_ptr(), raw_array.as_mut_ptr().cast::<T>(), N);
let _ = self.advance_by(N);
Ok(MaybeUninit::array_assume_init(raw_array))
}
Expand Down
19 changes: 10 additions & 9 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ impl<T> MaybeUninit<T> {
#[inline(always)]
pub const fn as_ptr(&self) -> *const T {
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
self as *const _ as *const T
// FIXME: consider `ptr::from_ref` once that's const-stable
ptr::addr_of!(*self).cast::<T>()
}

/// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
Expand Down Expand Up @@ -566,7 +567,7 @@ impl<T> MaybeUninit<T> {
#[inline(always)]
pub const fn as_mut_ptr(&mut self) -> *mut T {
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
self as *mut _ as *mut T
ptr::from_mut(self).cast::<T>()
}

/// Extracts the value from the `MaybeUninit<T>` container. This is a great way
Expand Down Expand Up @@ -947,7 +948,7 @@ impl<T> MaybeUninit<T> {
// And thus the conversion is safe
let ret = unsafe {
intrinsics::assert_inhabited::<[T; N]>();
(&array as *const _ as *const [T; N]).read()
ptr::from_ref(&array).cast::<[T; N]>().read()
};

// FIXME: required to avoid `~const Destruct` bound
Expand Down Expand Up @@ -1002,15 +1003,15 @@ impl<T> MaybeUninit<T> {
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[inline(always)]
pub const fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T {
this.as_ptr() as *const T
this.as_ptr().cast::<T>()
}

/// Gets a mutable pointer to the first element of the array.
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[inline(always)]
pub const fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
this.as_mut_ptr() as *mut T
this.as_mut_ptr().cast::<T>()
}

/// Copies the elements from `src` to `this`, returning a mutable reference to the now initialized contents of `this`.
Expand Down Expand Up @@ -1182,7 +1183,7 @@ impl<T> MaybeUninit<T> {
pub fn as_bytes(&self) -> &[MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts(self.as_ptr() as *const MaybeUninit<u8>, mem::size_of::<T>())
slice::from_raw_parts(self.as_ptr().cast::<MaybeUninit<u8>>(), mem::size_of::<T>())
}
}

Expand Down Expand Up @@ -1214,7 +1215,7 @@ impl<T> MaybeUninit<T> {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts_mut(
self.as_mut_ptr() as *mut MaybeUninit<u8>,
self.as_mut_ptr().cast::<MaybeUninit<u8>>(),
mem::size_of::<T>(),
)
}
Expand Down Expand Up @@ -1244,7 +1245,7 @@ impl<T> MaybeUninit<T> {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts(
this.as_ptr() as *const MaybeUninit<u8>,
this.as_ptr().cast::<MaybeUninit<u8>>(),
this.len() * mem::size_of::<T>(),
)
}
Expand Down Expand Up @@ -1277,7 +1278,7 @@ impl<T> MaybeUninit<T> {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts_mut(
this.as_mut_ptr() as *mut MaybeUninit<u8>,
this.as_mut_ptr().cast::<MaybeUninit<u8>>(),
this.len() * mem::size_of::<T>(),
)
}
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 @@ -1054,12 +1054,12 @@ pub const unsafe fn transmute_copy<Src, Dst>(src: &Src) -> Dst {
if align_of::<Dst>() > align_of::<Src>() {
// SAFETY: `src` is a reference which is guaranteed to be valid for reads.
// The caller must guarantee that the actual transmutation is safe.
unsafe { ptr::read_unaligned(src as *const Src as *const Dst) }
unsafe { ptr::from_ref(src).cast::<Dst>().read_unaligned() }
} else {
// SAFETY: `src` is a reference which is guaranteed to be valid for reads.
// We just checked that `src as *const Dst` was properly aligned.
// The caller must guarantee that the actual transmutation is safe.
unsafe { ptr::read(src as *const Src as *const Dst) }
unsafe { ptr::from_ref(src).cast::<Dst>().read() }
}
}

Expand Down
8 changes: 4 additions & 4 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T: ?Sized> *const T {
}

// SAFETY: The two versions are equivalent at runtime.
unsafe { const_eval_select((self as *const u8,), const_impl, runtime_impl) }
unsafe { const_eval_select((self.cast::<u8>(),), const_impl, runtime_impl) }
}

/// Casts to a pointer of another type.
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<T: ?Sized> *const T {
where
U: ?Sized,
{
from_raw_parts::<U>(self as *const (), metadata(meta))
from_raw_parts::<U>(self.cast::<()>(), metadata(meta))
}

/// Changes constness without changing the type.
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<T: ?Sized> *const T {
{
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
if self.is_null() { None } else { Some(unsafe { &*self.cast::<MaybeUninit<T>>() }) }
}

/// Calculates the offset from a pointer.
Expand Down Expand Up @@ -822,7 +822,7 @@ impl<T: ?Sized> *const T {
where
T: Sized,
{
match intrinsics::ptr_guaranteed_cmp(self as _, other as _) {
match intrinsics::ptr_guaranteed_cmp(self, other) {
2 => None,
other => Some(other == 1),
}
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
// Also, since we just wrote a valid value into `tmp`, it is guaranteed
// to be properly initialized.
unsafe {
copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>());
copy_nonoverlapping(src.cast::<u8>(), tmp.as_mut_ptr().cast::<u8>(), mem::size_of::<T>());
tmp.assume_init()
}
}
Expand Down Expand Up @@ -1473,7 +1473,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
// `dst` cannot overlap `src` because the caller has mutable access
// to `dst` while `src` is owned by this function.
unsafe {
copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>());
copy_nonoverlapping(addr_of!(src).cast::<u8>(), dst.cast::<u8>(), mem::size_of::<T>());
// We are calling the intrinsic directly to avoid function calls in the generated code.
intrinsics::forget(src);
}
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl<T: ?Sized> *mut T {
{
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
unsafe { self.cast_const().as_uninit_ref() }
}

/// Calculates the offset from a pointer.
Expand Down Expand Up @@ -476,7 +476,7 @@ impl<T: ?Sized> *mut T {
// SAFETY: the caller must uphold the safety contract for `offset`.
// The obtained pointer is valid for writes since the caller must
// guarantee that it points to the same allocated object as `self`.
unsafe { intrinsics::offset(self, count) as *mut T }
unsafe { intrinsics::offset(self, count).cast_mut() }
}

/// Calculates the offset from a pointer in bytes.
Expand Down Expand Up @@ -555,7 +555,7 @@ impl<T: ?Sized> *mut T {
T: Sized,
{
// SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
unsafe { intrinsics::arith_offset(self, count) as *mut T }
unsafe { intrinsics::arith_offset(self, count).cast_mut() }
}

/// Calculates the offset from a pointer in bytes using wrapping arithmetic.
Expand Down Expand Up @@ -717,7 +717,7 @@ impl<T: ?Sized> *mut T {
{
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
if self.is_null() { None } else { Some(unsafe { &mut *(self as *mut MaybeUninit<T>) }) }
if self.is_null() { None } else { Some(unsafe { &mut *self.cast::<MaybeUninit<T>>() }) }
}

/// Returns whether two pointers are guaranteed to be equal.
Expand All @@ -744,7 +744,7 @@ impl<T: ?Sized> *mut T {
where
T: Sized,
{
(self as *const T).guaranteed_eq(other as _)
self.cast_const().guaranteed_eq(other.cast_const())
}

/// Returns whether two pointers are guaranteed to be inequal.
Expand All @@ -771,7 +771,7 @@ impl<T: ?Sized> *mut T {
where
T: Sized,
{
(self as *const T).guaranteed_ne(other as _)
self.cast_const().guaranteed_ne(other.cast_const())
}

/// Calculates the distance between two pointers. The returned value is in
Expand Down Expand Up @@ -864,7 +864,7 @@ impl<T: ?Sized> *mut T {
T: Sized,
{
// SAFETY: the caller must uphold the safety contract for `offset_from`.
unsafe { (self as *const T).offset_from(origin) }
unsafe { self.cast_const().offset_from(origin) }
}

/// Calculates the distance between two pointers. The returned value is in
Expand Down Expand Up @@ -955,7 +955,7 @@ impl<T: ?Sized> *mut T {
T: Sized,
{
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
unsafe { (self as *const T).sub_ptr(origin) }
unsafe { self.cast_const().sub_ptr(origin) }
}

/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/slice/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ fn is_ascii(s: &[u8]) -> bool {

let start = s.as_ptr();
// SAFETY: We verify `len < USIZE_SIZE` above.
let first_word = unsafe { (start as *const usize).read_unaligned() };
let first_word = unsafe { start.cast::<usize>().read_unaligned() };

if contains_nonascii(first_word) {
return false;
Expand All @@ -283,7 +283,7 @@ fn is_ascii(s: &[u8]) -> bool {

// SAFETY: word_ptr is the (properly aligned) usize ptr we use to read the
// middle chunk of the slice.
let mut word_ptr = unsafe { start.add(offset_to_aligned) as *const usize };
let mut word_ptr = unsafe { start.add(offset_to_aligned).cast::<usize>() };

// `byte_pos` is the byte index of `word_ptr`, used for loop end checks.
let mut byte_pos = offset_to_aligned;
Expand Down Expand Up @@ -322,7 +322,7 @@ fn is_ascii(s: &[u8]) -> bool {
debug_assert!(byte_pos <= len && len - byte_pos <= USIZE_SIZE);

// SAFETY: This relies on `len >= USIZE_SIZE`, which we check at the start.
let last_word = unsafe { (start.add(len - USIZE_SIZE) as *const usize).read_unaligned() };
let last_word = unsafe { start.add(len - USIZE_SIZE).cast::<usize>().read_unaligned() };

!contains_nonascii(last_word)
}
4 changes: 2 additions & 2 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where
// The two slices have been checked to have the same size above.
unsafe {
let size = mem::size_of_val(self);
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
memcmp(self.as_ptr().cast::<u8>(), other.as_ptr().cast::<u8>(), size) == 0
}
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ impl SliceContains for i8 {
// as `*const u8` is safe. The `x.as_ptr()` comes from a reference and is thus guaranteed
// to be valid for reads for the length of the slice `x.len()`, which cannot be larger
// than `isize::MAX`. The returned slice is never mutated.
let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr().cast::<u8>(), x.len()) };
memchr::memchr(byte, bytes).is_some()
}
}
2 changes: 1 addition & 1 deletion library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a, T> Iter<'a, T> {
let end =
if T::IS_ZST { ptr.wrapping_byte_add(slice.len()) } else { ptr.add(slice.len()) };

Self { ptr: NonNull::new_unchecked(ptr as *mut T), end, _marker: PhantomData }
Self { ptr: NonNull::new_unchecked(ptr.cast_mut()), end, _marker: PhantomData }
}
}

Expand Down
12 changes: 6 additions & 6 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ impl<T> [T] {
pub fn split_array_ref<const N: usize>(&self) -> (&[T; N], &[T]) {
let (a, b) = self.split_at(N);
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
unsafe { (&*(a.as_ptr() as *const [T; N]), b) }
unsafe { (&*a.as_ptr().cast::<[T; N]>(), b) }
}

/// Divides one mutable slice into an array and a remainder slice at an index.
Expand Down Expand Up @@ -1833,7 +1833,7 @@ impl<T> [T] {
pub fn split_array_mut<const N: usize>(&mut self) -> (&mut [T; N], &mut [T]) {
let (a, b) = self.split_at_mut(N);
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
unsafe { (&mut *(a.as_mut_ptr() as *mut [T; N]), b) }
unsafe { (&mut *a.as_mut_ptr().cast::<[T; N]>(), b) }
}

/// Divides one slice into an array and a remainder slice at an index from
Expand Down Expand Up @@ -1879,7 +1879,7 @@ impl<T> [T] {
assert!(N <= self.len());
let (a, b) = self.split_at(self.len() - N);
// SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at)
unsafe { (a, &*(b.as_ptr() as *const [T; N])) }
unsafe { (a, &*b.as_ptr().cast::<[T; N]>()) }
}

/// Divides one mutable slice into an array and a remainder slice at an
Expand Down Expand Up @@ -1913,7 +1913,7 @@ impl<T> [T] {
assert!(N <= self.len());
let (a, b) = self.split_at_mut(self.len() - N);
// SAFETY: b points to [T; N]? Yes it's [T] of length N (checked by split_at_mut)
unsafe { (a, &mut *(b.as_mut_ptr() as *mut [T; N])) }
unsafe { (a, &mut *b.as_mut_ptr().cast::<[T; N]>()) }
}

/// Returns an iterator over subslices separated by elements that match
Expand Down Expand Up @@ -3582,7 +3582,7 @@ impl<T> [T] {
unsafe {
(
left,
from_raw_parts(rest.as_ptr() as *const U, us_len),
from_raw_parts(rest.as_ptr().cast::<U>(), us_len),
from_raw_parts(rest.as_ptr().add(rest.len() - ts_len), ts_len),
)
}
Expand Down Expand Up @@ -3652,7 +3652,7 @@ impl<T> [T] {
unsafe {
(
left,
from_raw_parts_mut(mut_ptr as *mut U, us_len),
from_raw_parts_mut(mut_ptr.cast::<U>(), us_len),
from_raw_parts_mut(mut_ptr.add(rest_len - ts_len), ts_len),
)
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/slice/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mut mid: *mut T, mut right: usize)
// Algorithm 2
// The `[T; 0]` here is to ensure this is appropriately aligned for T
let mut rawarray = MaybeUninit::<(BufType, [T; 0])>::uninit();
let buf = rawarray.as_mut_ptr() as *mut T;
let buf = rawarray.as_mut_ptr().cast::<T>();
// SAFETY: `mid-left <= mid-left+right < mid+right`
let dim = unsafe { mid.sub(left).add(right) };
if left <= right {
Expand Down
Loading