about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-07-17 03:31:18 +0000
committerbors <bors@rust-lang.org>2024-07-17 03:31:18 +0000
commita28b35eb35ef874d068b8a66e9ff7460fa23a672 (patch)
tree743286dccaf8522f04a2659b7e1029b4eae4d0e4 /library/std/src
parent1a6e777c3c9e5dd57e1e62d95e13eeebaaebac04 (diff)
parent1a1b44fcae5eddac579b4102d96a0ec24b21c569 (diff)
downloadrust-a28b35eb35ef874d068b8a66e9ff7460fa23a672.tar.gz
rust-a28b35eb35ef874d068b8a66e9ff7460fa23a672.zip
Auto merge of #127840 - tgross35:rollup-jfkg1dq, r=tgross35
Rollup of 9 pull requests

Successful merges:

 - #125206 (Simplify environment variable examples)
 - #126271 (Skip fast path for dec2flt when optimize_for_size)
 - #126776 (Clean up more comments near use declarations)
 - #127444 (`impl Send + Sync` and override `count` for the `CStr::bytes` iterator)
 - #127512 (Terminate `--print link-args` output with newline)
 - #127792 (std: Use `read_unaligned` for reads from DWARF)
 - #127807 (Use futex.rs for Windows thread parking)
 - #127833 (zkvm: add `#[forbid(unsafe_op_in_unsafe_fn)]` in `stdlib`)
 - #127836 (std: Forbid unwrapped unsafe ops in xous and uefi modules)

Failed merges:

 - #127813 (Prevent double reference in generic futex)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/env.rs14
-rw-r--r--library/std/src/lib.rs1
-rw-r--r--library/std/src/os/fortanix_sgx/mod.rs1
-rw-r--r--library/std/src/os/uefi/mod.rs1
-rw-r--r--library/std/src/os/xous/mod.rs1
-rw-r--r--library/std/src/prelude/common.rs3
-rw-r--r--library/std/src/prelude/mod.rs3
-rw-r--r--library/std/src/rt.rs1
-rw-r--r--library/std/src/sys/pal/hermit/futex.rs5
-rw-r--r--library/std/src/sys/pal/uefi/mod.rs1
-rw-r--r--library/std/src/sys/pal/unix/futex.rs5
-rw-r--r--library/std/src/sys/pal/wasi/mod.rs9
-rw-r--r--library/std/src/sys/pal/wasip2/mod.rs9
-rw-r--r--library/std/src/sys/pal/wasm/atomics/futex.rs5
-rw-r--r--library/std/src/sys/pal/windows/futex.rs5
-rw-r--r--library/std/src/sys/pal/xous/mod.rs2
-rw-r--r--library/std/src/sys/pal/zkvm/alloc.rs2
-rw-r--r--library/std/src/sys/pal/zkvm/mod.rs1
-rw-r--r--library/std/src/sys/personality/dwarf/mod.rs26
-rw-r--r--library/std/src/sys/sync/mutex/futex.rs21
-rw-r--r--library/std/src/sys/sync/thread_parking/futex.rs17
-rw-r--r--library/std/src/sys/sync/thread_parking/mod.rs7
-rw-r--r--library/std/src/sys/sync/thread_parking/windows7.rs (renamed from library/std/src/sys/sync/thread_parking/windows.rs)0
23 files changed, 79 insertions, 61 deletions
diff --git a/library/std/src/env.rs b/library/std/src/env.rs
index 36add02d68c..fc9b8cfd46d 100644
--- a/library/std/src/env.rs
+++ b/library/std/src/env.rs
@@ -120,11 +120,8 @@ pub struct VarsOs {
 /// # Examples
 ///
 /// ```
-/// use std::env;
-///
-/// // We will iterate through the references to the element returned by
-/// // env::vars();
-/// for (key, value) in env::vars() {
+/// // Print all environment variables.
+/// for (key, value) in std::env::vars() {
 ///     println!("{key}: {value}");
 /// }
 /// ```
@@ -150,11 +147,8 @@ pub fn vars() -> Vars {
 /// # Examples
 ///
 /// ```
-/// use std::env;
-///
-/// // We will iterate through the references to the element returned by
-/// // env::vars_os();
-/// for (key, value) in env::vars_os() {
+/// // Print all environment variables.
+/// for (key, value) in std::env::vars_os() {
 ///     println!("{key:?}: {value:?}");
 /// }
 /// ```
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index d4d68c2068d..f0a73a308a4 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -470,7 +470,6 @@ pub mod rt;
 // The Rust prelude
 pub mod prelude;
 
-// Public module declarations and re-exports
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use alloc_crate::borrow;
 #[stable(feature = "rust1", since = "1.0.0")]
diff --git a/library/std/src/os/fortanix_sgx/mod.rs b/library/std/src/os/fortanix_sgx/mod.rs
index 39a42f4e17f..b31dc06f8df 100644
--- a/library/std/src/os/fortanix_sgx/mod.rs
+++ b/library/std/src/os/fortanix_sgx/mod.rs
@@ -28,7 +28,6 @@ pub mod usercalls {
         pub use crate::sys::abi::usercalls::raw::{do_usercall, Usercalls as UsercallNrs};
         pub use crate::sys::abi::usercalls::raw::{Register, RegisterArgument, ReturnValue};
 
-        // fortanix-sgx-abi re-exports
         pub use crate::sys::abi::usercalls::raw::Error;
         pub use crate::sys::abi::usercalls::raw::{
             ByteBuffer, Cancel, FifoDescriptor, Return, Usercall,
diff --git a/library/std/src/os/uefi/mod.rs b/library/std/src/os/uefi/mod.rs
index 8ef05eee1f4..b42d796b28f 100644
--- a/library/std/src/os/uefi/mod.rs
+++ b/library/std/src/os/uefi/mod.rs
@@ -2,6 +2,7 @@
 
 #![unstable(feature = "uefi_std", issue = "100499")]
 #![doc(cfg(target_os = "uefi"))]
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 pub mod env;
 #[path = "../windows/ffi.rs"]
diff --git a/library/std/src/os/xous/mod.rs b/library/std/src/os/xous/mod.rs
index 153694a89a7..4b21695c4ac 100644
--- a/library/std/src/os/xous/mod.rs
+++ b/library/std/src/os/xous/mod.rs
@@ -1,5 +1,6 @@
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(cfg(target_os = "xous"))]
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 pub mod ffi;
 
diff --git a/library/std/src/prelude/common.rs b/library/std/src/prelude/common.rs
index 055ab7eb6d9..b231bd871b3 100644
--- a/library/std/src/prelude/common.rs
+++ b/library/std/src/prelude/common.rs
@@ -2,6 +2,9 @@
 //!
 //! See the [module-level documentation](super) for more.
 
+// No formatting: this file is nothing but re-exports, and their order is worth preserving.
+#![cfg_attr(rustfmt, rustfmt::skip)]
+
 // Re-exported core operators
 #[stable(feature = "rust1", since = "1.0.0")]
 #[doc(no_inline)]
diff --git a/library/std/src/prelude/mod.rs b/library/std/src/prelude/mod.rs
index 2d4639342bf..0c610ba67e6 100644
--- a/library/std/src/prelude/mod.rs
+++ b/library/std/src/prelude/mod.rs
@@ -95,6 +95,9 @@
 //! [book-enums]: ../../book/ch06-01-defining-an-enum.html
 //! [book-iter]: ../../book/ch13-02-iterators.html
 
+// No formatting: this file is nothing but re-exports, and their order is worth preserving.
+#![cfg_attr(rustfmt, rustfmt::skip)]
+
 #![stable(feature = "rust1", since = "1.0.0")]
 
 mod common;
diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs
index d030017cfb4..deb4a8fa7ee 100644
--- a/library/std/src/rt.rs
+++ b/library/std/src/rt.rs
@@ -16,7 +16,6 @@
 #![deny(unsafe_op_in_unsafe_fn)]
 #![allow(unused_macros)]
 
-// Re-export some of our utilities which are expected by other crates.
 pub use crate::panicking::{begin_panic, panic_count};
 pub use core::panicking::{panic_display, panic_fmt};
 
diff --git a/library/std/src/sys/pal/hermit/futex.rs b/library/std/src/sys/pal/hermit/futex.rs
index b2d74d1311b..21c5facd52f 100644
--- a/library/std/src/sys/pal/hermit/futex.rs
+++ b/library/std/src/sys/pal/hermit/futex.rs
@@ -3,6 +3,11 @@ use crate::ptr::null;
 use crate::sync::atomic::AtomicU32;
 use crate::time::Duration;
 
+/// An atomic for use as a futex that is at least 8-bits but may be larger.
+pub type SmallAtomic = AtomicU32;
+/// Must be the underlying type of SmallAtomic
+pub type SmallPrimitive = u32;
+
 pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
     // Calculate the timeout as a relative timespec.
     //
diff --git a/library/std/src/sys/pal/uefi/mod.rs b/library/std/src/sys/pal/uefi/mod.rs
index 408031a4616..4d50d9e8c3d 100644
--- a/library/std/src/sys/pal/uefi/mod.rs
+++ b/library/std/src/sys/pal/uefi/mod.rs
@@ -11,6 +11,7 @@
 //!
 //! [`OsStr`]: crate::ffi::OsStr
 //! [`OsString`]: crate::ffi::OsString
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 pub mod alloc;
 pub mod args;
diff --git a/library/std/src/sys/pal/unix/futex.rs b/library/std/src/sys/pal/unix/futex.rs
index 26161a9af79..b8900da4cdd 100644
--- a/library/std/src/sys/pal/unix/futex.rs
+++ b/library/std/src/sys/pal/unix/futex.rs
@@ -11,6 +11,11 @@
 use crate::sync::atomic::AtomicU32;
 use crate::time::Duration;
 
+/// An atomic for use as a futex that is at least 8-bits but may be larger.
+pub type SmallAtomic = AtomicU32;
+/// Must be the underlying type of SmallAtomic
+pub type SmallPrimitive = u32;
+
 /// Wait for a futex_wake operation to wake us.
 ///
 /// Returns directly if the futex doesn't hold the expected value.
diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs
index 8dfb733043e..d8fe06d1973 100644
--- a/library/std/src/sys/pal/wasi/mod.rs
+++ b/library/std/src/sys/pal/wasi/mod.rs
@@ -39,12 +39,15 @@ pub mod time;
 #[deny(unsafe_op_in_unsafe_fn)]
 #[allow(unused)]
 mod common;
+
 pub use common::*;
 
 mod helpers;
-// These exports are listed individually to work around Rust's glob import
-// conflict rules. If we glob export `helpers` and `common` together, then
-// the compiler complains about conflicts.
+
+// The following exports are listed individually to work around Rust's glob
+// import conflict rules. If we glob export `helpers` and `common` together,
+// then the compiler complains about conflicts.
+
 pub use helpers::abort_internal;
 pub use helpers::decode_error_kind;
 use helpers::err2io;
diff --git a/library/std/src/sys/pal/wasip2/mod.rs b/library/std/src/sys/pal/wasip2/mod.rs
index 7af0917b8ed..0930d2e22fa 100644
--- a/library/std/src/sys/pal/wasip2/mod.rs
+++ b/library/std/src/sys/pal/wasip2/mod.rs
@@ -41,13 +41,16 @@ pub mod time;
 #[deny(unsafe_op_in_unsafe_fn)]
 #[allow(unused)]
 mod common;
+
 pub use common::*;
 
 #[path = "../wasi/helpers.rs"]
 mod helpers;
-// These exports are listed individually to work around Rust's glob import
-// conflict rules. If we glob export `helpers` and `common` together, then
-// the compiler complains about conflicts.
+
+// The following exports are listed individually to work around Rust's glob
+// import conflict rules. If we glob export `helpers` and `common` together,
+// then the compiler complains about conflicts.
+
 pub use helpers::abort_internal;
 pub use helpers::decode_error_kind;
 use helpers::err2io;
diff --git a/library/std/src/sys/pal/wasm/atomics/futex.rs b/library/std/src/sys/pal/wasm/atomics/futex.rs
index a21b71efbbc..3584138ca04 100644
--- a/library/std/src/sys/pal/wasm/atomics/futex.rs
+++ b/library/std/src/sys/pal/wasm/atomics/futex.rs
@@ -6,6 +6,11 @@ use core::arch::wasm64 as wasm;
 use crate::sync::atomic::AtomicU32;
 use crate::time::Duration;
 
+/// An atomic for use as a futex that is at least 8-bits but may be larger.
+pub type SmallAtomic = AtomicU32;
+/// Must be the underlying type of SmallAtomic
+pub type SmallPrimitive = u32;
+
 /// Wait for a futex_wake operation to wake us.
 ///
 /// Returns directly if the futex doesn't hold the expected value.
diff --git a/library/std/src/sys/pal/windows/futex.rs b/library/std/src/sys/pal/windows/futex.rs
index 08b7fe300dc..cb802fdd9c9 100644
--- a/library/std/src/sys/pal/windows/futex.rs
+++ b/library/std/src/sys/pal/windows/futex.rs
@@ -10,6 +10,11 @@ use core::sync::atomic::{
 };
 use core::time::Duration;
 
+/// An atomic for use as a futex that is at least 8-bits but may be larger.
+pub type SmallAtomic = AtomicU8;
+/// Must be the underlying type of SmallAtomic
+pub type SmallPrimitive = u8;
+
 pub unsafe trait Waitable {
     type Atomic;
 }
diff --git a/library/std/src/sys/pal/xous/mod.rs b/library/std/src/sys/pal/xous/mod.rs
index a28a52e305e..961d45c5e83 100644
--- a/library/std/src/sys/pal/xous/mod.rs
+++ b/library/std/src/sys/pal/xous/mod.rs
@@ -1,4 +1,4 @@
-#![deny(unsafe_op_in_unsafe_fn)]
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 pub mod alloc;
 #[path = "../unsupported/args.rs"]
diff --git a/library/std/src/sys/pal/zkvm/alloc.rs b/library/std/src/sys/pal/zkvm/alloc.rs
index fd333f12151..2fdca223524 100644
--- a/library/std/src/sys/pal/zkvm/alloc.rs
+++ b/library/std/src/sys/pal/zkvm/alloc.rs
@@ -5,7 +5,7 @@ use crate::alloc::{GlobalAlloc, Layout, System};
 unsafe impl GlobalAlloc for System {
     #[inline]
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
-        abi::sys_alloc_aligned(layout.size(), layout.align())
+        unsafe { abi::sys_alloc_aligned(layout.size(), layout.align()) }
     }
 
     #[inline]
diff --git a/library/std/src/sys/pal/zkvm/mod.rs b/library/std/src/sys/pal/zkvm/mod.rs
index bacde9d880c..651f25d6623 100644
--- a/library/std/src/sys/pal/zkvm/mod.rs
+++ b/library/std/src/sys/pal/zkvm/mod.rs
@@ -6,6 +6,7 @@
 //! This is all super highly experimental and not actually intended for
 //! wide/production use yet, it's still all in the experimental category. This
 //! will likely change over time.
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 const WORD_SIZE: usize = core::mem::size_of::<u32>();
 
diff --git a/library/std/src/sys/personality/dwarf/mod.rs b/library/std/src/sys/personality/dwarf/mod.rs
index 652fbe95a14..89f7f133e21 100644
--- a/library/std/src/sys/personality/dwarf/mod.rs
+++ b/library/std/src/sys/personality/dwarf/mod.rs
@@ -17,32 +17,30 @@ pub struct DwarfReader {
     pub ptr: *const u8,
 }
 
-#[repr(C, packed)]
-struct Unaligned<T>(T);
-
+#[forbid(unsafe_op_in_unsafe_fn)]
 impl DwarfReader {
     pub fn new(ptr: *const u8) -> DwarfReader {
         DwarfReader { ptr }
     }
 
-    // DWARF streams are packed, so e.g., a u32 would not necessarily be aligned
-    // on a 4-byte boundary. This may cause problems on platforms with strict
-    // alignment requirements. By wrapping data in a "packed" struct, we are
-    // telling the backend to generate "misalignment-safe" code.
+    /// Read a type T and then bump the pointer by that amount.
+    ///
+    /// DWARF streams are "packed", so all types must be read at align 1.
     pub unsafe fn read<T: Copy>(&mut self) -> T {
-        let Unaligned(result) = *(self.ptr as *const Unaligned<T>);
-        self.ptr = self.ptr.add(mem::size_of::<T>());
-        result
+        unsafe {
+            let result = self.ptr.cast::<T>().read_unaligned();
+            self.ptr = self.ptr.byte_add(mem::size_of::<T>());
+            result
+        }
     }
 
-    // ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
-    // Length Data".
+    /// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable Length Data".
     pub unsafe fn read_uleb128(&mut self) -> u64 {
         let mut shift: usize = 0;
         let mut result: u64 = 0;
         let mut byte: u8;
         loop {
-            byte = self.read::<u8>();
+            byte = unsafe { self.read::<u8>() };
             result |= ((byte & 0x7F) as u64) << shift;
             shift += 7;
             if byte & 0x80 == 0 {
@@ -57,7 +55,7 @@ impl DwarfReader {
         let mut result: u64 = 0;
         let mut byte: u8;
         loop {
-            byte = self.read::<u8>();
+            byte = unsafe { self.read::<u8>() };
             result |= ((byte & 0x7F) as u64) << shift;
             shift += 7;
             if byte & 0x80 == 0 {
diff --git a/library/std/src/sys/sync/mutex/futex.rs b/library/std/src/sys/sync/mutex/futex.rs
index 7427cae94d6..81afa94b147 100644
--- a/library/std/src/sys/sync/mutex/futex.rs
+++ b/library/std/src/sys/sync/mutex/futex.rs
@@ -1,19 +1,8 @@
-use crate::sync::atomic::{
-    self,
-    Ordering::{Acquire, Relaxed, Release},
-};
-use crate::sys::futex::{futex_wait, futex_wake};
-
-cfg_if::cfg_if! {
-if #[cfg(windows)] {
-    // On Windows we can have a smol futex
-    type Atomic = atomic::AtomicU8;
-    type State = u8;
-} else {
-    type Atomic = atomic::AtomicU32;
-    type State = u32;
-}
-}
+use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
+use crate::sys::futex::{self, futex_wait, futex_wake};
+
+type Atomic = futex::SmallAtomic;
+type State = futex::SmallPrimitive;
 
 pub struct Mutex {
     futex: Atomic,
diff --git a/library/std/src/sys/sync/thread_parking/futex.rs b/library/std/src/sys/sync/thread_parking/futex.rs
index 588e7b27826..034eececb2a 100644
--- a/library/std/src/sys/sync/thread_parking/futex.rs
+++ b/library/std/src/sys/sync/thread_parking/futex.rs
@@ -1,15 +1,18 @@
+#![forbid(unsafe_op_in_unsafe_fn)]
 use crate::pin::Pin;
-use crate::sync::atomic::AtomicU32;
 use crate::sync::atomic::Ordering::{Acquire, Release};
-use crate::sys::futex::{futex_wait, futex_wake};
+use crate::sys::futex::{self, futex_wait, futex_wake};
 use crate::time::Duration;
 
-const PARKED: u32 = u32::MAX;
-const EMPTY: u32 = 0;
-const NOTIFIED: u32 = 1;
+type Atomic = futex::SmallAtomic;
+type State = futex::SmallPrimitive;
+
+const PARKED: State = State::MAX;
+const EMPTY: State = 0;
+const NOTIFIED: State = 1;
 
 pub struct Parker {
-    state: AtomicU32,
+    state: Atomic,
 }
 
 // Notes about memory ordering:
@@ -36,7 +39,7 @@ impl Parker {
     /// Construct the futex parker. The UNIX parker implementation
     /// requires this to happen in-place.
     pub unsafe fn new_in_place(parker: *mut Parker) {
-        parker.write(Self { state: AtomicU32::new(EMPTY) });
+        unsafe { parker.write(Self { state: Atomic::new(EMPTY) }) };
     }
 
     // Assumes this is only called by the thread that owns the Parker,
diff --git a/library/std/src/sys/sync/thread_parking/mod.rs b/library/std/src/sys/sync/thread_parking/mod.rs
index ed1a6437faa..0ebc5e093ee 100644
--- a/library/std/src/sys/sync/thread_parking/mod.rs
+++ b/library/std/src/sys/sync/thread_parking/mod.rs
@@ -1,5 +1,6 @@
 cfg_if::cfg_if! {
     if #[cfg(any(
+        all(target_os = "windows", not(target_vendor = "win7")),
         target_os = "linux",
         target_os = "android",
         all(target_arch = "wasm32", target_feature = "atomics"),
@@ -18,9 +19,9 @@ cfg_if::cfg_if! {
     ))] {
         mod id;
         pub use id::Parker;
-    } else if #[cfg(target_os = "windows")] {
-        mod windows;
-        pub use windows::Parker;
+    } else if #[cfg(target_vendor = "win7")] {
+        mod windows7;
+        pub use windows7::Parker;
     } else if #[cfg(all(target_vendor = "apple", not(miri)))] {
         mod darwin;
         pub use darwin::Parker;
diff --git a/library/std/src/sys/sync/thread_parking/windows.rs b/library/std/src/sys/sync/thread_parking/windows7.rs
index 3a8d40dc5cf..3a8d40dc5cf 100644
--- a/library/std/src/sys/sync/thread_parking/windows.rs
+++ b/library/std/src/sys/sync/thread_parking/windows7.rs