-
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
Amend Rc/Arc::from_raw() docs regarding unsafety #68099
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -573,10 +573,11 @@ impl<T: ?Sized> Rc<T> { | |
/// Constructs an `Rc` from a raw pointer. | ||
/// | ||
/// The raw pointer must have been previously returned by a call to a | ||
/// [`Rc::into_raw`][into_raw]. | ||
/// [`Rc::into_raw`][into_raw] using the same `T`. | ||
/// | ||
/// This function is unsafe because improper use may lead to memory problems. For example, a | ||
/// double-free may occur if the function is called twice on the same raw pointer. | ||
/// This function is unsafe because improper use may lead to memory unsafety, | ||
/// even if `T` is never accessed. For example, a double-free may occur if the function is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this means "if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True as well, although the pitfall here is to assume accessing
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole paragraph seems a bit strange to me. Underspecified in a way. So what are the conditions for this method to be safe?
That's it, right? Shouldn't we explicitly document that? We can still mention the double free later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's worth noting (in my opinion) that the Other than that, I don't see any issues with the other points. unsafe fn clone_arc(ptr: *const T) -> Arc<T> {
let a = Arc::from_raw(ptr);
let copy = a.clone();
mem::forget(a);
copy
}
let ptr = Arc::into_raw(some_arc);
clone_arc(ptr);
clone_arc(ptr);
clone_arc(ptr); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I guess we can just say "the user of |
||
/// called twice on the same raw pointer. | ||
/// | ||
/// [into_raw]: struct.Rc.html#method.into_raw | ||
/// | ||
|
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 think this is not quite true, or at least is likely over-constraining. For example, I believe it is fine to go between the unsized representation and the sized representation via pointer casts (e.g,.
Rc<T>
unsizes toRc<dyn Debug>
and then you into_raw, cast to T, and from_raw).I'm not sure what exact wording we want to guarantee here, though, maybe just same T is in practice both sufficient and good enough for most use cases. Maybe something like "layout and alignment equivalent to T; note that this is essentially a reference to reference transmute if the T differs from the one used by into_raw, and so the same conditions apply"?
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.
Yes, I thought about that as well, it could be done. My thinking, however, is that it's too flaky to try to document under which conditions this is safe (and therefore binds all future changes to
Rc
/Arc
).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.
How about this? (basically the same what @Mark-Simulacrum suggested)
I usually wouldn't mind over-constraining, but if we document this exactly, this PR is just a doc bug fix.