From fe15c63bed174058da46ec04d94e10892cf7161d Mon Sep 17 00:00:00 2001 From: Max Dexheimer Date: Mon, 14 Sep 2026 13:50:12 +0200 Subject: [PATCH] stop unleaking `&mut`s in std and the compiler --- compiler/rustc_arena/src/lib.rs | 5 +--- library/alloc/src/rc.rs | 33 +++++++++------------ library/alloc/src/sync.rs | 23 +++++++------- library/core/src/marker.rs | 2 +- src/tools/miri/tests/fail/memleak_rc.stderr | 9 ++++-- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_arena/src/lib.rs b/compiler/rustc_arena/src/lib.rs index 43e2031db65..0fc1f96dfe4 100644 --- a/compiler/rustc_arena/src/lib.rs +++ b/compiler/rustc_arena/src/lib.rs @@ -52,10 +52,7 @@ unsafe impl<#[may_dangle] T> Drop for ArenaChunk { impl ArenaChunk { #[inline] unsafe fn new(capacity: usize) -> ArenaChunk { - ArenaChunk { - storage: NonNull::from(Box::leak(Box::new_uninit_slice(capacity))), - entries: 0, - } + ArenaChunk { storage: Box::into_non_null(Box::new_uninit_slice(capacity)), entries: 0 } } /// Destroys this arena chunk. diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 0a374894745..5c6f5245e7c 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -431,10 +431,11 @@ impl Rc { // the allocation while the strong destructor is running, even // if the weak pointer is stored inside the strong one. unsafe { - Self::from_inner( - Box::leak(Box::new(RcInner { strong: Cell::new(1), weak: Cell::new(1), value })) - .into(), - ) + Self::from_inner(Box::into_non_null(Box::new(RcInner { + strong: Cell::new(1), + weak: Cell::new(1), + value, + }))) } } @@ -578,14 +579,11 @@ impl Rc { // the allocation while the strong destructor is running, even // if the weak pointer is stored inside the strong one. unsafe { - Ok(Self::from_inner( - Box::leak(Box::try_new(RcInner { - strong: Cell::new(1), - weak: Cell::new(1), - value, - })?) - .into(), - )) + Ok(Self::from_inner(Box::into_non_null(Box::try_new(RcInner { + strong: Cell::new(1), + weak: Cell::new(1), + value, + })?))) } } @@ -2656,13 +2654,10 @@ impl Default for Rc { fn default() -> Self { // ignore-tidy-undocumented-unsafe unsafe { - Self::from_inner( - Box::leak(Box::write( - Box::new_uninit(), - RcInner { strong: Cell::new(1), weak: Cell::new(1), value: T::default() }, - )) - .into(), - ) + Self::from_inner(Box::into_non_null(Box::write( + Box::new_uninit(), + RcInner { strong: Cell::new(1), weak: Cell::new(1), value: T::default() }, + ))) } } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 3f5e8614987..95490625668 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -450,7 +450,7 @@ impl Arc { data, }); // SAFETY: Pointer is valid. - unsafe { Self::from_inner(Box::leak(x).into()) } + unsafe { Self::from_inner(Box::into_non_null(x)) } } /// Constructs a new `Arc` while giving you a `Weak` to the allocation, @@ -618,7 +618,7 @@ impl Arc { data, })?; // SAFETY: Pointer is valid. - unsafe { Ok(Self::from_inner(Box::leak(x).into())) } + unsafe { Ok(Self::from_inner(Box::into_non_null(x))) } } /// Constructs a new `Arc` with uninitialized contents, returning an error @@ -3916,17 +3916,14 @@ impl Default for Arc { fn default() -> Arc { // ignore-tidy-undocumented-unsafe unsafe { - Self::from_inner( - Box::leak(Box::write( - Box::new_uninit(), - ArcInner { - strong: atomic::AtomicUsize::new(1), - weak: atomic::AtomicUsize::new(1), - data: T::default(), - }, - )) - .into(), - ) + Self::from_inner(Box::into_non_null(Box::write( + Box::new_uninit(), + ArcInner { + strong: atomic::AtomicUsize::new(1), + weak: atomic::AtomicUsize::new(1), + data: T::default(), + }, + ))) } } } diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 395652c51dc..217f29d6a86 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1302,7 +1302,7 @@ marker_impls! { /// value, /// }); /// Self { -/// inner: NonNull::from(Box::leak(inner)), +/// inner: Box::into_non_null(inner), /// } /// } /// } diff --git a/src/tools/miri/tests/fail/memleak_rc.stderr b/src/tools/miri/tests/fail/memleak_rc.stderr index 2c22367f524..a77cf55b0c3 100644 --- a/src/tools/miri/tests/fail/memleak_rc.stderr +++ b/src/tools/miri/tests/fail/memleak_rc.stderr @@ -1,8 +1,13 @@ error: memory leaked: ALLOC (Rust heap, SIZE, ALIGN), allocated here: --> RUSTLIB/alloc/src/rc.rs:LL:CC | -LL | Box::leak(Box::new(RcInner { strong: Cell::new(1), weak: Cell::new(1), value })) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | Self::from_inner(Box::into_non_null(Box::new(RcInner { + | _________________________________________________^ +LL | | strong: Cell::new(1), +LL | | weak: Cell::new(1), +LL | | value, +LL | | }))) + | |______________^ | = note: stack backtrace: 0: std::rc::Rc::>>::new