Skip to content
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

RFC: Add #[repr(pack = "N")] #1399

Merged
merged 7 commits into from Apr 22, 2016

Conversation

Projects
None yet
10 participants
@retep998
Copy link
Member

retep998 commented Dec 7, 2015

Rendered

retep998 added some commits Dec 7, 2015

RFC: Add #[repr(pack = "N")]
Signed-off-by: Peter Atashian <retep998@gmail.com>
More alternative
Signed-off-by: Peter Atashian <retep998@gmail.com>
Clarify that this is really needed
Signed-off-by: Peter Atashian <retep998@gmail.com>
* 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 otherwise. The

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

s/otherwise//

Clarify wrongness of first alternative
Signed-off-by: Peter Atashian <retep998@gmail.com>
# Drawbacks
[drawbacks]: #drawbacks

This would unfortunately make my life easier even though one of the unstated

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

Please remove this paragraph.

Address cmr's nit
Signed-off-by: Peter Atashian <retep998@gmail.com>
# Unresolved questions
[unresolved]: #unresolved-questions

* The behavior specified here should match the behavior of MSVC at least. Does

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

This must be resolved. Verify against the behavior of clang (or gcc).

This comment has been minimized.

@retep998

retep998 Dec 7, 2015

Author Member

Specifically clang or gcc on a platform other than Windows. On Windows they are supposed to match the msvc ABI.


* 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

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

It should have the same behavior of #[repr(packed)] I think.

@cmr

This comment has been minimized.

Copy link
Member

cmr commented Dec 7, 2015

The feature is necessary. If this behavior is in line with common C compilers, :shipit:

[motivation]: #motivation

Many C/C++ compilers allow a packing to be specified for structs which
effectivally lowers the alignment for a struct and its fields (for example with

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

s/effectivally/effectively/

effectivally 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 all over the place making
writing Rust libraries such as `winapi` a nightmare).

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

s/a nightmare/challenging/

Many C/C++ compilers allow a packing to be specified for structs which
effectivally 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 all over the place making

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

s/all over the place/pervasively/

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 basically impossible to do in Rust at the moment.

This comment has been minimized.

@cmr

cmr Dec 7, 2015

Member

s/basically //

@DanielKeep

This comment has been minimized.

Copy link

DanielKeep commented Dec 7, 2015

For reference, here's an example of Clang doing this.

retep998 added some commits Dec 7, 2015

More cmr nitfixes
Signed-off-by: Peter Atashian <retep998@gmail.com>
Am I doing this right?
Signed-off-by: Peter Atashian <retep998@gmail.com>
@retep998

This comment has been minimized.

Copy link
Member Author

retep998 commented Dec 7, 2015

See this comment for an interesting interaction edge case with the alignment RFC regarding required alignment.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Apr 8, 2016

Hear ye, hear ye! This RFC is now entering final comment period.

@ubsan

This comment has been minimized.

Copy link
Contributor

ubsan commented Apr 8, 2016

This seems right to have. I think the best is #[repr(packed(1))] or #[repr(packed = "1")]. So the default of #[repr(packed)] would be #[repr(packed(1))].

@solson

This comment has been minimized.

Copy link
Member

solson commented Apr 11, 2016

@ubsan #[repr(packed(1))] is not currently valid attribute syntax (but #[repr(packed = "1")] is). I guess the RFC mentions this, so cc @retep998 as well.

It could become valid with #1559, though, or as @nrc mentions in a comment, the procedural macros RFC.

@retep998

This comment has been minimized.

Copy link
Member Author

retep998 commented Apr 11, 2016

I don't mind the syntax either way, as long as I can have this attribute at all.

@Dr-Emann

This comment has been minimized.

Copy link

Dr-Emann commented on 64f6229 Apr 11, 2016

Can't you force an alignment with a [u64; 0] or the like?

This comment has been minimized.

Copy link
Owner Author

retep998 replied Apr 11, 2016

@Dr-Emann That doesn't work with #[repr(packed)] since that would force the [u64; 0] to have an alignment of 1. Once you've used #[repr(packed)] there is no way to bring the alignment back up.

@starkat99

This comment has been minimized.

Copy link

starkat99 commented Apr 13, 2016

I think it should just extend #[repr(packed)] rather than adding a new pack syntax. The duplication is confusing otherwise. Just have #[repr(packed)] be default to #[repr(packed = "1")].

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Apr 20, 2016

Hmm, I think I too prefer #[repr(packed = "N")]. Otherwise, the RFC seems unobjectionable to me. It'd also be nice to approve #1358, which covers very similar subject matter.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Apr 20, 2016

One area the RFC did not (explicitly) address -- or I missed it -- is what to do if there are multiple packed specifiers. It did say that #[repr(pack = "2")] and #[repr(packed)] cannot be combined, but it did not say anything about #[repr(pack = "2")] and #[repr(pack = "4")]. Presumably this is an error as well? (Unlike the proposed align RFC?)

Is there any reason one might want multiple specifiers? Like, independent macros might add tighter and tighter requirements? (However, unlike with alignment, it's not obvious to me whether you would want the max or min packing that was specified.)

@retep998

This comment has been minimized.

Copy link
Member Author

retep998 commented Apr 20, 2016

I think if we did allow specifying the packing multiple times, that the right thing to do would be to use the smallest packing specified. The alignment of any given field is min(default, packing) so extending packing to be min(packing1, packing2) seems fairly natural.

As for the align RFC, there'd need to be a decision on how it interacts with packing. Whether it does the msvc style __declspec(align(N)) with required alignment that cannot be lowered by packing, or the gcc style alignment attribute that can be lowered by packing.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Apr 22, 2016

Huzzah! The @rust-lang/lang team has decided to accept this RFC, with the caveat that the attribute be renamed to packed (and not pack). I will make this change when merging (I assume @retep998 you do not object.)

We discussed a few other things:

  • We should (obviously) clarify the relationship to an align attribute. We're going to bring the align RFC into FCP, discussion can happen there. But in the @rust-lang/lang meeting, I think the general feeling was that if both attributes are present, then the specified align would overrule the alignment derived from packing (in other words, we would match the MSVC style). Another option is to forbid both options from being used simultaneously, but that seems unnecessary to me.

@nikomatsakis nikomatsakis referenced this pull request Apr 22, 2016

Closed

`#[repr(packed(N))]` (tracking issue for RFC 1399) #33158

0 of 2 tasks complete
@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Apr 22, 2016

Tracking issue: rust-lang/rust#33158

If you'd like to keep following the development of this feature, please subscribe to that issue, thanks! :)

nikomatsakis added a commit to nikomatsakis/rfcs that referenced this pull request Apr 22, 2016

merge RFC rust-lang#1399
also adjust from `#[repr(pack = "N")]` to `#[repr(packed = "N")]`

@nikomatsakis nikomatsakis merged commit cbb18a4 into rust-lang:master Apr 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.