-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: Add #[repr(pack = "N")] #1399
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
Merged
nikomatsakis
merged 7 commits into
rust-lang:master
from
retep998:optimal-rabbit-packing
Apr 22, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dea4612
RFC: Add #[repr(pack = "N")]
retep998 c6e522b
More alternative
retep998 c92aa33
Clarify that this is really needed
retep998 898c791
Clarify wrongness of first alternative
retep998 6e269de
Address cmr's nit
retep998 64f6229
More cmr nitfixes
retep998 cbb18a4
Am I doing this right?
retep998 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
- Feature Name: `repr_pack` | ||
- Start Date: 2015-12-06 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
Extend the existing `#[repr]` attribute on structs with a `pack = "N"` option to | ||
specify a custom packing for `struct` types. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
Many C/C++ compilers allow a packing to be specified for structs which | ||
effectively lowers the alignment for a struct and its fields (for example with | ||
MSVC there is `#pragma pack(N)`). Such packing is used extensively in certain | ||
C/C++ libraries (such as Windows API which uses it pervasively making writing | ||
Rust libraries such as `winapi` challenging). | ||
|
||
At the moment the only way to work around the lack of a proper | ||
`#[repr(pack = "N")]` attribute is to use `#[repr(packed)]` and then manually | ||
fill in padding which is a burdensome task. Even then that isn't quite right | ||
because the overall alignment of the struct would end up as 1 even though it | ||
needs to be N (or the default if that is smaller than N), so this fills in a gap | ||
which is impossible to do in Rust at the moment. | ||
|
||
# Detailed design | ||
[design]: #detailed-design | ||
|
||
The `#[repr]` attribute on `struct`s will be extended to include a form such as: | ||
|
||
```rust | ||
#[repr(pack = "2")] | ||
struct LessAligned(i16, i32); | ||
``` | ||
|
||
This structure will have an alignment of 2 and a size of 6, as well as the | ||
second field having an offset of 2 instead of 4 from the base of the struct. | ||
This is in contrast to without the attribute where the structure would have an | ||
alignment of 4 and a size of 8, and the second field would have an offset of 4 | ||
from the base of the struct. | ||
|
||
Syntactically, the `repr` meta list will be extended to accept a meta item | ||
name/value pair with the name "pack" and the value as a string which can be | ||
parsed as a `u64`. The restrictions on where this attribute can be placed along | ||
with the accepted values are: | ||
|
||
* Custom packing can only be specified on `struct` declarations for now. | ||
Specifying a different packing on perhaps `enum` or `type` definitions should | ||
be a backwards-compatible extension. | ||
* Packing values must be a power of two. | ||
|
||
By specifying this attribute, the alignment of the struct would be the smaller | ||
of the specified packing and the default alignment of the struct. The alignments | ||
of each struct field for the purpose of positioning fields would also be the | ||
smaller of the specified packing and the alignment of the type of that field. If | ||
the specified packing is greater than or equal to the default alignment of the | ||
struct, then the alignment and layout of the struct should be unaffected. | ||
|
||
When combined with `#[repr(C)]` the size alignment and layout of the struct | ||
should match the equivalent struct in C. | ||
|
||
`#[repr(packed)]` and `#[repr(pack = "1")]` should have identical behavior. | ||
|
||
Because this lowers the effective alignment of fields in the same way that | ||
`#[repr(packed)]` does (which caused [issue #27060][gh27060]), while accessing a | ||
field should be safe, borrowing a field should be unsafe. | ||
|
||
Specifying `#[repr(packed)]` and `#[repr(pack = "N")]` where N is not 1 should | ||
result in an error. | ||
|
||
Specifying `#[repr(pack = "A")]` and `#[repr(align = "B")]` should still pack | ||
together fields with the packing specified, but then increase the overall | ||
alignment to the alignment specified. Depends on [RFC #1358][rfc1358] landing. | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
Duplication in the language where `#[repr(packed)]` and `#[repr(pack = "1")]` | ||
have identical behavior. | ||
|
||
# Alternatives | ||
[alternatives]: #alternatives | ||
|
||
* The alternative is not doing this and forcing people to continue using | ||
`#[repr(packed)]` with manual padding, although such structs would always have | ||
an alignment of 1 which is often wrong. | ||
* Alternatively a new attribute could be used such as `#[pack]`. | ||
* `#[repr(packed)]` could be extended as either `#[repr(packed(N))]` or | ||
`#[repr(packed = "N")]`. | ||
|
||
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
* The behavior specified here should match the behavior of MSVC at least. Does | ||
it match the behavior of other C/C++ compilers as well? | ||
* Should it still be safe to borrow fields whose alignment is less than or equal | ||
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. It should have the same behavior of |
||
to the specified packing or should all field borrows be unsafe? | ||
|
||
[gh27060]: https://github.com/rust-lang/rust/issues/27060 | ||
[rfc1358]: https://github.com/rust-lang/rfcs/pull/1358 |
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.
This must be resolved. Verify against the behavior of clang (or gcc).
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.
Specifically clang or gcc on a platform other than Windows. On Windows they are supposed to match the msvc ABI.