-
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
deduplicate and clarify rules for converting pointers to references #128157
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7643ea5
create a new section on pointer to reference conversion
lolbinarycat 1073f97
remove duplicate explanations of the ptr to ref conversion rules
lolbinarycat 3877a7b
clarify interactions with MaybeUninit and UnsafeCell
lolbinarycat 988bc1c
fix typos in new pointer conversion docs
lolbinarycat b11e0a8
Apply suggestions from code review
cuviper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,24 +247,7 @@ impl<T: ?Sized> *mut T { | |
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that *either* the pointer is null *or* | ||
/// all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * The pointer must point to an initialized instance of `T`. | ||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get mutated (except inside `UnsafeCell`). | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// (The part about being initialized is not yet fully decided, but until | ||
/// it is, the only safe approach is to ensure that they are indeed initialized.) | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -313,24 +296,7 @@ impl<T: ?Sized> *mut T { | |
/// | ||
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * The pointer must point to an initialized instance of `T`. | ||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get mutated (except inside `UnsafeCell`). | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// (The part about being initialized is not yet fully decided, but until | ||
/// it is, the only safe approach is to ensure that they are indeed initialized.) | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// When calling this method, you have to ensure that the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -364,20 +330,9 @@ impl<T: ?Sized> *mut T { | |
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that *either* the pointer is null *or* | ||
/// all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get mutated (except inside `UnsafeCell`). | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
/// Note that because the created reference is to `MaybeUninit<T>`, the | ||
/// source pointer can point to uninitialized memory. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -609,25 +564,10 @@ impl<T: ?Sized> *mut T { | |
/// | ||
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that *either* the pointer is null *or* | ||
/// all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * The pointer must point to an initialized instance of `T`. | ||
/// When calling this method, you have to ensure that *either* | ||
/// the pointer is null *or* | ||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
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. That section is about shared references though, and this function returns a mutable reference. |
||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get accessed (read or written) through any other pointer. | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// (The part about being initialized is not yet fully decided, but until | ||
/// it is, the only safe approach is to ensure that they are indeed initialized.) | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -675,24 +615,8 @@ impl<T: ?Sized> *mut T { | |
/// | ||
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * The pointer must point to an initialized instance of `T`. | ||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get mutated (except inside `UnsafeCell`). | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// (The part about being initialized is not yet fully decided, but until | ||
/// it is, the only safe approach is to ensure that they are indeed initialized.) | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// When calling this method, you have to ensure that | ||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -727,20 +651,7 @@ impl<T: ?Sized> *mut T { | |
/// # Safety | ||
/// | ||
/// When calling this method, you have to ensure that *either* the pointer is null *or* | ||
/// all of the following is true: | ||
/// | ||
/// * The pointer must be properly aligned. | ||
/// | ||
/// * It must be "dereferenceable" in the sense defined in [the module documentation]. | ||
/// | ||
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is | ||
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. | ||
/// In particular, while this reference exists, the memory the pointer points to must | ||
/// not get accessed (read or written) through any other pointer. | ||
/// | ||
/// This applies even if the result of this method is unused! | ||
/// | ||
/// [the module documentation]: crate::ptr#safety | ||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion). | ||
#[inline] | ||
#[unstable(feature = "ptr_as_uninit", issue = "75402")] | ||
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wrong. It was copied from docs for a function that returns a mutable reference, but this section here claims to be about shared references.
EDIT: Actually this is not even right for mutable references. It's a weird mix of the two that is wrong for both cases.