Skip to content

Commit d3dd34a

Browse files
committed
Auto merge of rust-lang#127757 - workingjubilee:rollup-4dbks5r, r=workingjubilee
Rollup of 3 pull requests Successful merges: - rust-lang#127712 (Windows: Remove some unnecessary type aliases) - rust-lang#127744 (std: `#![deny(unsafe_op_in_unsafe_fn)]` in platform-independent code) - rust-lang#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

File tree

45 files changed

+307
-292
lines changed

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)