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.
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
Rewrite docs for pointer methods #53783
Rewrite docs for pointer methods #53783
Changes from 22 commits
911d35f
da58beb
9f5a3cc
04a08c6
7b2ef6b
6f7338b
30122e9
e40585f
ea5570c
3a55c85
95a9088
7e165d9
c8da321
b0c5dc2
098bec8
fc63113
1ec66fb
e869b81
d97f61f
c06f551
2741224
18a7bdb
4ed469c
dc2237c
b463871
408a6a0
755de3c
bc809e0
78f5b68
c44e88c
2713d36
0ec87d0
adcc0d2
c197dc4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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'm not entirely sure what's going on here? Is it overwriting the pointer inside the
Box
itself? This seems like a situation where Deref can make things confusing if you're not careful - i.e. changing&mut v
to&mut *v
(adding the asterisk to force Deref) now means you're overwriting the heap contents rather than the pointer itself.Making this
&mut v as *mut Box<i32>
would make this much more obvious that you're overwriting a heap pointer with null. (Though it's an extremely heavy-handed way to do it, sincenull_mut()
exists...)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.
My intention here was to demonstrate that creating an invalid value can result in UB later in the program, outside of the
unsafe
block which caused the issue.Rereading it now, it does seem a bit convoluted. Perhaps we could write an arbitrary value (instead of 0), or-- since
NonZeroU32
is now stable--rewrite this example to invalidate one of those instead.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 added the
as *mut Box<i32>
. However, TBH I am not sure how useful this example really is.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 am... very suspect of saying that this is "fine". Specifically, passing an invalid value seems incredibly suspect -
fn foo(&T); foo(&*std::ptr::null())
should, in my opinion, be UB, and this is equivalent.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.
Would
ManuallyDrop::new(v)
be any better ? We are just moving an invalid value into a memory location such thatdrop
won't be called.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.
Hm, yeah, this violates the validity invariant. It is UB to touch this in any way.
I will change the example to instead overwrite it with a valid value again: