-
Notifications
You must be signed in to change notification settings - Fork 105
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 that writes to padding are not preserved #1660
Conversation
@saethlin Following up on our conversation at RustConf, does the wording in this warning accurately describe the behavior wrt padding bytes? |
ad9b0bc
to
a01ce83
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1660 +/- ##
=======================================
Coverage 88.00% 88.01%
=======================================
Files 16 16
Lines 5801 5805 +4
=======================================
+ Hits 5105 5109 +4
Misses 696 696 ☔ View full report in Codecov by Sentry. |
src/lib.rs
Outdated
@@ -1953,6 +1953,15 @@ fn swap<T, U>((t, u): (T, U)) -> (U, T) { | |||
/// overhead. This is useful whenever memory is known to be in a zeroed state, | |||
/// such memory returned from some allocation routines. | |||
/// | |||
/// # Warning: Padding bytes | |||
/// | |||
/// Note that, when an object is moved or copied, only the non-padding bytes of |
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.
Rust does not have objects. We have allocations and typed or untyped copies. Untyped copies copy all bytes in the specified range, and currently the only way you can do this is ptr::copy
and ptr::copy_nonoverlapping
.
Typed copies are every other copy, which includes (some of these may be surprising to users) assignment, reads/writes through pointers, function arguments, and function return values.
Typed copies are not guaranteed to preserve bytes which are padding in all variants, of the type for the copy (the allocation doesn't have a type). That's probably what this comment means to identify.
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, "typed copy" is what I'm referring to. Do you know if that term is defined anywhere in the Reference or stdlib docs? A quick Google doesn't turn anything up.
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 docs for ptr::copy contain a probably-unhelpful mention of "untyped".
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.
Do you know of any internal docs/discussions that define the term? Or is this just one of those "compiler people use these terms and know what they mean when they talk to each other and they weren't expecting you to look inside the compiler" things?
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.
@rust-lang/opsem do we have documentation for a typed copy?
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.
Ugh of course that doesn't work.
@CAD97 @digama0 @JakobDegen @RalfJung do we have documentation for what a typed copy 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 don't know of any proper docs for this, no. That's clearly something we should fix. Any volunteers? :)
One question is where these docs would even go. The primary syntax for a typed copy is a = b;
, so maybe this should be here?
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.
s/object/value, but otherwise I'm gonna merge this and we can nitpick the wording later once docs have landed to Rust that we can cite.
Document that writes to padding bytes are not preserved when an object is moved or copied. Makes progress on #1648
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.
Approved, contingent on this change.
src/lib.rs
Outdated
@@ -1953,6 +1953,15 @@ fn swap<T, U>((t, u): (T, U)) -> (U, T) { | |||
/// overhead. This is useful whenever memory is known to be in a zeroed state, | |||
/// such memory returned from some allocation routines. | |||
/// | |||
/// # Warning: Padding bytes | |||
/// | |||
/// Note that, when an object is moved or copied, only the non-padding bytes of |
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.
/// Note that, when an object is moved or copied, only the non-padding bytes of | |
/// Note that, when an value is moved or copied, only the non-padding bytes of |
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.
Done
a01ce83
to
afb1b76
Compare
Document that writes to padding bytes are not preserved when an object is moved or copied.
Makes progress on #1648