about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-03-29 06:16:56 +0000
committerbors <bors@rust-lang.org>2024-03-29 06:16:56 +0000
commiteae940fcef7f95d31b566f92f511f91f24f2f703 (patch)
treea5635c4d1821e9114fe7cc27d00353f98cac5402 /library/core/src
parent58a771ebfa2e59f48cb50229049487c6550b2f7b (diff)
parented29546a27db9fa024c6f99a7dd001e86d75e14f (diff)
Auto merge of #3427 - rust-lang:rustup-2024-03-29, r=saethlin
Automatic Rustup
Diffstat (limited to 'library/core/src')
-rw-r--r--library/core/src/intrinsics.rs14
-rw-r--r--library/core/src/lib.rs1
-rw-r--r--library/core/src/macros/mod.rs20
-rw-r--r--library/core/src/num/nonzero.rs26
-rw-r--r--library/core/src/ops/deref.rs19
-rw-r--r--library/core/src/ops/mod.rs3
-rw-r--r--library/core/src/panicking.rs65
-rw-r--r--library/core/src/pin.rs2
-rw-r--r--library/core/src/prelude/common.rs (renamed from library/core/src/prelude/v1.rs)9
-rw-r--r--library/core/src/prelude/mod.rs34
-rw-r--r--library/core/src/sync/atomic.rs6
-rw-r--r--library/core/src/unicode/printable.rs26
12 files changed, 172 insertions, 53 deletions
diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs
index bb1debfc77d..ae6e39d19e1 100644
--- a/library/core/src/intrinsics.rs
+++ b/library/core/src/intrinsics.rs
@@ -2686,12 +2686,14 @@ pub const unsafe fn typed_swap<T>(x: *mut T, y: *mut T) {
     unsafe { ptr::swap_nonoverlapping(x, y, 1) };
 }
 
-/// Returns whether we should perform some UB-checking at runtime. This evaluate to the value of
-/// `cfg!(debug_assertions)` during monomorphization.
-///
-/// This intrinsic is evaluated after monomorphization, which is relevant when mixing crates
-/// compiled with and without debug_assertions. The common case here is a user program built with
-/// debug_assertions linked against the distributed sysroot which is built without debug_assertions.
+/// Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
+/// `cfg!(debug_assertions)`, but behaves different from `cfg!` when mixing crates built with different
+/// flags: if the crate has debug assertions enabled or carries the `#[rustc_preserve_ub_checks]`
+/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
+/// a crate that does not delay evaluation further); otherwise it can happen any time.
+///
+/// The common case here is a user program built with debug_assertions linked against the distributed
+/// sysroot which is built without debug_assertions but with `#[rustc_preserve_ub_checks]`.
 /// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
 /// `#[inline]`), gating assertions on `ub_checks()` rather than `cfg!(debug_assertions)` means that
 /// assertions are enabled whenever the *user crate* has debug assertions enabled. However if the
diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs
index 3bc1a87f848..cf13496b367 100644
--- a/library/core/src/lib.rs
+++ b/library/core/src/lib.rs
@@ -94,6 +94,7 @@
 ))]
 #![no_core]
 #![rustc_coherence_is_core]
+#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]
 //
 // Lints:
 #![deny(rust_2021_incompatible_or_patterns)]
diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs
index a78842c8f8d..574a357b44a 100644
--- a/library/core/src/macros/mod.rs
+++ b/library/core/src/macros/mod.rs
@@ -1726,20 +1726,28 @@ pub(crate) mod builtin {
         builtin # deref($pat)
     }
 
-    /// Unstable implementation detail of the `rustc` compiler, do not use.
+    /// Derive macro for `rustc-serialize`. Should not be used in new code.
     #[rustc_builtin_macro]
-    #[stable(feature = "rust1", since = "1.0.0")]
-    #[allow_internal_unstable(core_intrinsics, libstd_sys_internals, rt)]
+    #[unstable(
+        feature = "rustc_encodable_decodable",
+        issue = "none",
+        soft,
+        reason = "derive macro for `rustc-serialize`; should not be used in new code"
+    )]
     #[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
     #[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
     pub macro RustcDecodable($item:item) {
         /* compiler built-in */
     }
 
-    /// Unstable implementation detail of the `rustc` compiler, do not use.
+    /// Derive macro for `rustc-serialize`. Should not be used in new code.
     #[rustc_builtin_macro]
-    #[stable(feature = "rust1", since = "1.0.0")]
-    #[allow_internal_unstable(core_intrinsics, rt)]
+    #[unstable(
+        feature = "rustc_encodable_decodable",
+        issue = "none",
+        soft,
+        reason = "derive macro for `rustc-serialize`; should not be used in new code"
+    )]
     #[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
     #[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
     pub macro RustcEncodable($item:item) {
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index c65ffbb98f2..1171407c07a 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -20,7 +20,15 @@ use super::{IntErrorKind, ParseIntError};
 ///
 /// # Safety
 ///
-/// Types implementing this trait must be primitves that are valid when zeroed.
+/// Types implementing this trait must be primitives that are valid when zeroed.
+///
+/// The associated `Self::NonZeroInner` type must have the same size+align as `Self`,
+/// but with a niche and bit validity making it so the following `transmutes` are sound:
+///
+/// - `Self::NonZeroInner` to `Option<Self::NonZeroInner>`
+/// - `Option<Self::NonZeroInner>` to `Self`
+///
+/// (And, consequently, `Self::NonZeroInner` to `Self`.)
 #[unstable(
     feature = "nonzero_internals",
     reason = "implementation detail which may disappear or be replaced at any time",
@@ -434,17 +442,11 @@ where
         // of some not-inlined function, LLVM don't have range metadata
         // to understand that the value cannot be zero.
         //
-        // SAFETY: `Self` is guaranteed to have the same layout as `Option<Self>`.
-        match unsafe { intrinsics::transmute_unchecked(self) } {
-            None => {
-                // SAFETY: `NonZero` is guaranteed to only contain non-zero values, so this is unreachable.
-                unsafe { intrinsics::unreachable() }
-            }
-            Some(Self(inner)) => {
-                // SAFETY: `T::NonZeroInner` is guaranteed to have the same layout as `T`.
-                unsafe { intrinsics::transmute_unchecked(inner) }
-            }
-        }
+        // For now, using the transmute `assume`s the range at runtime.
+        //
+        // SAFETY: `ZeroablePrimitive` guarantees that the size and bit validity
+        // of `.0` is such that this transmute is sound.
+        unsafe { intrinsics::transmute_unchecked(self) }
     }
 }
 
diff --git a/library/core/src/ops/deref.rs b/library/core/src/ops/deref.rs
index 2c7845d4304..3795a81c2c1 100644
--- a/library/core/src/ops/deref.rs
+++ b/library/core/src/ops/deref.rs
@@ -275,6 +275,25 @@ impl<T: ?Sized> DerefMut for &mut T {
     }
 }
 
+/// Perma-unstable marker trait. Indicates that the type has a well-behaved [`Deref`]
+/// (and, if applicable, [`DerefMut`]) implementation. This is relied on for soundness
+/// of deref patterns.
+///
+/// FIXME(deref_patterns): The precise semantics are undecided; the rough idea is that
+/// successive calls to `deref`/`deref_mut` without intermediate mutation should be
+/// idempotent, in the sense that they return the same value as far as pattern-matching
+/// is concerned. Calls to `deref`/`deref_mut`` must leave the pointer itself likewise
+/// unchanged.
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+#[cfg_attr(not(bootstrap), lang = "deref_pure")]
+pub unsafe trait DerefPure {}
+
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+unsafe impl<T: ?Sized> DerefPure for &T {}
+
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+unsafe impl<T: ?Sized> DerefPure for &mut T {}
+
 /// Indicates that a struct can be used as a method receiver, without the
 /// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
 /// `Rc<T>`, `&T`, and `Pin<P>`.
diff --git a/library/core/src/ops/mod.rs b/library/core/src/ops/mod.rs
index 4289a86f89b..ac808bec50e 100644
--- a/library/core/src/ops/mod.rs
+++ b/library/core/src/ops/mod.rs
@@ -165,6 +165,9 @@ pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssig
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use self::deref::{Deref, DerefMut};
 
+#[unstable(feature = "deref_pure_trait", issue = "87121")]
+pub use self::deref::DerefPure;
+
 #[unstable(feature = "receiver_trait", issue = "none")]
 pub use self::deref::Receiver;
 
diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs
index a8940d9cd1e..cbb0a7d61db 100644
--- a/library/core/src/panicking.rs
+++ b/library/core/src/panicking.rs
@@ -130,7 +130,7 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo
 #[cfg_attr(feature = "panic_immediate_abort", inline)]
 #[track_caller]
 #[rustc_const_unstable(feature = "panic_internals", issue = "none")]
-#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
+#[lang = "panic"] // used by lints and miri for panics
 pub const fn panic(expr: &'static str) -> ! {
     // Use Arguments::new_const instead of format_args!("{expr}") to potentially
     // reduce size overhead. The format_args! macro uses str's Display trait to
@@ -141,6 +141,69 @@ pub const fn panic(expr: &'static str) -> ! {
     panic_fmt(fmt::Arguments::new_const(&[expr]));
 }
 
+// We generate functions for usage by compiler-generated assertions.
+//
+// Placing these functions in libcore means that all Rust programs can generate a jump into this
+// code rather than expanding to panic("...") above, which adds extra bloat to call sites (for the
+// constant string argument's pointer and length).
+//
+// This is especially important when this code is called often (e.g., with -Coverflow-checks) for
+// reducing binary size impact.
+macro_rules! panic_const {
+    ($($lang:ident = $message:expr,)+) => {
+        #[cfg(not(bootstrap))]
+        pub mod panic_const {
+            use super::*;
+
+            $(
+                /// This is a panic called with a message that's a result of a MIR-produced Assert.
+                //
+                // never inline unless panic_immediate_abort to avoid code
+                // bloat at the call sites as much as possible
+                #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
+                #[cfg_attr(feature = "panic_immediate_abort", inline)]
+                #[track_caller]
+                #[rustc_const_unstable(feature = "panic_internals", issue = "none")]
+                #[lang = stringify!($lang)]
+                pub const fn $lang() -> ! {
+                    // Use Arguments::new_const instead of format_args!("{expr}") to potentially
+                    // reduce size overhead. The format_args! macro uses str's Display trait to
+                    // write expr, which calls Formatter::pad, which must accommodate string
+                    // truncation and padding (even though none is used here). Using
+                    // Arguments::new_const may allow the compiler to omit Formatter::pad from the
+                    // output binary, saving up to a few kilobytes.
+                    panic_fmt(fmt::Arguments::new_const(&[$message]));
+                }
+            )+
+        }
+    }
+}
+
+// Unfortunately this set of strings is replicated here and in a few places in the compiler in
+// slightly different forms. It's not clear if there's a good way to deduplicate without adding
+// special cases to the compiler (e.g., a const generic function wouldn't have a single definition
+// shared across crates, which is exactly what we want here).
+panic_const! {
+    panic_const_add_overflow = "attempt to add with overflow",
+    panic_const_sub_overflow = "attempt to subtract with overflow",
+    panic_const_mul_overflow = "attempt to multiply with overflow",
+    panic_const_div_overflow = "attempt to divide with overflow",
+    panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
+    panic_const_neg_overflow = "attempt to negate with overflow",
+    panic_const_shr_overflow = "attempt to shift right with overflow",
+    panic_const_shl_overflow = "attempt to shift left with overflow",
+    panic_const_div_by_zero = "attempt to divide by zero",
+    panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
+    panic_const_coroutine_resumed = "coroutine resumed after completion",
+    panic_const_async_fn_resumed = "`async fn` resumed after completion",
+    panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
+    panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
+    panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
+    panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
+    panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
+    panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
+}
+
 /// Like `panic`, but without unwinding and track_caller to reduce the impact on codesize on the caller.
 /// If you want `#[track_caller]` for nicer errors, call `panic_nounwind_fmt` directly.
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs
index d14cac9afb5..e843a5d5790 100644
--- a/library/core/src/pin.rs
+++ b/library/core/src/pin.rs
@@ -806,7 +806,7 @@
 //!
 //!     As a consequence, the struct *must not* be [`#[repr(packed)]`][packed].
 //!
-//! 3.  *Structural Notice of Destruction.* You must uphold the the
+//! 3.  *Structural Notice of Destruction.* You must uphold the
 //!     [`Drop` guarantee][drop-guarantee]: once your struct is pinned, the struct's storage cannot
 //!     be re-used without calling the structurally-pinned fields' destructors, as well.
 //!
diff --git a/library/core/src/prelude/v1.rs b/library/core/src/prelude/common.rs
index 29f73bb4942..b98f3a4659b 100644
--- a/library/core/src/prelude/v1.rs
+++ b/library/core/src/prelude/common.rs
@@ -1,9 +1,7 @@
-//! The first version of the core prelude.
+//! Items common to the prelude of all editions.
 //!
 //! See the [module-level documentation](super) for more.
 
-#![stable(feature = "core_prelude", since = "1.4.0")]
-
 // Re-exported core operators
 #[stable(feature = "core_prelude", since = "1.4.0")]
 #[doc(no_inline)]
@@ -68,11 +66,6 @@ pub use crate::{
 #[doc(no_inline)]
 pub use crate::concat_bytes;
 
-// Do not `doc(inline)` these `doc(hidden)` items.
-#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
-#[allow(deprecated)]
-pub use crate::macros::builtin::{RustcDecodable, RustcEncodable};
-
 // Do not `doc(no_inline)` so that they become doc items on their own
 // (no public module for them to be re-exported from).
 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
diff --git a/library/core/src/prelude/mod.rs b/library/core/src/prelude/mod.rs
index b4791c2c022..ca33ef160e8 100644
--- a/library/core/src/prelude/mod.rs
+++ b/library/core/src/prelude/mod.rs
@@ -6,7 +6,26 @@
 
 #![stable(feature = "core_prelude", since = "1.4.0")]
 
-pub mod v1;
+mod common;
+
+/// The first version of the prelude of The Rust Standard Library.
+///
+/// See the [module-level documentation](self) for more.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub mod v1 {
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub use super::common::*;
+
+    // Do not `doc(inline)` these `doc(hidden)` items.
+    #[unstable(
+        feature = "rustc_encodable_decodable",
+        issue = "none",
+        soft,
+        reason = "derive macro for `rustc-serialize`; should not be used in new code"
+    )]
+    #[allow(deprecated)]
+    pub use crate::macros::builtin::{RustcDecodable, RustcEncodable};
+}
 
 /// The 2015 version of the core prelude.
 ///
@@ -46,14 +65,21 @@ pub mod rust_2021 {
     pub use crate::convert::{TryFrom, TryInto};
 }
 
-/// The 2024 edition of the core prelude.
+/// The 2024 version of the core prelude.
 ///
 /// See the [module-level documentation](self) for more.
 #[unstable(feature = "prelude_2024", issue = "121042")]
 pub mod rust_2024 {
-    #[unstable(feature = "prelude_2024", issue = "121042")]
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub use super::common::*;
+
+    #[stable(feature = "prelude_2021", since = "1.55.0")]
+    #[doc(no_inline)]
+    pub use crate::iter::FromIterator;
+
+    #[stable(feature = "prelude_2021", since = "1.55.0")]
     #[doc(no_inline)]
-    pub use super::rust_2021::*;
+    pub use crate::convert::{TryFrom, TryInto};
 
     #[unstable(feature = "prelude_2024", issue = "121042")]
     #[doc(no_inline)]
diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs
index 00e75ec9a25..77002ef87aa 100644
--- a/library/core/src/sync/atomic.rs
+++ b/library/core/src/sync/atomic.rs
@@ -243,7 +243,7 @@ const EMULATE_ATOMIC_BOOL: bool =
 
 /// A boolean type which can be safely shared between threads.
 ///
-/// This type has the same in-memory representation as a [`bool`].
+/// This type has the same size, alignment, and bit validity as a [`bool`].
 ///
 /// **Note**: This type is only available on platforms that support atomic
 /// loads and stores of `u8`.
@@ -272,7 +272,7 @@ unsafe impl Sync for AtomicBool {}
 
 /// A raw pointer type which can be safely shared between threads.
 ///
-/// This type has the same in-memory representation as a `*mut T`.
+/// This type has the same size and bit validity as a `*mut T`.
 ///
 /// **Note**: This type is only available on platforms that support atomic
 /// loads and stores of pointers. Its size depends on the target pointer's size.
@@ -2121,7 +2121,7 @@ macro_rules! atomic_int {
      $int_type:ident $atomic_type:ident) => {
         /// An integer type which can be safely shared between threads.
         ///
-        /// This type has the same in-memory representation as the underlying
+        /// This type has the same size and bit validity as the underlying
         /// integer type, [`
         #[doc = $s_int_type]
         /// `].
diff --git a/library/core/src/unicode/printable.rs b/library/core/src/unicode/printable.rs
index ffb18a5ba8b..33c3ef8083d 100644
--- a/library/core/src/unicode/printable.rs
+++ b/library/core/src/unicode/printable.rs
@@ -63,7 +63,10 @@ pub(crate) fn is_printable(x: char) -> bool {
         if 0x2cea2 <= x && x < 0x2ceb0 {
             return false;
         }
-        if 0x2ebe1 <= x && x < 0x2f800 {
+        if 0x2ebe1 <= x && x < 0x2ebf0 {
+            return false;
+        }
+        if 0x2ee5e <= x && x < 0x2f800 {
             return false;
         }
         if 0x2fa1e <= x && x < 0x30000 {
@@ -112,7 +115,7 @@ const SINGLETONS0U: &[(u8, u8)] = &[
     (0x2b, 3),
     (0x2d, 11),
     (0x2e, 1),
-    (0x30, 3),
+    (0x30, 4),
     (0x31, 2),
     (0x32, 1),
     (0xa7, 2),
@@ -157,12 +160,12 @@ const SINGLETONS0L: &[u8] = &[
     0x7e, 0x7f, 0xb5, 0xc5, 0xd4, 0xd5, 0xdc, 0xf0,
     0xf1, 0xf5, 0x72, 0x73, 0x8f, 0x74, 0x75, 0x96,
     0x26, 0x2e, 0x2f, 0xa7, 0xaf, 0xb7, 0xbf, 0xc7,
-    0xcf, 0xd7, 0xdf, 0x9a, 0x40, 0x97, 0x98, 0x30,
-    0x8f, 0x1f, 0xd2, 0xd4, 0xce, 0xff, 0x4e, 0x4f,
-    0x5a, 0x5b, 0x07, 0x08, 0x0f, 0x10, 0x27, 0x2f,
-    0xee, 0xef, 0x6e, 0x6f, 0x37, 0x3d, 0x3f, 0x42,
-    0x45, 0x90, 0x91, 0x53, 0x67, 0x75, 0xc8, 0xc9,
-    0xd0, 0xd1, 0xd8, 0xd9, 0xe7, 0xfe, 0xff,
+    0xcf, 0xd7, 0xdf, 0x9a, 0x00, 0x40, 0x97, 0x98,
+    0x30, 0x8f, 0x1f, 0xd2, 0xd4, 0xce, 0xff, 0x4e,
+    0x4f, 0x5a, 0x5b, 0x07, 0x08, 0x0f, 0x10, 0x27,
+    0x2f, 0xee, 0xef, 0x6e, 0x6f, 0x37, 0x3d, 0x3f,
+    0x42, 0x45, 0x90, 0x91, 0x53, 0x67, 0x75, 0xc8,
+    0xc9, 0xd0, 0xd1, 0xd8, 0xd9, 0xe7, 0xfe, 0xff,
 ];
 #[rustfmt::skip]
 const SINGLETONS1U: &[(u8, u8)] = &[
@@ -339,10 +342,9 @@ const NORMAL0: &[u8] = &[
     0x80, 0xbe, 0x22,
     0x74, 0x0c,
     0x80, 0xd6, 0x1a,
-    0x0c, 0x05,
-    0x80, 0xff, 0x05,
-    0x80, 0xdf, 0x0c,
-    0xf2, 0x9d, 0x03,
+    0x81, 0x10, 0x05,
+    0x80, 0xdf, 0x0b,
+    0xf2, 0x9e, 0x03,
     0x37, 0x09,
     0x81, 0x5c, 0x14,
     0x80, 0xb8, 0x08,