Skip to content

Commit 4c40153

Browse files
authored
Rollup merge of #121556 - GrigorenkoPV:addr_of, r=Nilstrieb
Use `addr_of!` As per #121303 (comment)
2 parents 9975e84 + 613cb32 commit 4c40153

File tree

42 files changed

+92
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+92
-83
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn get_llvm_object_symbols(
313313
llvm::LLVMRustGetSymbols(
314314
buf.as_ptr(),
315315
buf.len(),
316-
&mut *state as *mut &mut _ as *mut c_void,
316+
std::ptr::addr_of_mut!(*state) as *mut c_void,
317317
callback,
318318
error_callback,
319319
)

compiler/rustc_codegen_llvm/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
169169
fn print_pass_timings(&self) {
170170
unsafe {
171171
let mut size = 0;
172-
let cstr = llvm::LLVMRustPrintPassTimings(&mut size as *mut usize);
172+
let cstr = llvm::LLVMRustPrintPassTimings(std::ptr::addr_of_mut!(size));
173173
if cstr.is_null() {
174174
println!("failed to get pass timings");
175175
} else {
@@ -182,7 +182,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
182182
fn print_statistics(&self) {
183183
unsafe {
184184
let mut size = 0;
185-
let cstr = llvm::LLVMRustPrintStatistics(&mut size as *mut usize);
185+
let cstr = llvm::LLVMRustPrintStatistics(std::ptr::addr_of_mut!(size));
186186
if cstr.is_null() {
187187
println!("failed to get pass stats");
188188
} else {

compiler/rustc_codegen_llvm/src/llvm_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess
435435
&tm,
436436
cpu_cstring.as_ptr(),
437437
callback,
438-
&mut out as *mut &mut dyn PrintBackendInfo as *mut c_void,
438+
std::ptr::addr_of_mut!(out) as *mut c_void,
439439
);
440440
}
441441
}

compiler/rustc_data_structures/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<T> RwLock<T> {
429429
#[inline(always)]
430430
pub fn leak(&self) -> &T {
431431
let guard = self.read();
432-
let ret = unsafe { &*(&*guard as *const T) };
432+
let ret = unsafe { &*std::ptr::addr_of!(*guard) };
433433
std::mem::forget(guard);
434434
ret
435435
}

compiler/rustc_middle/src/query/on_disk_cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'sess> OnDiskCache<'sess> {
233233

234234
for (index, file) in files.iter().enumerate() {
235235
let index = SourceFileIndex(index as u32);
236-
let file_ptr: *const SourceFile = &**file as *const _;
236+
let file_ptr: *const SourceFile = std::ptr::addr_of!(**file);
237237
file_to_file_index.insert(file_ptr, index);
238238
let source_file_id = EncodedSourceFileId::new(tcx, file);
239239
file_index_to_stable_id.insert(index, source_file_id);
@@ -835,7 +835,7 @@ pub struct CacheEncoder<'a, 'tcx> {
835835
impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
836836
#[inline]
837837
fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
838-
self.file_to_file_index[&(&*source_file as *const SourceFile)]
838+
self.file_to_file_index[&std::ptr::addr_of!(*source_file)]
839839
}
840840

841841
/// Encode something with additional information that allows to do some

compiler/rustc_middle/src/ty/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<T> List<T> {
6161
// length) that is 64-byte aligned, thus featuring the necessary
6262
// trailing padding for elements with up to 64-byte alignment.
6363
static EMPTY_SLICE: InOrder<usize, MaxAlign> = InOrder(0, MaxAlign);
64-
unsafe { &*(&EMPTY_SLICE as *const _ as *const List<T>) }
64+
unsafe { &*(std::ptr::addr_of!(EMPTY_SLICE) as *const List<T>) }
6565
}
6666

6767
pub fn len(&self) -> usize {

compiler/stable_mir/src/compiler_interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ where
208208
if TLV.is_set() {
209209
Err(Error::from("StableMIR already running"))
210210
} else {
211-
let ptr: *const () = &context as *const &_ as _;
211+
let ptr: *const () = std::ptr::addr_of!(context) as _;
212212
TLV.set(&Cell::new(ptr), || Ok(f()))
213213
}
214214
}

library/alloc/src/boxed/thin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<T: ?Sized> ThinBox<T> {
176176

177177
fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
178178
// SAFETY: both types are transparent to `NonNull<u8>`
179-
unsafe { &*((&self.ptr) as *const WithOpaqueHeader as *const WithHeader<_>) }
179+
unsafe { &*(core::ptr::addr_of!(self.ptr) as *const WithHeader<_>) }
180180
}
181181
}
182182

library/alloc/src/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
19691969

19701970
// Copy value as bytes
19711971
ptr::copy_nonoverlapping(
1972-
&*src as *const T as *const u8,
1972+
core::ptr::addr_of!(*src) as *const u8,
19731973
ptr::addr_of_mut!((*ptr).value) as *mut u8,
19741974
value_size,
19751975
);
@@ -2440,7 +2440,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Rc<T, A> {
24402440
#[stable(feature = "rust1", since = "1.0.0")]
24412441
impl<T: ?Sized, A: Allocator> fmt::Pointer for Rc<T, A> {
24422442
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2443-
fmt::Pointer::fmt(&(&**self as *const T), f)
2443+
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
24442444
}
24452445
}
24462446

library/alloc/src/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
19141914

19151915
// Copy value as bytes
19161916
ptr::copy_nonoverlapping(
1917-
&*src as *const T as *const u8,
1917+
core::ptr::addr_of!(*src) as *const u8,
19181918
ptr::addr_of_mut!((*ptr).data) as *mut u8,
19191919
value_size,
19201920
);
@@ -3265,7 +3265,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Arc<T, A> {
32653265
#[stable(feature = "rust1", since = "1.0.0")]
32663266
impl<T: ?Sized, A: Allocator> fmt::Pointer for Arc<T, A> {
32673267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3268-
fmt::Pointer::fmt(&(&**self as *const T), f)
3268+
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
32693269
}
32703270
}
32713271

library/core/src/ffi/c_str.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::ffi::c_char;
44
use crate::fmt;
55
use crate::intrinsics;
66
use crate::ops;
7+
use crate::ptr::addr_of;
78
use crate::slice;
89
use crate::slice::memchr;
910
use crate::str;
@@ -603,7 +604,7 @@ impl CStr {
603604
pub const fn to_bytes_with_nul(&self) -> &[u8] {
604605
// SAFETY: Transmuting a slice of `c_char`s to a slice of `u8`s
605606
// is safe on all supported targets.
606-
unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
607+
unsafe { &*(addr_of!(self.inner) as *const [u8]) }
607608
}
608609

609610
/// Yields a <code>&[str]</code> slice if the `CStr` contains valid UTF-8.

library/core/src/iter/adapters/filter_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::iter::{adapters::SourceIter, FusedIterator, InPlaceIterable, TrustedF
22
use crate::mem::{ManuallyDrop, MaybeUninit};
33
use crate::num::NonZero;
44
use crate::ops::{ControlFlow, Try};
5+
use crate::ptr::addr_of;
56
use crate::{array, fmt};
67

78
/// An iterator that uses `f` to both filter and map elements from `iter`.
@@ -98,9 +99,8 @@ where
9899
// SAFETY: Loop conditions ensure the index is in bounds.
99100

100101
unsafe {
101-
let opt_payload_at: *const MaybeUninit<B> = (&val as *const Option<B>)
102-
.byte_add(core::mem::offset_of!(Option<B>, Some.0))
103-
.cast();
102+
let opt_payload_at: *const MaybeUninit<B> =
103+
addr_of!(val).byte_add(core::mem::offset_of!(Option<B>, Some.0)).cast();
104104
let dst = guard.array.as_mut_ptr().add(idx);
105105
crate::ptr::copy_nonoverlapping(opt_payload_at, dst, 1);
106106
crate::mem::forget(val);

library/core/src/ptr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
15531553
// `dst` cannot overlap `src` because the caller has mutable access
15541554
// to `dst` while `src` is owned by this function.
15551555
unsafe {
1556-
copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>());
1556+
copy_nonoverlapping(addr_of!(src) as *const u8, dst as *mut u8, mem::size_of::<T>());
15571557
// We are calling the intrinsic directly to avoid function calls in the generated code.
15581558
intrinsics::forget(src);
15591559
}

library/std/src/os/unix/net/addr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod libc {
2121
fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
2222
// Work with an actual instance of the type since using a null pointer is UB
2323
let base = (addr as *const libc::sockaddr_un).addr();
24-
let path = (&addr.sun_path as *const libc::c_char).addr();
24+
let path = core::ptr::addr_of!(addr.sun_path).addr();
2525
path - base
2626
}
2727

@@ -98,7 +98,7 @@ impl SocketAddr {
9898
unsafe {
9999
let mut addr: libc::sockaddr_un = mem::zeroed();
100100
let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
101-
cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
101+
cvt(f(core::ptr::addr_of_mut!(addr) as *mut _, &mut len))?;
102102
SocketAddr::from_parts(addr, len)
103103
}
104104
}

library/std/src/os/unix/net/ancillary.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(super) fn recv_vectored_with_ancillary_from(
3737
unsafe {
3838
let mut msg_name: libc::sockaddr_un = zeroed();
3939
let mut msg: libc::msghdr = zeroed();
40-
msg.msg_name = &mut msg_name as *mut _ as *mut _;
40+
msg.msg_name = core::ptr::addr_of_mut!(msg_name) as *mut _;
4141
msg.msg_namelen = size_of::<libc::sockaddr_un>() as libc::socklen_t;
4242
msg.msg_iov = bufs.as_mut_ptr().cast();
4343
msg.msg_iovlen = bufs.len() as _;
@@ -70,7 +70,7 @@ pub(super) fn send_vectored_with_ancillary_to(
7070
if let Some(path) = path { sockaddr_un(path)? } else { (zeroed(), 0) };
7171

7272
let mut msg: libc::msghdr = zeroed();
73-
msg.msg_name = &mut msg_name as *mut _ as *mut _;
73+
msg.msg_name = core::ptr::addr_of_mut!(msg_name) as *mut _;
7474
msg.msg_namelen = msg_namelen;
7575
msg.msg_iov = bufs.as_ptr() as *mut _;
7676
msg.msg_iovlen = bufs.len() as _;

library/std/src/os/unix/net/datagram.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl UnixDatagram {
9191
let socket = UnixDatagram::unbound()?;
9292
let (addr, len) = sockaddr_un(path.as_ref())?;
9393

94-
cvt(libc::bind(socket.as_raw_fd(), &addr as *const _ as *const _, len as _))?;
94+
cvt(libc::bind(socket.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len as _))?;
9595

9696
Ok(socket)
9797
}
@@ -124,7 +124,7 @@ impl UnixDatagram {
124124
let socket = UnixDatagram::unbound()?;
125125
cvt(libc::bind(
126126
socket.as_raw_fd(),
127-
&socket_addr.addr as *const _ as *const _,
127+
core::ptr::addr_of!(socket_addr.addr) as *const _,
128128
socket_addr.len as _,
129129
))?;
130130
Ok(socket)
@@ -206,7 +206,7 @@ impl UnixDatagram {
206206
unsafe {
207207
let (addr, len) = sockaddr_un(path.as_ref())?;
208208

209-
cvt(libc::connect(self.as_raw_fd(), &addr as *const _ as *const _, len))?;
209+
cvt(libc::connect(self.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len))?;
210210
}
211211
Ok(())
212212
}
@@ -238,7 +238,7 @@ impl UnixDatagram {
238238
unsafe {
239239
cvt(libc::connect(
240240
self.as_raw_fd(),
241-
&socket_addr.addr as *const _ as *const _,
241+
core::ptr::addr_of!(socket_addr.addr) as *const _,
242242
socket_addr.len,
243243
))?;
244244
}
@@ -505,7 +505,7 @@ impl UnixDatagram {
505505
buf.as_ptr() as *const _,
506506
buf.len(),
507507
MSG_NOSIGNAL,
508-
&addr as *const _ as *const _,
508+
core::ptr::addr_of!(addr) as *const _,
509509
len,
510510
))?;
511511
Ok(count as usize)
@@ -540,7 +540,7 @@ impl UnixDatagram {
540540
buf.as_ptr() as *const _,
541541
buf.len(),
542542
MSG_NOSIGNAL,
543-
&socket_addr.addr as *const _ as *const _,
543+
core::ptr::addr_of!(socket_addr.addr) as *const _,
544544
socket_addr.len,
545545
))?;
546546
Ok(count as usize)

library/std/src/os/unix/net/listener.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ impl UnixListener {
9999
)))]
100100
const backlog: libc::c_int = libc::SOMAXCONN;
101101

102-
cvt(libc::bind(inner.as_inner().as_raw_fd(), &addr as *const _ as *const _, len as _))?;
102+
cvt(libc::bind(
103+
inner.as_inner().as_raw_fd(),
104+
core::ptr::addr_of!(addr) as *const _,
105+
len as _,
106+
))?;
103107
cvt(libc::listen(inner.as_inner().as_raw_fd(), backlog))?;
104108

105109
Ok(UnixListener(inner))
@@ -139,7 +143,7 @@ impl UnixListener {
139143
const backlog: libc::c_int = 128;
140144
cvt(libc::bind(
141145
inner.as_raw_fd(),
142-
&socket_addr.addr as *const _ as *const _,
146+
core::ptr::addr_of!(socket_addr.addr) as *const _,
143147
socket_addr.len as _,
144148
))?;
145149
cvt(libc::listen(inner.as_raw_fd(), backlog))?;
@@ -174,7 +178,7 @@ impl UnixListener {
174178
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
175179
let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
176180
let mut len = mem::size_of_val(&storage) as libc::socklen_t;
177-
let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
181+
let sock = self.0.accept(core::ptr::addr_of_mut!(storage) as *mut _, &mut len)?;
178182
let addr = SocketAddr::from_parts(storage, len)?;
179183
Ok((UnixStream(sock), addr))
180184
}

library/std/src/os/unix/net/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl UnixStream {
9696
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
9797
let (addr, len) = sockaddr_un(path.as_ref())?;
9898

99-
cvt(libc::connect(inner.as_raw_fd(), &addr as *const _ as *const _, len))?;
99+
cvt(libc::connect(inner.as_raw_fd(), core::ptr::addr_of!(addr) as *const _, len))?;
100100
Ok(UnixStream(inner))
101101
}
102102
}
@@ -130,7 +130,7 @@ impl UnixStream {
130130
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
131131
cvt(libc::connect(
132132
inner.as_raw_fd(),
133-
&socket_addr.addr as *const _ as *const _,
133+
core::ptr::addr_of!(socket_addr.addr) as *const _,
134134
socket_addr.len,
135135
))?;
136136
Ok(UnixStream(inner))

library/std/src/os/unix/ucred.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub mod impl_linux {
6262
socket.as_raw_fd(),
6363
SOL_SOCKET,
6464
SO_PEERCRED,
65-
&mut ucred as *mut ucred as *mut c_void,
65+
core::ptr::addr_of_mut!(ucred) as *mut c_void,
6666
&mut ucred_size,
6767
);
6868

@@ -122,7 +122,7 @@ pub mod impl_mac {
122122
socket.as_raw_fd(),
123123
SOL_LOCAL,
124124
LOCAL_PEERPID,
125-
&mut pid as *mut pid_t as *mut c_void,
125+
core::ptr::addr_of_mut!(pid) as *mut c_void,
126126
&mut pid_size,
127127
);
128128

library/std/src/panicking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
502502
// method of calling a catch panic whilst juggling ownership.
503503
let mut data = Data { f: ManuallyDrop::new(f) };
504504

505-
let data_ptr = &mut data as *mut _ as *mut u8;
505+
let data_ptr = core::ptr::addr_of_mut!(data) as *mut u8;
506506
// SAFETY:
507507
//
508508
// Access to the union's fields: this is `std` and we know that the `r#try`

library/std/src/sync/mpmc/zero.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ impl<T> Channel<T> {
182182
// Prepare for blocking until a receiver wakes us up.
183183
let oper = Operation::hook(token);
184184
let mut packet = Packet::<T>::message_on_stack(msg);
185-
inner.senders.register_with_packet(oper, &mut packet as *mut Packet<T> as *mut (), cx);
185+
inner.senders.register_with_packet(
186+
oper,
187+
core::ptr::addr_of_mut!(packet) as *mut (),
188+
cx,
189+
);
186190
inner.receivers.notify();
187191
drop(inner);
188192

@@ -251,7 +255,7 @@ impl<T> Channel<T> {
251255
let mut packet = Packet::<T>::empty_on_stack();
252256
inner.receivers.register_with_packet(
253257
oper,
254-
&mut packet as *mut Packet<T> as *mut (),
258+
core::ptr::addr_of_mut!(packet) as *mut (),
255259
cx,
256260
);
257261
inner.senders.notify();

library/std/src/sys/pal/hermit/net.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl Socket {
207207
buf.as_mut_ptr(),
208208
buf.len(),
209209
flags,
210-
&mut storage as *mut _ as *mut _,
210+
core::ptr::addr_of_mut!(storage) as *mut _,
211211
&mut addrlen,
212212
)
213213
})?;
@@ -323,7 +323,7 @@ impl Socket {
323323
netc::ioctl(
324324
self.as_raw_fd(),
325325
netc::FIONBIO,
326-
&mut nonblocking as *mut _ as *mut core::ffi::c_void,
326+
core::ptr::addr_of_mut!(nonblocking) as *mut core::ffi::c_void,
327327
)
328328
})
329329
.map(drop)

library/std/src/sys/pal/hermit/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub struct Instant(Timespec);
100100
impl Instant {
101101
pub fn now() -> Instant {
102102
let mut time: Timespec = Timespec::zero();
103-
let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, &mut time.t as *mut timespec) };
103+
let _ = unsafe { abi::clock_gettime(CLOCK_MONOTONIC, core::ptr::addr_of_mut!(time.t)) };
104104

105105
Instant(time)
106106
}
@@ -197,7 +197,7 @@ pub const UNIX_EPOCH: SystemTime = SystemTime(Timespec::zero());
197197
impl SystemTime {
198198
pub fn now() -> SystemTime {
199199
let mut time: Timespec = Timespec::zero();
200-
let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, &mut time.t as *mut timespec) };
200+
let _ = unsafe { abi::clock_gettime(CLOCK_REALTIME, core::ptr::addr_of_mut!(time.t)) };
201201

202202
SystemTime(time)
203203
}

0 commit comments

Comments
 (0)