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

Document repr packed(N). #553

Merged
merged 5 commits into from
Apr 6, 2019
Merged

Document repr packed(N). #553

merged 5 commits into from
Apr 6, 2019

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Mar 25, 2019

Closes #483

I feel like this is lacking, but I'm not sure what else to add.

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My overall sense is that the text added here would be more succinctly described in a few equations, but I think this is mostly good for now.

src/type-layout.md Outdated Show resolved Hide resolved
src/type-layout.md Outdated Show resolved Hide resolved
src/type-layout.md Outdated Show resolved Hide resolved
It modifies the representation (either the default or `C`) by removing any
padding bytes and forcing the alignment of the type to `1`.
The `packed` representation sets the alignment to the smaller of the specified
packing and the current alignment (either default or `C`). The alignments of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the "default" alignment is referenced but above "primitive alignment" is used... we should pick one phrasing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of both "current' and "primitive". Get rid of the parenthetical. Instead spell it out explicitly that if the type has the default or C representation, and the alignment from that representation is smaller, it will use that alignment.

It modifies the representation (either the default or `C`) by removing any
padding bytes and forcing the alignment of the type to `1`.
The `packed` representation sets the alignment to the smaller of the specified
packing and the current alignment (either default or `C`). The alignments of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"or C" is not super clear wrt. referring to C-the-language

@ehuss
Copy link
Contributor Author

ehuss commented Mar 25, 2019

I added an experimental commit to propose a different way of documenting these repr instructions. I think it more accurately captures how it works. That is, there are some basic "representations", and packed and align just alter them. In the relevant cases (on stable) there are only two — C and default. I think it makes it clearer about what can be "combined".

I do have some concerns and doubts:

  • This introduces new terminology ("modifier") that I made up.
  • The descriptions between align and packed have a lot of duplication.
  • I think that align and packed force the alignment to the given value. However, I'm not certain. The parsing code calls them "hints" in the error messages, but that terminology is not used elsewhere. I'm fairly sure that align is forced here and packed is forced here. This 1000 line function is a bit hard to follow.
    • Related: I think that packed has a floor of the i8 alignment here. This is always 8-bits on all platforms rustc builds on that I can find. I tried building with a custom target where it was 16-bits, but libcore fails to build with i8:16:32 alignment (and I need the align_of function to test my hypothesis). I'm not sure if this floor should be documented, or if Rust should always assume this to be "1"

src/type-layout.md Outdated Show resolved Hide resolved
@@ -113,13 +113,12 @@ All user-defined composite types (`struct`s, `enum`s, and `union`s) have a
*representation* that specifies what the layout is for the type.

The possible representations for a type are the default representation, `C`,
the primitive representations, `packed`, and `transparent`. Multiple
representations can be applied to a single type.
the primitive representations, and `transparent`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Itemize ^

Also make it clear what the "primitive representations" entail (by itemizing all the primitive reprs)

src/type-layout.md Outdated Show resolved Hide resolved
`#[repr(align(x))]`. The alignment value must be a power of two from 1 up to
2<sup>29</sup>.

The `align` modifier raises the type's alignment to the specified alignment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a partial repetition of what you said in the leading paragraph.

2<sup>29</sup>.

The `align` modifier raises the type's alignment to the specified alignment.
If the specified alignment is less than the alignment of the type without the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this concept of "without the ** modifier" is referred to as the "natural **", for example "natural alignment". I think it would be good to introduce such terms earlier (when we first start to speak about alignment) and then use them.

The packing value is specified as an integer parameter in the form of
`#[repr(packed(x))]`. If no value is given, as in `#[repr(packed)]`, then the
packing value is 1. The packing value must be a power of two from 1 up to
2<sup>29</sup>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you extract out the wording about the default it is the same as the paragraph for align. So extract these two out into a common notion repr_value or something and then reference that one.

src/type-layout.md Outdated Show resolved Hide resolved
src/type-layout.md Outdated Show resolved Hide resolved
src/type-layout.md Outdated Show resolved Hide resolved
src/type-layout.md Outdated Show resolved Hide resolved
@ehuss
Copy link
Contributor Author

ehuss commented Mar 26, 2019

I attempted to reduce some of the duplication. It still seems a little awkward, though.

@Centril
Copy link
Contributor

Centril commented Mar 27, 2019

I think this is strict improvement for now. Thanks @ehuss.
Feel free to merge at will :)

@ehuss ehuss merged commit 98f90ff into rust-lang:master Apr 6, 2019
@retep998
Copy link
Member

retep998 commented Apr 6, 2019

Technically it would be more accurate to say packed lowers the alignment of the fields in the struct, rather than the struct itself. But since the alignment of the struct itself is determined from the maximum alignment of the fields, the struct alignment effectively decreases as well. align meanwhile increases the alignment of the struct but not its fields.

@Centril
Copy link
Contributor

Centril commented Apr 6, 2019

@retep998 Could you file a clarification PR?

Centril added a commit to Centril/rust that referenced this pull request Apr 6, 2019
Update books

## nomicon

1 commits in f1ff93b66844493a7b03101c7df66ac958c62418..c02e0e7754a76886e55b976a3a4fac20100cd35d
2019-02-26 13:37:28 -0500 to 2019-03-25 16:52:56 -0400
- dropck: The drop order is now defined (rust-lang/nomicon#113)

## reference

3 commits in 27ad493..98f90ff
2019-03-26 02:06:15 +0100 to 2019-04-06 09:29:08 -0700
- Document repr packed(N). (rust-lang/reference#553)
- Fix broken link in glossary. (rust-lang/reference#558)
- Typo fixes (rust-lang/reference#556)

## embedded-book

2 commits in 07fd3880ea0874d82b1d9ed30ad3427ec98b4e8a..7989c723607ef5b13b57208022259e6c771e11d0
2019-03-27 15:40:52 +0000 to 2019-04-04 12:14:37 +0000
- fix rust-embedded/book#182  (rust-embedded/book#183)
- Add openocd to list of installable packages  (rust-embedded/book#179)
Centril added a commit to Centril/rust that referenced this pull request Apr 6, 2019
Update books

## nomicon

1 commits in f1ff93b66844493a7b03101c7df66ac958c62418..c02e0e7754a76886e55b976a3a4fac20100cd35d
2019-02-26 13:37:28 -0500 to 2019-03-25 16:52:56 -0400
- dropck: The drop order is now defined (rust-lang/nomicon#113)

## reference

3 commits in 27ad493..98f90ff
2019-03-26 02:06:15 +0100 to 2019-04-06 09:29:08 -0700
- Document repr packed(N). (rust-lang/reference#553)
- Fix broken link in glossary. (rust-lang/reference#558)
- Typo fixes (rust-lang/reference#556)

## embedded-book

2 commits in 07fd3880ea0874d82b1d9ed30ad3427ec98b4e8a..7989c723607ef5b13b57208022259e6c771e11d0
2019-03-27 15:40:52 +0000 to 2019-04-04 12:14:37 +0000
- fix rust-embedded/book#182  (rust-embedded/book#183)
- Add openocd to list of installable packages  (rust-embedded/book#179)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants