Skip to content

Commit d3dd34a

Browse files
committed
Auto merge of #127757 - workingjubilee:rollup-4dbks5r, r=workingjubilee
Rollup of 3 pull requests Successful merges: - #127712 (Windows: Remove some unnecessary type aliases) - #127744 (std: `#![deny(unsafe_op_in_unsafe_fn)]` in platform-independent code) - #127750 (Make os/windows and pal/windows default to `#![deny(unsafe_op_in_unsafe_fn)]`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents adeb79d + 476d399 commit d3dd34a

Some content is hidden

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

45 files changed

+307
-292
lines changed

library/std/src/collections/hash/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ where
10181018
K: Borrow<Q>,
10191019
Q: Hash + Eq,
10201020
{
1021-
self.base.get_many_unchecked_mut(ks)
1021+
unsafe { self.base.get_many_unchecked_mut(ks) }
10221022
}
10231023

10241024
/// Returns `true` if the map contains a value for the specified key.

library/std/src/env.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,8 @@ impl Error for VarError {
366366
#[rustc_deprecated_safe_2024]
367367
#[stable(feature = "env", since = "1.0.0")]
368368
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
369-
_set_var(key.as_ref(), value.as_ref())
370-
}
371-
372-
unsafe fn _set_var(key: &OsStr, value: &OsStr) {
373-
os_imp::setenv(key, value).unwrap_or_else(|e| {
369+
let (key, value) = (key.as_ref(), value.as_ref());
370+
unsafe { os_imp::setenv(key, value) }.unwrap_or_else(|e| {
374371
panic!("failed to set environment variable `{key:?}` to `{value:?}`: {e}")
375372
})
376373
}
@@ -433,11 +430,8 @@ unsafe fn _set_var(key: &OsStr, value: &OsStr) {
433430
#[rustc_deprecated_safe_2024]
434431
#[stable(feature = "env", since = "1.0.0")]
435432
pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
436-
_remove_var(key.as_ref())
437-
}
438-
439-
unsafe fn _remove_var(key: &OsStr) {
440-
os_imp::unsetenv(key)
433+
let key = key.as_ref();
434+
unsafe { os_imp::unsetenv(key) }
441435
.unwrap_or_else(|e| panic!("failed to remove environment variable `{key:?}`: {e}"))
442436
}
443437

library/std/src/ffi/os_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl OsString {
184184
#[inline]
185185
#[stable(feature = "os_str_bytes", since = "1.74.0")]
186186
pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec<u8>) -> Self {
187-
OsString { inner: Buf::from_encoded_bytes_unchecked(bytes) }
187+
OsString { inner: unsafe { Buf::from_encoded_bytes_unchecked(bytes) } }
188188
}
189189

190190
/// Converts to an [`OsStr`] slice.
@@ -813,7 +813,7 @@ impl OsStr {
813813
#[inline]
814814
#[stable(feature = "os_str_bytes", since = "1.74.0")]
815815
pub unsafe fn from_encoded_bytes_unchecked(bytes: &[u8]) -> &Self {
816-
Self::from_inner(Slice::from_encoded_bytes_unchecked(bytes))
816+
Self::from_inner(unsafe { Slice::from_encoded_bytes_unchecked(bytes) })
817817
}
818818

819819
#[inline]

library/std/src/io/buffered/bufwriter.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ impl<W: ?Sized + Write> BufWriter<W> {
433433
let old_len = self.buf.len();
434434
let buf_len = buf.len();
435435
let src = buf.as_ptr();
436-
let dst = self.buf.as_mut_ptr().add(old_len);
437-
ptr::copy_nonoverlapping(src, dst, buf_len);
438-
self.buf.set_len(old_len + buf_len);
436+
unsafe {
437+
let dst = self.buf.as_mut_ptr().add(old_len);
438+
ptr::copy_nonoverlapping(src, dst, buf_len);
439+
self.buf.set_len(old_len + buf_len);
440+
}
439441
}
440442

441443
#[inline]

library/std/src/io/cursor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ where
482482
A: Allocator,
483483
{
484484
debug_assert!(vec.capacity() >= pos + buf.len());
485-
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
485+
unsafe { vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()) };
486486
pos + buf.len()
487487
}
488488

library/std/src/io/error/repr_bitpacked.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,14 @@ where
267267
// Using this rather than unwrap meaningfully improves the code
268268
// for callers which only care about one variant (usually
269269
// `Custom`)
270-
core::hint::unreachable_unchecked();
270+
unsafe { core::hint::unreachable_unchecked() };
271271
});
272272
ErrorData::Simple(kind)
273273
}
274-
TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()),
274+
TAG_SIMPLE_MESSAGE => {
275+
// SAFETY: per tag
276+
unsafe { ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()) }
277+
}
275278
TAG_CUSTOM => {
276279
// It would be correct for us to use `ptr::byte_sub` here (see the
277280
// comment above the `wrapping_add` call in `new_custom` for why),

library/std/src/io/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize
382382
where
383383
F: FnOnce(&mut Vec<u8>) -> Result<usize>,
384384
{
385-
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
385+
let mut g = Guard { len: buf.len(), buf: unsafe { buf.as_mut_vec() } };
386386
let ret = f(g.buf);
387387

388388
// SAFETY: the caller promises to only append data to `buf`
389-
let appended = g.buf.get_unchecked(g.len..);
389+
let appended = unsafe { g.buf.get_unchecked(g.len..) };
390390
if str::from_utf8(appended).is_err() {
391391
ret.and_then(|_| Err(Error::INVALID_UTF8))
392392
} else {

library/std/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
#![allow(internal_features)]
253253
#![deny(rustc::existing_doc_keyword)]
254254
#![deny(fuzzy_provenance_casts)]
255+
#![deny(unsafe_op_in_unsafe_fn)]
255256
#![allow(rustdoc::redundant_explicit_links)]
256257
// Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind`
257258
#![deny(ffi_unwind_calls)]
@@ -664,7 +665,7 @@ pub mod alloc;
664665
mod panicking;
665666

666667
#[path = "../../backtrace/src/lib.rs"]
667-
#[allow(dead_code, unused_attributes, fuzzy_provenance_casts)]
668+
#[allow(dead_code, unused_attributes, fuzzy_provenance_casts, unsafe_op_in_unsafe_fn)]
668669
mod backtrace_rs;
669670

670671
// Re-export macros defined in core.

library/std/src/os/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![stable(feature = "os", since = "1.0.0")]
44
#![allow(missing_docs, nonstandard_style, missing_debug_implementations)]
5+
#![allow(unsafe_op_in_unsafe_fn)]
56

67
pub mod raw;
78

library/std/src/os/windows/io/raw.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ fn stdio_handle(raw: RawHandle) -> RawHandle {
159159
impl FromRawHandle for fs::File {
160160
#[inline]
161161
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
162-
let handle = handle as sys::c::HANDLE;
163-
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
164-
OwnedHandle::from_raw_handle(handle),
165-
)))
162+
unsafe {
163+
let handle = handle as sys::c::HANDLE;
164+
fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
165+
OwnedHandle::from_raw_handle(handle),
166+
)))
167+
}
166168
}
167169
}
168170

@@ -260,24 +262,30 @@ impl AsRawSocket for net::UdpSocket {
260262
impl FromRawSocket for net::TcpStream {
261263
#[inline]
262264
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
263-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
264-
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
265+
unsafe {
266+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
267+
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
268+
}
265269
}
266270
}
267271
#[stable(feature = "from_raw_os", since = "1.1.0")]
268272
impl FromRawSocket for net::TcpListener {
269273
#[inline]
270274
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
271-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
272-
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
275+
unsafe {
276+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
277+
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
278+
}
273279
}
274280
}
275281
#[stable(feature = "from_raw_os", since = "1.1.0")]
276282
impl FromRawSocket for net::UdpSocket {
277283
#[inline]
278284
unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
279-
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
280-
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
285+
unsafe {
286+
let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
287+
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
288+
}
281289
}
282290
}
283291

library/std/src/os/windows/io/socket.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl BorrowedSocket<'_> {
7676
#[stable(feature = "io_safety", since = "1.63.0")]
7777
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
7878
assert!(socket != sys::c::INVALID_SOCKET as RawSocket);
79-
Self { socket, _phantom: PhantomData }
79+
unsafe { Self { socket, _phantom: PhantomData } }
8080
}
8181
}
8282

@@ -201,8 +201,10 @@ impl IntoRawSocket for OwnedSocket {
201201
impl FromRawSocket for OwnedSocket {
202202
#[inline]
203203
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
204-
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
205-
Self { socket }
204+
unsafe {
205+
debug_assert_ne!(socket, sys::c::INVALID_SOCKET as RawSocket);
206+
Self { socket }
207+
}
206208
}
207209
}
208210

library/std/src/os/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
#![stable(feature = "rust1", since = "1.0.0")]
2626
#![doc(cfg(windows))]
27+
#![deny(unsafe_op_in_unsafe_fn)]
2728

2829
pub mod ffi;
2930
pub mod fs;

library/std/src/os/windows/process.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1616
#[stable(feature = "process_extensions", since = "1.2.0")]
1717
impl FromRawHandle for process::Stdio {
1818
unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
19-
let handle = sys::handle::Handle::from_raw_handle(handle as *mut _);
19+
let handle = unsafe { sys::handle::Handle::from_raw_handle(handle as *mut _) };
2020
let io = sys::process::Stdio::Handle(handle);
2121
process::Stdio::from_inner(io)
2222
}
@@ -407,7 +407,7 @@ impl CommandExt for process::Command {
407407
attribute: usize,
408408
value: T,
409409
) -> &mut process::Command {
410-
self.as_inner_mut().raw_attribute(attribute, value);
410+
unsafe { self.as_inner_mut().raw_attribute(attribute, value) };
411411
self
412412
}
413413
}

library/std/src/process/tests.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -385,29 +385,25 @@ fn test_interior_nul_in_env_value_is_error() {
385385
#[cfg(windows)]
386386
fn test_creation_flags() {
387387
use crate::os::windows::process::CommandExt;
388-
use crate::sys::c::{BOOL, DWORD, INFINITE};
388+
use crate::sys::c::{BOOL, INFINITE};
389389
#[repr(C)]
390390
struct DEBUG_EVENT {
391-
pub event_code: DWORD,
392-
pub process_id: DWORD,
393-
pub thread_id: DWORD,
391+
pub event_code: u32,
392+
pub process_id: u32,
393+
pub thread_id: u32,
394394
// This is a union in the real struct, but we don't
395395
// need this data for the purposes of this test.
396396
pub _junk: [u8; 164],
397397
}
398398

399399
extern "system" {
400-
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
401-
fn ContinueDebugEvent(
402-
dwProcessId: DWORD,
403-
dwThreadId: DWORD,
404-
dwContinueStatus: DWORD,
405-
) -> BOOL;
400+
fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: u32) -> BOOL;
401+
fn ContinueDebugEvent(dwProcessId: u32, dwThreadId: u32, dwContinueStatus: u32) -> BOOL;
406402
}
407403

408-
const DEBUG_PROCESS: DWORD = 1;
409-
const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
410-
const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;
404+
const DEBUG_PROCESS: u32 = 1;
405+
const EXIT_PROCESS_DEBUG_EVENT: u32 = 5;
406+
const DBG_EXCEPTION_NOT_HANDLED: u32 = 0x80010001;
411407

412408
let mut child =
413409
Command::new("cmd").creation_flags(DEBUG_PROCESS).stdin(Stdio::piped()).spawn().unwrap();

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ impl<T> Channel<T> {
200200
return Err(msg);
201201
}
202202

203-
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
204-
205203
// Write the message into the slot and update the stamp.
206-
slot.msg.get().write(MaybeUninit::new(msg));
207-
slot.stamp.store(token.array.stamp, Ordering::Release);
204+
unsafe {
205+
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
206+
slot.msg.get().write(MaybeUninit::new(msg));
207+
slot.stamp.store(token.array.stamp, Ordering::Release);
208+
}
208209

209210
// Wake a sleeping receiver.
210211
self.receivers.notify();
@@ -291,11 +292,14 @@ impl<T> Channel<T> {
291292
return Err(());
292293
}
293294

294-
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
295-
296295
// Read the message from the slot and update the stamp.
297-
let msg = slot.msg.get().read().assume_init();
298-
slot.stamp.store(token.array.stamp, Ordering::Release);
296+
let msg = unsafe {
297+
let slot: &Slot<T> = &*(token.array.slot as *const Slot<T>);
298+
299+
let msg = slot.msg.get().read().assume_init();
300+
slot.stamp.store(token.array.stamp, Ordering::Release);
301+
msg
302+
};
299303

300304
// Wake a sleeping sender.
301305
self.senders.notify();
@@ -471,7 +475,7 @@ impl<T> Channel<T> {
471475
false
472476
};
473477

474-
self.discard_all_messages(tail);
478+
unsafe { self.discard_all_messages(tail) };
475479
disconnected
476480
}
477481

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<C> Sender<C> {
6363
disconnect(&self.counter().chan);
6464

6565
if self.counter().destroy.swap(true, Ordering::AcqRel) {
66-
drop(Box::from_raw(self.counter));
66+
drop(unsafe { Box::from_raw(self.counter) });
6767
}
6868
}
6969
}
@@ -116,7 +116,7 @@ impl<C> Receiver<C> {
116116
disconnect(&self.counter().chan);
117117

118118
if self.counter().destroy.swap(true, Ordering::AcqRel) {
119-
drop(Box::from_raw(self.counter));
119+
drop(unsafe { Box::from_raw(self.counter) });
120120
}
121121
}
122122
}

0 commit comments

Comments
 (0)