-
Notifications
You must be signed in to change notification settings - Fork 13.4k
deduplicate and clarify rules for converting pointers to references #128157
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
Merged
Merged
Changes from 3 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -56,6 +56,52 @@ | |
//! has size 0, i.e., even if memory is not actually touched. Consider using | ||
//! [`NonNull::dangling`] in such cases. | ||
//! | ||
//! ## Pointer to reference conversion | ||
//! When converting a pointer to a reference `&T` using `&*`, | ||
//! there are several rules that must be followed: | ||
//! | ||
//! * The pointer must be properly aligned. | ||
//! | ||
// some microprocessors may use address 0 for an interrupt vector. | ||
// users of these microprocessors must always read/write address 0 through | ||
// a raw pointer, not a reference. | ||
//! * It must be non-null. | ||
//! | ||
//! * It must be "dereferenceable" in the sense defined above. | ||
//! | ||
//! * The pointer must point to a valid value of type `T`. | ||
//! This means that the created reference can only refer to | ||
//! uninitialized memory through careful use of `MaybeUninit`, | ||
//! or if the uninitialized memory is entirly contained within | ||
cuviper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! padding bytes, since | ||
//! [padding has the same validity invariant as `MaybeUninit`][ucg-pad]. | ||
//! | ||
//! * You must enforce Rust's aliasing rules, since the lifetime of the | ||
//! created reference 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 raw pointer, | ||
//! except for data inside an `UnsafeCell`. | ||
//! Note that aliased writes are always UB for mutable references, | ||
//! even if they only modify `UnsafeCell` data. | ||
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 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. |
||
//! | ||
//! If a pointer follows all of these rules, it is said to be | ||
//! *convertable to a reference*. | ||
// ^ we use this term instead of saying that the produced reference must | ||
// be valid, as the validity of a reference is easily confused for the | ||
// validity of the thing it refers to, and while the two concepts are | ||
// closly related, they are not identical. | ||
//! | ||
//! These apply even if the result 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.) | ||
//! | ||
//! An example of the implications of the above rules is that an expression such | ||
//! as `unsafe { &*(0 as *const u8) }` is Immediate Undefined Behavior. | ||
//! | ||
//! [ucgpad]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#padding | ||
//! | ||
//! ## Allocated object | ||
//! | ||
//! An *allocated object* is a subset of program memory which is addressable | ||
|
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.