-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Document subtleties of ManuallyDrop
#130279
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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.
I think documentation should clarify that this also applies to any types that directly contain
Box<_>
, like(Box<T>, u8)
orstruct Foo(Box<T>);
.I also believe that per current rules, calling
ManuallyDrop::drop()
on any type that directly containsBox<_>
is insta-UB, even if you don’t move it afterwards. Technically, it doesn’t neccessarily count as producing an invalid value:but at least touching it in any way (like creating a reference) is certainly UB.
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.
If you don't name the place after calling
ManuallyDrop::drop
, everything is fine; the place isn't touched after that point. BecauseManuallyDrop
does not have any drop glue, it does not get adrop_in_place
terminator at the end of scope, and thus the place is never "used" implicitly by the language, and no reference is created unless you create it yourself.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 have reworded the text to clarify that the UB also applies to types containing
Box
.I am pretty sure that calling
ManuallyDrop::drop()
on a type containingBox
is not UB. rust-lang/unsafe-code-guidelines#245 only states that using theManuallyDrop
later is UB. If you believe thatManuallyDrop::drop()
is insta-UB, then I would like a citation to prove otherwise.Of note, Miri doesn't seem to complain anything if I create a reference to an already-dropped
ManuallyDrop<Box<T>>
. I believe this is because we are still undecided on whether the existence of a&T
implies that theT
must be valid.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.
Re-reading a reference, I don’t think I can prove that it’s insta-UB. This is certainly UB per reference though:
since it produces (by writing to a place) an invalid value. Reference takes a conservative stance and declares references to invalid values to be invalid:
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 rust-lang/unsafe-code-guidelines#412:
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.
Yeah, I know. I still think that documentation should be coherent: if we document this as being unsound in reference, it should be also documented as being unsound in other places that deal with the same situation.
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 just found out that the nightly version of the reference now states: