mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Rollup merge of #162766 - maxdexh:unleak-considered-harmful, r=hanna-kruppe,saethlin
stop unleaking `&mut`s in std and the compiler As discussed on [zulip](https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/What.20is.20the.20current.20state.20of.20unleaking.20a.20.60Box.3CT.2C.20Global.3E.60.3F/with/623860839), this PR removes all the dubious uses (I could find) of `Box::leak` from rustc and std r? libs
This commit is contained in:
@@ -52,10 +52,7 @@ unsafe impl<#[may_dangle] T> Drop for ArenaChunk<T> {
|
||||
impl<T> ArenaChunk<T> {
|
||||
#[inline]
|
||||
unsafe fn new(capacity: usize) -> ArenaChunk<T> {
|
||||
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.
|
||||
|
||||
@@ -431,10 +431,11 @@ impl<T> Rc<T> {
|
||||
// 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<T> Rc<T> {
|
||||
// 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<T: Default> Default for Rc<T> {
|
||||
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() },
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,7 +450,7 @@ impl<T> Arc<T> {
|
||||
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<T>` while giving you a `Weak<T>` to the allocation,
|
||||
@@ -618,7 +618,7 @@ impl<T> Arc<T> {
|
||||
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<T: Default> Default for Arc<T> {
|
||||
fn default() -> Arc<T> {
|
||||
// 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(),
|
||||
},
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1302,7 +1302,7 @@ marker_impls! {
|
||||
/// value,
|
||||
/// });
|
||||
/// Self {
|
||||
/// inner: NonNull::from(Box::leak(inner)),
|
||||
/// inner: Box::into_non_null(inner),
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
|
||||
@@ -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::<std::cell::RefCell<std::option::Option<Dummy>>>::new
|
||||
|
||||
Reference in New Issue
Block a user