-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
from_ref, from_mut: clarify documentation #125897
Changes from 2 commits
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 |
---|---|---|
|
@@ -777,8 +777,17 @@ where | |
|
||
/// Convert a reference to a raw pointer. | ||
/// | ||
/// This is equivalent to `r as *const T`, but is a bit safer since it will never silently change | ||
/// type or mutability, in particular if the code is refactored. | ||
/// For `r: &T`, `from_ref(r)` is equivalent to `r as *const T`, but is a bit safer since it will | ||
/// never silently change type or mutability, in particular if the code is refactored. | ||
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. In addition, I think it is worth also clarifying that for However, https://doc.rust-lang.org/reference/type-coercions.html#coercion-types does seem to say that when 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.
Actually, 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 don't think this is the right place to discuss the semantics of coercions. They behave the same everywhere, surely we don't want to explain them in each function that takes a reference. (e.g. I also don't see why this PR should be blocked on #56604. That issue is long-standing so I don't expect it to be resolved soon, this clarification however is necessary short-term according to yourself as you said it is ambiguous.
IMO the new wording is not necessary under any ruleset, since 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 analogous to the situation for
In both cases, the construct is designed to make things safer, but not understanding the interaction with coercions is especially dangerous. 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 suggest adding the following text, which really gets to the point: For |
||
/// | ||
/// The caller must ensure that the pointee outlives the pointer this function returns, or else it | ||
/// will end up pointing to garbage. | ||
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 suggest replacing "pointing to garbage" with "dangling". I also suggest adding here a concrete example from the clippy experience: For example,
to
. |
||
/// | ||
/// The caller must also ensure that the memory the pointer (non-transitively) points to is never | ||
/// written to (except inside an `UnsafeCell`) using this pointer or any pointer derived from it. If | ||
/// you need to mutate the pointee, use [`from_mut`]`. Specifically, to turn a mutable reference `m: | ||
/// &mut T` into `*const T`, prefer `from_mut(m).cast_const()` to obtain a pointer that can later be | ||
/// used for mutation. | ||
#[inline(always)] | ||
#[must_use] | ||
#[stable(feature = "ptr_from_ref", since = "1.76.0")] | ||
|
@@ -791,8 +800,11 @@ pub const fn from_ref<T: ?Sized>(r: &T) -> *const T { | |
|
||
/// Convert a mutable reference to a raw pointer. | ||
/// | ||
/// This is equivalent to `r as *mut T`, but is a bit safer since it will never silently change | ||
/// type or mutability, in particular if the code is refactored. | ||
/// The caller must ensure that the pointee outlives the pointer this function returns, or else it | ||
/// will end up pointing to garbage. | ||
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. Same changes here: avoid "garbage" and also add an example of what to avoid. Also, the structure of this comment should be made symmetric with the one for |
||
/// | ||
/// For `r: &mut T`, `from_mut(r)` is equivalent to `r as *mut T`, but is a bit safer since it will | ||
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. Same here: scale back the "is equivalent to" claim. |
||
/// never silently change type or mutability, in particular if the code is refactored. | ||
#[inline(always)] | ||
#[must_use] | ||
#[stable(feature = "ptr_from_ref", since = "1.76.0")] | ||
|
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.
We shouldn't be leading off with this incorrect statement. I suggest we replcae "is equivalent" is "is often but not always equivalent".