-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std: Mark mem::forget
as a safe function
#25187
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @aturon I'm specifically curious on suggestions for improvements to the doc-comment on |
/// The safety of this function implies that when writing `unsafe` code | ||
/// yourself, you cannot write a primitive that relies on a destructor running | ||
/// to preserve memory safety. Unsafe code must be resilient to destructors not | ||
/// running in all circumstances. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very confusing. We can't rely on destructors? Of course we can, as long as we don't give away the ownership of the value (so no risk of forget or being passed into an Rc owner, for example).
We rely on stack guards in our code, for example here https://github.com/rust-lang/rust/blob/master/src/libstd/io/mod.rs#L72-L78
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed some minor updates to the wording here, but I will echo @aturon in that explicitly specifying this kind of detail is likely best left to a future RFC with a more focused discussion.
e8ea4b4
to
a3aabdf
Compare
@@ -235,7 +235,6 @@ extern "rust-intrinsic" { | |||
/// | |||
/// `forget` is unsafe because the caller is responsible for | |||
/// ensuring the argument is deallocated already. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forget
is no longer unsafe.
Might be a breaking change because the calling convention of |
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 rust-lang#25186
a3aabdf
to
dd59b1f
Compare
@tbu- strictly speaking this is a breaking change, but due to the current implementation in the compiler it's not a breaking change because taking the function by value (which is when the ABI in the type starts mattering) you get an ICE: use std::mem;
fn main() {
let f = mem::forget::<i32>;
}
|
@bluss observes back in rust-lang/rfcs#1066 that |
@alexcrichton Thanks for updating the text! |
@bors r+ p=1 |
📌 Commit dd59b1f has been approved by |
@bors: p=50 (putting beta-accepted at the top) |
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
What's the reason that |
This commit is an implementation of RFC 1066 where the conclusion was
that leaking a value is a safe operation in Rust code, so updating the signature
of this function follows suit.
Closes #25186