Skip to content

Commit e8ea4b4

Browse files
committed
std: Mark mem::forget as a safe function
This commit is an implementation of [RFC 1066][rfc] where the conclusion was that leaking a value is a safe operation in Rust code, so updating the signature of this function follows suit. [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1066-safe-mem-forget.md Closes #25186
1 parent 05d5fca commit e8ea4b4

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

src/libcollections/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<T> Vec<T> {
647647
// zero-size types consume no memory, so we can't rely on the
648648
// address space running out
649649
self.len = self.len.checked_add(1).expect("length overflow");
650-
unsafe { mem::forget(value); }
650+
mem::forget(value);
651651
return
652652
}
653653

@@ -994,7 +994,7 @@ impl<T> Vec<T> {
994994
num_u: 0,
995995
marker: PhantomData,
996996
};
997-
unsafe { mem::forget(vec); }
997+
mem::forget(vec);
998998

999999
while pv.num_t != 0 {
10001000
unsafe {

src/libcore/intrinsics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ extern "rust-intrinsic" {
235235
///
236236
/// `forget` is unsafe because the caller is responsible for
237237
/// ensuring the argument is deallocated already.
238-
#[stable(feature = "rust1", since = "1.0.0")]
239238
pub fn forget<T>(_: T) -> ();
240239

241240
/// Unsafely transforms a value of one type into a value of another type.

src/libcore/mem.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,53 @@ use ptr;
2222
#[stable(feature = "rust1", since = "1.0.0")]
2323
pub use intrinsics::transmute;
2424

25-
/// Moves a thing into the void.
25+
/// Leaks a value into the void, consuming ownership and never running its
26+
/// destructor.
2627
///
27-
/// The forget function will take ownership of the provided value but neglect
28-
/// to run any required cleanup or memory management operations on it.
28+
/// This function will take ownership of its argument, but is distinct from the
29+
/// `mem::drop` function in that it **does not run the destructor**, leaking the
30+
/// value and any resources that it owns.
2931
///
30-
/// This function is the unsafe version of the `drop` function because it does
31-
/// not run any destructors.
32+
/// # Safety
33+
///
34+
/// This function is not marked as `unsafe` as Rust does not guarantee that the
35+
/// `Drop` implementation for a value will always run. Note, however, that
36+
/// leaking resources such as memory or I/O objects is likely not desired, so
37+
/// this function is only recommended for specialized use cases.
38+
///
39+
/// The safety of this function implies that when writing `unsafe` code
40+
/// yourself, you cannot write a primitive that relies on a destructor running
41+
/// to preserve memory safety. Unsafe code must be resilient to destructors not
42+
/// running in all circumstances.
43+
///
44+
/// # Other forms of Leakage
45+
///
46+
/// It's important to point out that this function is not the only method by
47+
/// which a value can be leaked in safe Rust code. Other known sources of
48+
/// leakage are:
49+
///
50+
/// * `Rc` and `Arc` cycles
51+
/// * `mpsc::{Sender, Receiver}` cycles (they use `Arc` internally)
52+
/// * Panicking destructors are likely to leak local resources
53+
///
54+
/// # Example
55+
///
56+
/// ```rust,no_run
57+
/// use std::mem;
58+
/// use std::fs::File;
59+
///
60+
/// // Leak some heap memory by never deallocating it
61+
/// let heap_memory = Box::new(3);
62+
/// mem::forget(heap_memory);
63+
///
64+
/// // Leak an I/O object, never closing the file
65+
/// let file = File::open("foo.txt").unwrap();
66+
/// mem::forget(file);
67+
/// ```
3268
#[stable(feature = "rust1", since = "1.0.0")]
33-
pub use intrinsics::forget;
69+
pub fn forget<T>(t: T) {
70+
unsafe { intrinsics::forget(t) }
71+
}
3472

3573
/// Returns the size of a type in bytes.
3674
///

src/libstd/sys/unix/fd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl FileDesc {
3131
/// Extracts the actual filedescriptor without closing it.
3232
pub fn into_raw(self) -> c_int {
3333
let fd = self.fd;
34-
unsafe { mem::forget(self) };
34+
mem::forget(self);
3535
fd
3636
}
3737

src/libstd/sys/windows/handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Handle {
3232

3333
pub fn into_raw(self) -> HANDLE {
3434
let ret = self.0;
35-
unsafe { mem::forget(self) }
35+
mem::forget(self);
3636
return ret;
3737
}
3838

0 commit comments

Comments
 (0)