Skip to content

Commit df6c197

Browse files
committed
Auto merge of rust-lang#121569 - matthiaskrgr:rollup-awglrax, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#121343 (Add examples for some methods on slices) - rust-lang#121374 (match lowering: Split off `test_candidates` into several functions and improve comments) - rust-lang#121474 (Ignore compiletest test directive migration commits) - rust-lang#121515 (promotion: don't promote int::MIN / -1) - rust-lang#121530 (Fix incorrect doc of ScopedJoinHandle::is_finished) - rust-lang#121551 (Forbid use of `extern "C-unwind"` inside standard library) - rust-lang#121556 (Use `addr_of!`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0a0d083 + ae9a855 commit df6c197

40 files changed

+97
-76
lines changed

alloc/src/boxed/thin.rs

Lines changed: 1 addition & 1 deletion
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

alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#![warn(multiple_supertrait_upcastable)]
9393
#![allow(internal_features)]
9494
#![allow(rustdoc::redundant_explicit_links)]
95+
#![deny(ffi_unwind_calls)]
9596
//
9697
// Library features:
9798
// tidy-alphabetical-start

alloc/src/rc.rs

Lines changed: 2 additions & 2 deletions
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

alloc/src/sync.rs

Lines changed: 2 additions & 2 deletions
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

core/src/ffi/c_str.rs

Lines changed: 2 additions & 1 deletion
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.

core/src/iter/adapters/filter_map.rs

Lines changed: 3 additions & 3 deletions
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);

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#![allow(incomplete_features)]
107107
#![warn(multiple_supertrait_upcastable)]
108108
#![allow(internal_features)]
109+
#![deny(ffi_unwind_calls)]
109110
// Do not check link redundancy on bootstraping phase
110111
#![allow(rustdoc::redundant_explicit_links)]
111112
//

core/src/ptr/mod.rs

Lines changed: 1 addition & 1 deletion
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
}

core/src/slice/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ impl<T> [T] {
146146
/// ```
147147
/// let a = [1, 2, 3];
148148
/// assert!(!a.is_empty());
149+
///
150+
/// let b: &[i32] = &[];
151+
/// assert!(b.is_empty());
149152
/// ```
150153
#[stable(feature = "rust1", since = "1.0.0")]
151154
#[rustc_const_stable(feature = "const_slice_is_empty", since = "1.39.0")]
@@ -185,6 +188,9 @@ impl<T> [T] {
185188
/// *first = 5;
186189
/// }
187190
/// assert_eq!(x, &[5, 1, 2]);
191+
///
192+
/// let y: &mut [i32] = &mut [];
193+
/// assert_eq!(None, y.first_mut());
188194
/// ```
189195
#[stable(feature = "rust1", since = "1.0.0")]
190196
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
@@ -297,7 +303,7 @@ impl<T> [T] {
297303
if let [.., last] = self { Some(last) } else { None }
298304
}
299305

300-
/// Returns a mutable reference to the last item in the slice.
306+
/// Returns a mutable reference to the last item in the slice, or `None` if it is empty.
301307
///
302308
/// # Examples
303309
///
@@ -308,6 +314,9 @@ impl<T> [T] {
308314
/// *last = 10;
309315
/// }
310316
/// assert_eq!(x, &[0, 1, 10]);
317+
///
318+
/// let y: &mut [i32] = &mut [];
319+
/// assert_eq!(None, y.last_mut());
311320
/// ```
312321
#[stable(feature = "rust1", since = "1.0.0")]
313322
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]

proc_macro/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#![feature(strict_provenance)]
3737
#![recursion_limit = "256"]
3838
#![allow(internal_features)]
39+
#![deny(ffi_unwind_calls)]
3940

4041
#[unstable(feature = "proc_macro_internals", issue = "27812")]
4142
#[doc(hidden)]

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

Lines changed: 2 additions & 2 deletions
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
}

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

Lines changed: 2 additions & 2 deletions
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 _;

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

Lines changed: 6 additions & 6 deletions
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)

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

Lines changed: 7 additions & 3 deletions
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
}

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

Lines changed: 2 additions & 2 deletions
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))

std/src/os/unix/ucred.rs

Lines changed: 2 additions & 2 deletions
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

std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
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`

std/src/sync/mpmc/zero.rs

Lines changed: 6 additions & 2 deletions
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();

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

Lines changed: 2 additions & 2 deletions
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)

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

Lines changed: 2 additions & 2 deletions
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
}

std/src/sys/pal/sgx/abi/tls/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Tls {
9595
#[allow(unused)]
9696
pub unsafe fn activate_persistent(self: Box<Self>) {
9797
// FIXME: Needs safety information. See entry.S for `set_tls_ptr` definition.
98-
unsafe { set_tls_ptr((&*self) as *const Tls as _) };
98+
unsafe { set_tls_ptr(core::ptr::addr_of!(*self) as _) };
9999
mem::forget(self);
100100
}
101101

0 commit comments

Comments
 (0)