-
Notifications
You must be signed in to change notification settings - Fork 13k
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
mem::forget
is unsafe, but can be written in entirely safe code
#24456
Comments
Nominating for 1.0 because some men just want to watch the world burn. |
This is one of the root causes of #24292 |
Hmm... I was trying to understand this example, but it doesn't compile:
My first guess was to wrap
|
Here is a working example: fn forget<T>(val: T) {
use std::cell::RefCell;
use std::rc::Rc;
struct Foo<T>(T, RefCell<Option<Rc<Foo<T>>>>);
let x = Rc::new(Foo(val, RefCell::new(None)));
*x.1.borrow_mut() = Some(x.clone());
}
struct DontDropMe;
impl Drop for DontDropMe {
fn drop(&mut self) { unreachable!() }
}
fn main() {
forget(DontDropMe)
} |
My current thinking:
|
@nikomatsakis Did you mean "4. If we did step 3, [...]" rather than "4. If we did step 2, [...]"? |
@nikomatsakis My concern is that (3) couldn't be done backwards-compatibly, at least not without making it unsafe to create a new type that leaks like |
If you have |
A safe
Making an unsafe function safe is a (minor) breaking change ( We might want two functions:
|
I commented on #24292, but the gist is that there are multiple ways to leak memory today (e.g. #14875 and #16135), so a targeted solution at |
I have created an RFC for marking |
By the way, is it okay to leak references through
|
closing this issue to encourage all further discussion of this matter to happen on the RFC thread, see rust-lang/rfcs#1066 |
@pnkfelix seems weird to close the issue if it's not fixed, how about just leaving the comment? |
@reem the idea was to funnel conversation over to the RFC. A single comment in the middle of a thread won't do that. (To be clear: this was was the outcome of the team triage, since the issue had been nominated; it was was a group decision. My initial instinct had been to let it sit for a week with the nomination, but was persuaded by arguments to close in favor of the RFC.) |
For instance:
This is problematic, and an indicator that something is amiss in our rules.
cc @aturon @nikomatsakis @bstrie from IRC
The text was updated successfully, but these errors were encountered: