-
Notifications
You must be signed in to change notification settings - Fork 489
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
Specify guarantees for repr(rust) structs #1152
Merged
ehuss
merged 7 commits into
rust-lang:master
from
Darksonn:type-layout-repr-rust-field-alignment
Sep 19, 2022
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a8a664
Specify guarantees for repr(rust) structs
Darksonn e9a1ddf
Address review
Darksonn 377c524
Fix typo
Darksonn f9ec1d2
Be less struct-centric
Darksonn 1275bcc
Rephrase layout of tuples
Darksonn 3ecc681
Remove stray 'minimum'
Darksonn 1ae7c2d
Link to default representation.
ehuss 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,9 +86,9 @@ String slices are a UTF-8 representation of characters that have the same layout | |
|
||
## Tuple Layout | ||
|
||
Tuples do not have any guarantees about their layout. | ||
Tuples are laid out according to the default representation. | ||
|
||
The exception to this is the unit tuple (`()`) which is guaranteed as a | ||
The exception to this is the unit tuple (`()`), which is guaranteed as a | ||
zero-sized type to have a size of 0 and an alignment of 1. | ||
Comment on lines
+91
to
92
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. Does the former statement imply this statement? 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. Not as I wrote it here, but you could certainly argue that it should be changed so it does. |
||
|
||
## Trait Object Layout | ||
|
@@ -162,7 +162,25 @@ representation will not change the layout of `Inner`. | |
Nominal types without a `repr` attribute have the default representation. | ||
Informally, this representation is also called the `rust` representation. | ||
|
||
There are no guarantees of data layout made by this representation. | ||
The only data layout guarantees made by this representation are those required | ||
for soundness. They are: | ||
|
||
1. The fields are properly aligned. | ||
2. The fields do not overlap. | ||
3. The alignment of the type is at least the maximum alignment of its fields. | ||
|
||
Formally, the first guarantee means that the offset of any field is divisible by | ||
that field's alignment. The second guarantee means that the fields can be | ||
ordered such that the offset plus the size of any field is less than or equal to | ||
the offset of the next field in the ordering. The ordering does not have to be | ||
the same as the order in which the fields are specified in the declaration of | ||
the type. | ||
|
||
Be aware that the second guarantee does not imply that the fields have distinct | ||
addresses: zero-sized types may have the same address as other fields in the | ||
same struct. | ||
|
||
Darksonn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There are no other guarantees of data layout made by this representation. | ||
|
||
### The `C` Representation | ||
|
||
|
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.
What does "the default representation" refer to 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.
The section with most of my changes has the heading "The Default Representation", and that's what I'm referring to. I don't know if there's a better way to phrase it, or perhaps add a link to the heading?
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.
+1 a link to the heading would be great, and with an explicit note that they may not be the same representation as a struct with equivalent fields.