From ff187a92d84f352670a9a63e8519eac114456d38 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Sat, 24 Feb 2024 16:02:17 +0300 Subject: [PATCH] library: use `addr_of!` --- library/alloc/src/boxed/thin.rs | 2 +- library/alloc/src/rc.rs | 4 ++-- library/alloc/src/sync.rs | 4 ++-- library/core/src/ffi/c_str.rs | 3 ++- library/core/src/iter/adapters/filter_map.rs | 6 +++--- library/core/src/ptr/mod.rs | 2 +- library/std/src/os/unix/net/addr.rs | 4 ++-- library/std/src/os/unix/net/ancillary.rs | 4 ++-- library/std/src/os/unix/net/datagram.rs | 12 ++++++------ library/std/src/os/unix/net/listener.rs | 10 +++++++--- library/std/src/os/unix/net/stream.rs | 4 ++-- library/std/src/os/unix/ucred.rs | 4 ++-- library/std/src/panicking.rs | 2 +- library/std/src/sync/mpmc/zero.rs | 8 ++++++-- library/std/src/sys/pal/hermit/net.rs | 4 ++-- library/std/src/sys/pal/hermit/time.rs | 4 ++-- library/std/src/sys/pal/sgx/abi/tls/mod.rs | 2 +- library/std/src/sys/pal/unix/fs.rs | 6 +++--- library/std/src/sys/pal/unix/mod.rs | 2 +- library/std/src/sys/pal/unix/net.rs | 2 +- .../std/src/sys/pal/unix/process/process_fuchsia.rs | 4 ++-- library/std/src/sys/pal/unix/process/process_unix.rs | 12 ++++++------ library/std/src/sys/pal/unix/thread.rs | 8 ++++---- library/std/src/sys/pal/unix/thread_local_dtor.rs | 2 +- library/std/src/sys/pal/wasi/mod.rs | 2 +- library/std/src/sys/pal/windows/fs.rs | 8 ++++---- library/std/src/sys/pal/windows/io.rs | 2 +- library/std/src/sys/pal/windows/net.rs | 2 +- library/std/src/sys/pal/windows/pipe.rs | 2 +- library/std/src/sys/pal/windows/process.rs | 6 +++--- library/std/src/sys/pal/windows/rand.rs | 4 ++-- library/std/src/sys/pal/windows/thread_parking.rs | 2 +- library/std/src/sys_common/net.rs | 8 ++++---- library/std/src/sys_common/once/queue.rs | 2 +- library/unwind/src/libunwind.rs | 4 ++-- 35 files changed, 83 insertions(+), 74 deletions(-) diff --git a/library/alloc/src/boxed/thin.rs b/library/alloc/src/boxed/thin.rs index 3b29c144a89..0421a12b3a9 100644 --- a/library/alloc/src/boxed/thin.rs +++ b/library/alloc/src/boxed/thin.rs @@ -176,7 +176,7 @@ fn data(&self) -> *mut u8 { fn with_header(&self) -> &WithHeader<::Metadata> { // SAFETY: both types are transparent to `NonNull` - unsafe { &*((&self.ptr) as *const WithOpaqueHeader as *const WithHeader<_>) } + unsafe { &*(core::ptr::addr_of!(self.ptr) as *const WithHeader<_>) } } } diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index c3d0019be39..084157b97ab 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1969,7 +1969,7 @@ fn from_box_in(src: Box) -> Rc { // Copy value as bytes ptr::copy_nonoverlapping( - &*src as *const T as *const u8, + core::ptr::addr_of!(*src) as *const u8, ptr::addr_of_mut!((*ptr).value) as *mut u8, value_size, ); @@ -2440,7 +2440,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Pointer for Rc { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Pointer::fmt(&(&**self as *const T), f) + fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f) } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 524aa35e045..00f47f5c6e0 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1914,7 +1914,7 @@ fn from_box_in(src: Box) -> Arc { // Copy value as bytes ptr::copy_nonoverlapping( - &*src as *const T as *const u8, + core::ptr::addr_of!(*src) as *const u8, ptr::addr_of_mut!((*ptr).data) as *mut u8, value_size, ); @@ -3265,7 +3265,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Pointer for Arc { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Pointer::fmt(&(&**self as *const T), f) + fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f) } } diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index 248943cf022..20186a2de0f 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -4,6 +4,7 @@ use crate::fmt; use crate::intrinsics; use crate::ops; +use crate::ptr::addr_of; use crate::slice; use crate::slice::memchr; use crate::str; @@ -603,7 +604,7 @@ pub const fn to_bytes(&self) -> &[u8] { pub const fn to_bytes_with_nul(&self) -> &[u8] { // SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s // is safe on all supported targets. - unsafe { &*(&self.inner as *const [c_char] as *const [u8]) } + unsafe { &*(addr_of!(self.inner) as *const [u8]) } } /// Yields a &[str] slice if the `CStr` contains valid UTF-8. diff --git a/library/core/src/iter/adapters/filter_map.rs b/library/core/src/iter/adapters/filter_map.rs index 64bd5b3e2b6..1a5f9e62654 100644 --- a/library/core/src/iter/adapters/filter_map.rs +++ b/library/core/src/iter/adapters/filter_map.rs @@ -2,6 +2,7 @@ use crate::mem::{ManuallyDrop, MaybeUninit}; use crate::num::NonZero; use crate::ops::{ControlFlow, Try}; +use crate::ptr::addr_of; use crate::{array, fmt}; /// An iterator that uses `f` to both filter and map elements from `iter`. @@ -98,9 +99,8 @@ fn drop(&mut self) { // SAFETY: Loop conditions ensure the index is in bounds. unsafe { - let opt_payload_at: *const MaybeUninit = (&val as *const Option) - .byte_add(core::mem::offset_of!(Option, Some.0)) - .cast(); + let opt_payload_at: *const MaybeUninit = + addr_of!(val).byte_add(core::mem::offset_of!(Option, Some.0)).cast(); let dst = guard.array.as_mut_ptr().add(idx); crate::ptr::copy_nonoverlapping(opt_payload_at, dst, 1); crate::mem::forget(val); diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 90b3341f0ad..fc5b08c9801 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1553,7 +1553,7 @@ macro_rules! attempt_swap_as_chunks { // `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::()); + copy_nonoverlapping(addr_of!(src) as *const u8, dst as *mut u8, mem::size_of::()); // We are calling the intrinsic directly to avoid function calls in the generated code. intrinsics::forget(src); } diff --git a/library/std/src/os/unix/net/addr.rs b/library/std/src/os/unix/net/addr.rs index 6c99e8c3620..9757653e02c 100644 --- a/library/std/src/os/unix/net/addr.rs +++ b/library/std/src/os/unix/net/addr.rs @@ -21,7 +21,7 @@ mod libc { fn sun_path_offset(addr: &libc::sockaddr_un) -> usize { // Work with an actual instance of the type since using a null pointer is UB let base = (addr as *const libc::sockaddr_un).addr(); - let path = (&addr.sun_path as *const libc::c_char).addr(); + let path = core::ptr::addr_of!(addr.sun_path).addr(); path - base } @@ -98,7 +98,7 @@ pub(super) fn new(f: F) -> io::Result unsafe { let mut addr: libc::sockaddr_un = mem::zeroed(); let mut len = mem::size_of::() as libc::socklen_t; - cvt(f(&mut addr as *mut _ as *mut _, &mut len))?; + cvt(f(core::ptr::addr_of_mut!(addr) as *mut _, &mut len))?; SocketAddr::from_parts(addr, len) } } diff --git a/library/std/src/os/unix/net/ancillary.rs b/library/std/src/os/unix/net/ancillary.rs index 218536689fd..1d279d6adbc 100644 --- a/library/std/src/os/unix/net/ancillary.rs +++ b/library/std/src/os/unix/net/ancillary.rs @@ -37,7 +37,7 @@ pub(super) fn recv_vectored_with_ancillary_from( unsafe { let mut msg_name: libc::sockaddr_un = zeroed(); let mut msg: libc::msghdr = zeroed(); - msg.msg_name = &mut msg_name as *mut _ as *mut _; + msg.msg_name = core::ptr::addr_of_mut!(msg_name) as *mut _; msg.msg_namelen = size_of::() as libc::socklen_t; msg.msg_iov = bufs.as_mut_ptr().cast(); msg.msg_iovlen = bufs.len() as _; @@ -70,7 +70,7 @@ pub(super) fn send_vectored_with_ancillary_to( if let Some(path) = path { sockaddr_un(path)? } else { (zeroed(), 0) }; let mut msg: libc::msghdr = zeroed(); - msg.msg_name = &mut msg_name as *mut _ as *mut _; + msg.msg_name = core::ptr::addr_of_mut!(msg_name) as *mut _; msg.msg_namelen = msg_namelen; msg.msg_iov = bufs.as_ptr() as *mut _; msg.msg_iovlen = bufs.len() as _; diff --git a/library/std/src/os/unix/net/datagram.rs b/library/std/src/os/unix/net/datagram.rs index 3b7b610fdf9..0b4d955294c 100644 --- a/library/std/src/os/unix/net/datagram.rs +++ b/library/std/src/os/unix/net/datagram.rs @@ -91,7 +91,7 @@ pub fn bind>(path: P) -> io::Result { let socket = UnixDatagram::unbound()?; let (addr, len) = sockaddr_un(path.as_ref())?; - cvt(libc::bind(socket.as_raw_fd(), &addr as *const _ as *const _, len as _))?; + cvt(libc::bind(socket.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len as _))?; Ok(socket) } @@ -124,7 +124,7 @@ pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result { let socket = UnixDatagram::unbound()?; cvt(libc::bind( socket.as_raw_fd(), - &socket_addr.addr as *const _ as *const _, + core::ptr::addr_of!(socket_addr.addr) as *const _, socket_addr.len as _, ))?; Ok(socket) @@ -206,7 +206,7 @@ pub fn connect>(&self, path: P) -> io::Result<()> { unsafe { let (addr, len) = sockaddr_un(path.as_ref())?; - cvt(libc::connect(self.as_raw_fd(), &addr as *const _ as *const _, len))?; + cvt(libc::connect(self.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len))?; } Ok(()) } @@ -238,7 +238,7 @@ pub fn connect_addr(&self, socket_addr: &SocketAddr) -> io::Result<()> { unsafe { cvt(libc::connect( self.as_raw_fd(), - &socket_addr.addr as *const _ as *const _, + core::ptr::addr_of!(socket_addr.addr) as *const _, socket_addr.len, ))?; } @@ -505,7 +505,7 @@ pub fn send_to>(&self, buf: &[u8], path: P) -> io::Result buf.as_ptr() as *const _, buf.len(), MSG_NOSIGNAL, - &addr as *const _ as *const _, + core::ptr::addr_of!(addr) as *const _, len, ))?; Ok(count as usize) @@ -540,7 +540,7 @@ pub fn send_to_addr(&self, buf: &[u8], socket_addr: &SocketAddr) -> io::Result>(path: P) -> io::Result { )))] const backlog: libc::c_int = libc::SOMAXCONN; - cvt(libc::bind(inner.as_inner().as_raw_fd(), &addr as *const _ as *const _, len as _))?; + cvt(libc::bind( + inner.as_inner().as_raw_fd(), + core::ptr::addr_of!(addr) as *const _, + len as _, + ))?; cvt(libc::listen(inner.as_inner().as_raw_fd(), backlog))?; Ok(UnixListener(inner)) @@ -139,7 +143,7 @@ pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result { const backlog: libc::c_int = 128; cvt(libc::bind( inner.as_raw_fd(), - &socket_addr.addr as *const _ as *const _, + core::ptr::addr_of!(socket_addr.addr) as *const _, socket_addr.len as _, ))?; cvt(libc::listen(inner.as_raw_fd(), backlog))?; @@ -174,7 +178,7 @@ pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result { pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> { let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() }; let mut len = mem::size_of_val(&storage) as libc::socklen_t; - let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?; + let sock = self.0.accept(core::ptr::addr_of_mut!(storage) as *mut _, &mut len)?; let addr = SocketAddr::from_parts(storage, len)?; Ok((UnixStream(sock), addr)) } diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index e117f616caf..b1cd504e219 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -96,7 +96,7 @@ pub fn connect>(path: P) -> io::Result { let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?; let (addr, len) = sockaddr_un(path.as_ref())?; - cvt(libc::connect(inner.as_raw_fd(), &addr as *const _ as *const _, len))?; + cvt(libc::connect(inner.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len))?; Ok(UnixStream(inner)) } } @@ -130,7 +130,7 @@ pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result { let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?; cvt(libc::connect( inner.as_raw_fd(), - &socket_addr.addr as *const _ as *const _, + core::ptr::addr_of!(socket_addr.addr) as *const _, socket_addr.len, ))?; Ok(UnixStream(inner)) diff --git a/library/std/src/os/unix/ucred.rs b/library/std/src/os/unix/ucred.rs index 6a0cc2d2c48..6efa74182cc 100644 --- a/library/std/src/os/unix/ucred.rs +++ b/library/std/src/os/unix/ucred.rs @@ -62,7 +62,7 @@ pub fn peer_cred(socket: &UnixStream) -> io::Result { socket.as_raw_fd(), SOL_SOCKET, SO_PEERCRED, - &mut ucred as *mut ucred as *mut c_void, + core::ptr::addr_of_mut!(ucred) as *mut c_void, &mut ucred_size, ); @@ -122,7 +122,7 @@ pub fn peer_cred(socket: &UnixStream) -> io::Result { socket.as_raw_fd(), SOL_LOCAL, LOCAL_PEERPID, - &mut pid as *mut pid_t as *mut c_void, + core::ptr::addr_of_mut!(pid) as *mut c_void, &mut pid_size, ); diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 66b4ec37c8e..ef701d3867a 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -502,7 +502,7 @@ union Data { // method of calling a catch panic whilst juggling ownership. let mut data = Data { f: ManuallyDrop::new(f) }; - let data_ptr = &mut data as *mut _ as *mut u8; + let data_ptr = core::ptr::addr_of_mut!(data) as *mut u8; // SAFETY: // // Access to the union's fields: this is `std` and we know that the `r#try` diff --git a/library/std/src/sync/mpmc/zero.rs b/library/std/src/sync/mpmc/zero.rs index 33f768dcbe9..1b82713edc7 100644 --- a/library/std/src/sync/mpmc/zero.rs +++ b/library/std/src/sync/mpmc/zero.rs @@ -182,7 +182,11 @@ pub(crate) fn send( // Prepare for blocking until a receiver wakes us up. let oper = Operation::hook(token); let mut packet = Packet::::message_on_stack(msg); - inner.senders.register_with_packet(oper, &mut packet as *mut Packet as *mut (), cx); + inner.senders.register_with_packet( + oper, + core::ptr::addr_of_mut!(packet) as *mut (), + cx, + ); inner.receivers.notify(); drop(inner); @@ -251,7 +255,7 @@ pub(crate) fn recv(&self, deadline: Option) -> Result::empty_on_stack(); inner.receivers.register_with_packet( oper, - &mut packet as *mut Packet as *mut (), + core::ptr::addr_of_mut!(packet) as *mut (), cx, ); inner.senders.notify(); diff --git a/library/std/src/sys/pal/hermit/net.rs b/library/std/src/sys/pal/hermit/net.rs index 871a2ccdfa4..1c53796f5d4 100644 --- a/library/std/src/sys/pal/hermit/net.rs +++ b/library/std/src/sys/pal/hermit/net.rs @@ -207,7 +207,7 @@ fn recv_from_with_flags(&self, buf: &mut [u8], flags: i32) -> io::Result<(usize, buf.as_mut_ptr(), buf.len(), flags, - &mut storage as *mut _ as *mut _, + core::ptr::addr_of_mut!(storage) as *mut _, &mut addrlen, ) })?; @@ -323,7 +323,7 @@ pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { netc::ioctl( self.as_raw_fd(), netc::FIONBIO, - &mut nonblocking as *mut _ as *mut core::ffi::c_void, + core::ptr::addr_of_mut!(nonblocking) as *mut core::ffi::c_void, ) }) .map(drop) diff --git a/library/std/src/sys/pal/hermit/time.rs b/library/std/src/sys/pal/hermit/time.rs index b0e9634d229..f289dafd8bc 100644 --- a/library/std/src/sys/pal/hermit/time.rs +++ b/library/std/src/sys/pal/hermit/time.rs @@ -100,7 +100,7 @@ fn hash(&self, state: &mut H) { impl Instant { pub fn now() -> Instant { let mut time: Timespec = Timespec::zero(); - let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) }; + let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, core::ptr::addr_of_mut!(time.t)) }; Instant(time) } @@ -197,7 +197,7 @@ fn sub(self, other: Instant) -> Duration { impl SystemTime { pub fn now() -> SystemTime { let mut time: Timespec = Timespec::zero(); - let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) }; + let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, core::ptr::addr_of_mut!(time.t)) }; SystemTime(time) } diff --git a/library/std/src/sys/pal/sgx/abi/tls/mod.rs b/library/std/src/sys/pal/sgx/abi/tls/mod.rs index 6762a43b483..8a9ea4ac00d 100644 --- a/library/std/src/sys/pal/sgx/abi/tls/mod.rs +++ b/library/std/src/sys/pal/sgx/abi/tls/mod.rs @@ -95,7 +95,7 @@ pub unsafe fn activate(&self) -> ActiveTls<'_> { #[allow(unused)] pub unsafe fn activate_persistent(self: Box) { // FIXME: Needs safety information. See entry.S for `set_tls_ptr` definition. - unsafe { set_tls_ptr((&*self) as *const Tls as _) }; + unsafe { set_tls_ptr(core::ptr::addr_of!(*self) as _) }; mem::forget(self); } diff --git a/library/std/src/sys/pal/unix/fs.rs b/library/std/src/sys/pal/unix/fs.rs index c75323ef775..086cdfe6e94 100644 --- a/library/std/src/sys/pal/unix/fs.rs +++ b/library/std/src/sys/pal/unix/fs.rs @@ -1344,7 +1344,7 @@ pub fn set_times(&self, times: FileTimes) -> io::Result<()> { } cvt(unsafe { libc::fsetattrlist( self.as_raw_fd(), - (&attrlist as *const libc::attrlist).cast::().cast_mut(), + core::ptr::addr_of!(attrlist).cast::().cast_mut(), buf.as_ptr().cast::().cast_mut(), num_times * mem::size_of::(), 0 @@ -1744,7 +1744,7 @@ fn open_from(from: &Path) -> io::Result<(crate::fs::File, crate::fs::Metadata)> #[cfg(target_os = "espidf")] fn open_to_and_set_permissions( to: &Path, - reader_metadata: crate::fs::Metadata, + _reader_metadata: crate::fs::Metadata, ) -> io::Result<(crate::fs::File, crate::fs::Metadata)> { use crate::fs::OpenOptions; let writer = OpenOptions::new().open(to)?; @@ -1918,7 +1918,7 @@ fn fclonefileat( copyfile_state_get( state.0, COPYFILE_STATE_COPIED, - &mut bytes_copied as *mut libc::off_t as *mut libc::c_void, + core::ptr::addr_of_mut!(bytes_copied) as *mut libc::c_void, ) })?; Ok(bytes_copied as u64) diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs index 04b8c5ca916..23287258f2f 100644 --- a/library/std/src/sys/pal/unix/mod.rs +++ b/library/std/src/sys/pal/unix/mod.rs @@ -38,7 +38,7 @@ pub mod time; #[cfg(target_os = "espidf")] -pub fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {} +pub fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {} #[cfg(not(target_os = "espidf"))] // SAFETY: must be called only once during runtime initialization. diff --git a/library/std/src/sys/pal/unix/net.rs b/library/std/src/sys/pal/unix/net.rs index 1b6a6bb2c5c..f4ae7d21781 100644 --- a/library/std/src/sys/pal/unix/net.rs +++ b/library/std/src/sys/pal/unix/net.rs @@ -316,7 +316,7 @@ fn recv_from_with_flags( buf.as_mut_ptr() as *mut c_void, buf.len(), flags, - &mut storage as *mut _ as *mut _, + core::ptr::addr_of_mut!(storage) as *mut _, &mut addrlen, ) })?; diff --git a/library/std/src/sys/pal/unix/process/process_fuchsia.rs b/library/std/src/sys/pal/unix/process/process_fuchsia.rs index b6a74fb4831..23c2be6adf9 100644 --- a/library/std/src/sys/pal/unix/process/process_fuchsia.rs +++ b/library/std/src/sys/pal/unix/process/process_fuchsia.rs @@ -182,7 +182,7 @@ pub fn wait(&mut self) -> io::Result { zx_cvt(zx_object_get_info( self.handle.raw(), ZX_INFO_PROCESS, - &mut proc_info as *mut _ as *mut libc::c_void, + core::ptr::addr_of_mut!(proc_info) as *mut libc::c_void, mem::size_of::(), &mut actual, &mut avail, @@ -219,7 +219,7 @@ pub fn try_wait(&mut self) -> io::Result> { zx_cvt(zx_object_get_info( self.handle.raw(), ZX_INFO_PROCESS, - &mut proc_info as *mut _ as *mut libc::c_void, + core::ptr::addr_of_mut!(proc_info) as *mut libc::c_void, mem::size_of::(), &mut actual, &mut avail, diff --git a/library/std/src/sys/pal/unix/process/process_unix.rs b/library/std/src/sys/pal/unix/process/process_unix.rs index d5a77085725..97cbd1929d3 100644 --- a/library/std/src/sys/pal/unix/process/process_unix.rs +++ b/library/std/src/sys/pal/unix/process/process_unix.rs @@ -694,15 +694,15 @@ union Cmsg { let mut iov = [IoSlice::new(b"")]; let mut msg: libc::msghdr = mem::zeroed(); - msg.msg_iov = &mut iov as *mut _ as *mut _; + msg.msg_iov = core::ptr::addr_of_mut!(iov) as *mut _; msg.msg_iovlen = 1; // only attach cmsg if we successfully acquired the pidfd if pidfd >= 0 { msg.msg_controllen = mem::size_of_val(&cmsg.buf) as _; - msg.msg_control = &mut cmsg.buf as *mut _ as *mut _; + msg.msg_control = core::ptr::addr_of_mut!(cmsg.buf) as *mut _; - let hdr = CMSG_FIRSTHDR(&mut msg as *mut _ as *mut _); + let hdr = CMSG_FIRSTHDR(core::ptr::addr_of_mut!(msg) as *mut _); (*hdr).cmsg_level = SOL_SOCKET; (*hdr).cmsg_type = SCM_RIGHTS; (*hdr).cmsg_len = CMSG_LEN(SCM_MSG_LEN as _) as _; @@ -744,17 +744,17 @@ union Cmsg { let mut msg: libc::msghdr = mem::zeroed(); - msg.msg_iov = &mut iov as *mut _ as *mut _; + msg.msg_iov = core::ptr::addr_of_mut!(iov) as *mut _; msg.msg_iovlen = 1; msg.msg_controllen = mem::size_of::() as _; - msg.msg_control = &mut cmsg as *mut _ as *mut _; + msg.msg_control = core::ptr::addr_of_mut!(cmsg) as *mut _; match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) { Err(_) => return -1, Ok(_) => {} } - let hdr = CMSG_FIRSTHDR(&mut msg as *mut _ as *mut _); + let hdr = CMSG_FIRSTHDR(core::ptr::addr_of_mut!(msg) as *mut _); if hdr.is_null() || (*hdr).cmsg_level != SOL_SOCKET || (*hdr).cmsg_type != SCM_RIGHTS diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 97976407bb4..864de31c6eb 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -239,7 +239,7 @@ pub fn sleep(dur: Duration) { tv_nsec: nsecs, }; secs -= ts.tv_sec as u64; - let ts_ptr = &mut ts as *mut _; + let ts_ptr = core::ptr::addr_of_mut!(ts); if libc::nanosleep(ts_ptr, ts_ptr) == -1 { assert_eq!(os::errno(), libc::EINTR); secs += ts.tv_sec as u64; @@ -418,8 +418,8 @@ pub fn available_parallelism() -> io::Result> { libc::sysctl( mib.as_mut_ptr(), 2, - &mut cpus as *mut _ as *mut _, - &mut cpus_size as *mut _ as *mut _, + core::ptr::addr_of_mut!(cpus) as *mut _, + core::ptr::addr_of_mut!(cpus_size) as *mut _, ptr::null_mut(), 0, ) @@ -864,7 +864,7 @@ pub unsafe fn init() -> Option { .unwrap(); match sysctlbyname.get() { Some(fcn) => { - if fcn(oid.as_ptr(), &mut guard as *mut _ as *mut _, &mut size as *mut _ as *mut _, crate::ptr::null_mut(), 0) == 0 { + if fcn(oid.as_ptr(), core::ptr::addr_of_mut!(guard) as *mut _, core::ptr::addr_of_mut!(size) as *mut _, crate::ptr::null_mut(), 0) == 0 { return guard; } return 1; diff --git a/library/std/src/sys/pal/unix/thread_local_dtor.rs b/library/std/src/sys/pal/unix/thread_local_dtor.rs index 8857f96501c..79b152cece9 100644 --- a/library/std/src/sys/pal/unix/thread_local_dtor.rs +++ b/library/std/src/sys/pal/unix/thread_local_dtor.rs @@ -58,7 +58,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { unsafe extern "C" fn(*mut libc::c_void), >(dtor), t.cast(), - &__dso_handle as *const _ as *mut _, + core::ptr::addr_of!(__dso_handle) as *mut _, ); } return; diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs index 084b8e0e216..e6cbd51e768 100644 --- a/library/std/src/sys/pal/wasi/mod.rs +++ b/library/std/src/sys/pal/wasi/mod.rs @@ -166,7 +166,7 @@ pub fn abort_internal() -> ! { pub fn hashmap_random_keys() -> (u64, u64) { let mut ret = (0u64, 0u64); unsafe { - let base = &mut ret as *mut (u64, u64) as *mut u8; + let base = core::ptr::addr_of_mut!(ret) as *mut u8; let len = mem::size_of_val(&ret); wasi::random_get(base, len).expect("random_get failure"); } diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs index b82a83ae7a3..3a9e7b4660b 100644 --- a/library/std/src/sys/pal/windows/fs.rs +++ b/library/std/src/sys/pal/windows/fs.rs @@ -394,7 +394,7 @@ pub fn file_attr(&self) -> io::Result { cvt(c::GetFileInformationByHandleEx( self.handle.as_raw_handle(), c::FileBasicInfo, - &mut info as *mut _ as *mut c_void, + core::ptr::addr_of_mut!(info) as *mut c_void, size as c::DWORD, ))?; let mut attr = FileAttr { @@ -422,7 +422,7 @@ pub fn file_attr(&self) -> io::Result { cvt(c::GetFileInformationByHandleEx( self.handle.as_raw_handle(), c::FileStandardInfo, - &mut info as *mut _ as *mut c_void, + core::ptr::addr_of_mut!(info) as *mut c_void, size as c::DWORD, ))?; attr.file_size = info.AllocationSize as u64; @@ -638,7 +638,7 @@ fn basic_info(&self) -> io::Result { cvt(c::GetFileInformationByHandleEx( self.handle.as_raw_handle(), c::FileBasicInfo, - &mut info as *mut _ as *mut c_void, + core::ptr::addr_of_mut!(info) as *mut c_void, size as c::DWORD, ))?; Ok(info) @@ -1438,7 +1438,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result { pfrom.as_ptr(), pto.as_ptr(), Some(callback), - &mut size as *mut _ as *mut _, + core::ptr::addr_of_mut!(size) as *mut _, ptr::null_mut(), 0, ) diff --git a/library/std/src/sys/pal/windows/io.rs b/library/std/src/sys/pal/windows/io.rs index b73d9f3ff4c..77b8f3c410e 100644 --- a/library/std/src/sys/pal/windows/io.rs +++ b/library/std/src/sys/pal/windows/io.rs @@ -122,7 +122,7 @@ struct FILE_NAME_INFO { let res = c::GetFileInformationByHandleEx( handle, c::FileNameInfo, - &mut name_info as *mut _ as *mut c_void, + core::ptr::addr_of_mut!(name_info) as *mut c_void, size_of::() as u32, ); if res == 0 { diff --git a/library/std/src/sys/pal/windows/net.rs b/library/std/src/sys/pal/windows/net.rs index e37fbe9ef83..1e6169ea8ec 100644 --- a/library/std/src/sys/pal/windows/net.rs +++ b/library/std/src/sys/pal/windows/net.rs @@ -310,7 +310,7 @@ fn recv_from_with_flags( buf.as_mut_ptr() as *mut _, length, flags, - &mut storage as *mut _ as *mut _, + core::ptr::addr_of_mut!(storage) as *mut _, &mut addrlen, ) }; diff --git a/library/std/src/sys/pal/windows/pipe.rs b/library/std/src/sys/pal/windows/pipe.rs index fd10df82d8b..013f588676a 100644 --- a/library/std/src/sys/pal/windows/pipe.rs +++ b/library/std/src/sys/pal/windows/pipe.rs @@ -375,7 +375,7 @@ struct AsyncResult { let mut overlapped: c::OVERLAPPED = crate::mem::zeroed(); // `hEvent` is unused by `ReadFileEx` and `WriteFileEx`. // Therefore the documentation suggests using it to smuggle a pointer to the callback. - overlapped.hEvent = &mut async_result as *mut _ as *mut _; + overlapped.hEvent = core::ptr::addr_of_mut!(async_result) as *mut _; // Asynchronous read of the pipe. // If successful, `callback` will be called once it completes. diff --git a/library/std/src/sys/pal/windows/process.rs b/library/std/src/sys/pal/windows/process.rs index 6a94d377140..e4ab2ca7da1 100644 --- a/library/std/src/sys/pal/windows/process.rs +++ b/library/std/src/sys/pal/windows/process.rs @@ -350,10 +350,10 @@ pub fn spawn( StartupInfo: si, lpAttributeList: proc_thread_attribute_list.0.as_mut_ptr() as _, }; - si_ptr = &mut si_ex as *mut _ as _; + si_ptr = core::ptr::addr_of_mut!(si_ex) as _; } else { si.cb = mem::size_of::() as c::DWORD; - si_ptr = &mut si as *mut _ as _; + si_ptr = core::ptr::addr_of_mut!(si) as _; } unsafe { @@ -935,7 +935,7 @@ fn make_proc_thread_attribute_list( // It's theoretically possible for the attribute count to exceed a u32 value. // Therefore, we ensure that we don't add more attributes than the buffer was initialized for. for (&attribute, value) in attributes.iter().take(attribute_count as usize) { - let value_ptr = &*value.data as *const (dyn Send + Sync) as _; + let value_ptr = core::ptr::addr_of!(*value.data) as _; cvt(unsafe { c::UpdateProcThreadAttribute( proc_thread_attribute_list.0.as_mut_ptr() as _, diff --git a/library/std/src/sys/pal/windows/rand.rs b/library/std/src/sys/pal/windows/rand.rs index 5d8fd13785a..bd1ae6b0607 100644 --- a/library/std/src/sys/pal/windows/rand.rs +++ b/library/std/src/sys/pal/windows/rand.rs @@ -7,7 +7,7 @@ pub fn hashmap_random_keys() -> (u64, u64) { let ret = unsafe { c::BCryptGenRandom( ptr::null_mut(), - &mut v as *mut _ as *mut u8, + core::ptr::addr_of_mut!(v) as *mut u8, mem::size_of_val(&v) as c::ULONG, c::BCRYPT_USE_SYSTEM_PREFERRED_RNG, ) @@ -28,7 +28,7 @@ fn fallback_rng() -> (u64, u64) { let mut v = (0, 0); let ret = unsafe { - c::RtlGenRandom(&mut v as *mut _ as *mut c_void, mem::size_of_val(&v) as c::ULONG) + c::RtlGenRandom(core::ptr::addr_of_mut!(v) as *mut c_void, mem::size_of_val(&v) as c::ULONG) }; if ret != 0 { v } else { panic!("fallback RNG broken: {}", io::Error::last_os_error()) } diff --git a/library/std/src/sys/pal/windows/thread_parking.rs b/library/std/src/sys/pal/windows/thread_parking.rs index 343b530b15e..ea485d71f5a 100644 --- a/library/std/src/sys/pal/windows/thread_parking.rs +++ b/library/std/src/sys/pal/windows/thread_parking.rs @@ -215,7 +215,7 @@ pub fn unpark(self: Pin<&Self>) { } fn ptr(&self) -> c::LPVOID { - &self.state as *const _ as c::LPVOID + core::ptr::addr_of!(self.state) as c::LPVOID } } diff --git a/library/std/src/sys_common/net.rs b/library/std/src/sys_common/net.rs index de7d31baaaf..581c46af0ea 100644 --- a/library/std/src/sys_common/net.rs +++ b/library/std/src/sys_common/net.rs @@ -70,7 +70,7 @@ pub fn setsockopt( sock.as_raw(), level, option_name, - &option_value as *const T as *const _, + core::ptr::addr_of!(option_value) as *const _, mem::size_of::() as c::socklen_t, ))?; Ok(()) @@ -85,7 +85,7 @@ pub fn getsockopt(sock: &Socket, level: c_int, option_name: c_int) -> i sock.as_raw(), level, option_name, - &mut option_value as *mut T as *mut _, + core::ptr::addr_of_mut!(option_value) as *mut _, &mut option_len, ))?; Ok(option_value) @@ -99,7 +99,7 @@ fn sockname(f: F) -> io::Result unsafe { let mut storage: c::sockaddr_storage = mem::zeroed(); let mut len = mem::size_of_val(&storage) as c::socklen_t; - cvt(f(&mut storage as *mut _ as *mut _, &mut len))?; + cvt(f(core::ptr::addr_of_mut!(storage) as *mut _, &mut len))?; sockaddr_to_addr(&storage, len as usize) } } @@ -444,7 +444,7 @@ pub fn socket_addr(&self) -> io::Result { pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() }; let mut len = mem::size_of_val(&storage) as c::socklen_t; - let sock = self.inner.accept(&mut storage as *mut _ as *mut _, &mut len)?; + let sock = self.inner.accept(core::ptr::addr_of_mut!(storage) as *mut _, &mut len)?; let addr = sockaddr_to_addr(&storage, len as usize)?; Ok((TcpStream { inner: sock }, addr)) } diff --git a/library/std/src/sys_common/once/queue.rs b/library/std/src/sys_common/once/queue.rs index 3cc1df113e3..730cdb768bd 100644 --- a/library/std/src/sys_common/once/queue.rs +++ b/library/std/src/sys_common/once/queue.rs @@ -212,7 +212,7 @@ fn wait(state_and_queue: &AtomicPtr, mut current_state: *mut Masked) { signaled: AtomicBool::new(false), next: current_state.with_addr(current_state.addr() & !STATE_MASK) as *const Waiter, }; - let me = &node as *const Waiter as *const Masked as *mut Masked; + let me = core::ptr::addr_of!(node) as *const Masked as *mut Masked; // Try to slide in the node at the head of the linked list, making sure // that another thread didn't just replace the head of the linked list. diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 1b5f6f9dde3..527c408c89e 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -219,14 +219,14 @@ fn _Unwind_VRS_Set(ctx: *mut _Unwind_Context, pub unsafe fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word { let mut val: _Unwind_Word = core::ptr::null(); _Unwind_VRS_Get(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32, - &mut val as *mut _ as *mut c_void); + core::ptr::addr_of_mut!(val) as *mut c_void); val } pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) { let mut value = value; _Unwind_VRS_Set(ctx, _UVRSC_CORE, reg_index as _Unwind_Word, _UVRSD_UINT32, - &mut value as *mut _ as *mut c_void); + core::ptr::addr_of_mut!(value) as *mut c_void); } pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)