-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Clarify as_uninit_mut may point to uninitialized memory
#158804
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
Open
yilin0518
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
yilin0518:fix_as_uninit_mut
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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.
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 formulation seems backwards to me, and this information also seems already to be included in "In contrast to [
as_mut], this does not require that the value has to be initialized." which is a few lines above this.View changes since the review
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.
The purpose of the change I made is let the safety documentation of this API consistent with other APIs(*mut T::as_uninit_ref, *const T::as_uninit_ref and std::ptr::NonNull::as_uninit_mut). All these APIs has this pattern in their safety documentation. Although "In contrast to [as_mut], this does not require that the value has to be initialized." has implied the uninitiazlition, it seems that explicitly emphasize this in
#Safetyis better. And thank you for your review and feedback!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 see. Also, you're welcome to the review and thanks for trying to improve the docs and their consistency in particular.
I'm not sure I agree this needs to be repeated in the SAFETY section, as it is not a safety condition that will have to be dispatched in a safety comment using these methods. The
MaybeUninittype already makes it very difficult to accidentally mishandleMaybeUninit<T>asT.Anyway, assuming something like this is included in the SAFETY section, I would prefer a reformulation. From "Note that because the created reference is to
MaybeUninit<T>, the source pointer can point to uninitialized memory." to perhaps one of:T.T.Or if the point is instead about the argument:
selfmay point to uninitialized memory.What do you think?
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.
From "Note that because the created reference is to
MaybeUninit<T>, the source pointer can point to uninitialized memory." to perhaps one of:Well I think reformulation is optional, but adding the uninitialization hint in SAFETY section is better than in other place, because SAFETY section needs to remind developers that what they should care about when using this API.
But I think the first discription is great if needing reformulation. It is more concise. And the other point we should care is that the documentation of these APIs need modification if reformulation is accepted. I can finfish the work and apply in this PR.