Skip to content

Commit e43e1ce

Browse files
committed
Documented the #[repr(align(x))] attribute.
1 parent fe87bd7 commit e43e1ce

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

Diff for: src/attributes.md

+5-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
> _MetaItem_ :
1414
>       IDENTIFIER
1515
>    | IDENTIFIER `=` LITERAL
16+
>    | IDENTIFIER `(` LITERAL `)`
1617
>    | IDENTIFIER `(` _MetaSeq_ `)`
1718
>    | IDENTIFIER `(` _MetaSeq_ `,` `)`
1819
>
@@ -30,6 +31,8 @@ may appear as any of:
3031
* A single identifier, the attribute name
3132
* An identifier followed by the equals sign '=' and a literal, providing a
3233
key/value pair
34+
* An identifier followed by a parenthesized literal, providing a
35+
key/value pair
3336
* An identifier followed by a parenthesized list of sub-attribute arguments
3437

3538
Attributes with a bang ("!") after the hash ("#") apply to the item that the
@@ -130,26 +133,8 @@ interpreted:
130133
- `linkage` - on a static, this specifies the [linkage
131134
type](http://llvm.org/docs/LangRef.html#linkage-types).
132135

133-
On `enum`s:
134-
135-
- `repr` - on C-like enums, this sets the underlying type used for
136-
representation. Takes one argument, which is the primitive
137-
type this enum should be represented for, or `C`, which specifies that it
138-
should be the default `enum` size of the C ABI for that platform. Note that
139-
enum representation in C is implementation-defined, and may not be compatible
140-
when the C code is compiled with certain flags.
141-
142-
On `struct`s:
143-
144-
- `repr` - specifies the representation to use for this struct. Takes a list
145-
of options. The currently accepted ones are `C` and `packed`, which may be
146-
combined. `C` will use a C ABI compatible struct layout, and `packed` will
147-
remove any padding between fields (note that this is very fragile and may
148-
break platforms which require aligned access).
149-
150-
On `union`s:
151-
152-
- `repr` - Same as per `struct`.
136+
See [type layout](type-layout.html) for documentation on the `repr` attribute
137+
which can be used to control type layout.
153138

154139
## Macro-related attributes
155140

Diff for: src/type-layout.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,29 @@ For all other enumerations, the layout is unspecified.
264264

265265
Likewise, combining two primitive representations together is unspecified.
266266

267+
### The `align` Representation
268+
269+
The `align` representation can be used on `struct`s and `union`s to raise the
270+
alignment of the type to a given value.
271+
272+
Alignment is specified as a parameter in the form of `#[repr(align(x))]`. The
273+
alignment value must be a power of two of type `u32`. The `align` representation
274+
can raise the alignment of a type to be greater than it's primitive alignment,
275+
it cannot lower the alignment of a type.
276+
277+
The `align` and `packed` representations cannot be applied on the same type and
278+
a `packed` type cannot transitively contain another `align`ed type.
279+
267280
### The `packed` Representation
268281

269282
The `packed` representation can only be used on `struct`s and `union`s.
270283

271284
It modifies the representation (either the default or `C`) by removing any
272285
padding bytes and forcing the alignment of the type to `1`.
273286

287+
The `align` and `packed` representations cannot be applied on the same type and
288+
a `packed` type cannot transitively contain another `align`ed type.
289+
274290
> Warning: Dereferencing an unaligned pointer is [undefined behaviour] and it is
275291
> possible to [safely create unaligned pointers to `packed` fields][27060].
276292
> Like all ways to create undefined behavior in safe Rust, this is a bug.
@@ -284,4 +300,4 @@ padding bytes and forcing the alignment of the type to `1`.
284300
[C-like enumerations]: items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
285301
[zero-variant enumerations]: items/enumerations.html#zero-variant-enums
286302
[undefined behavior]: behavior-considered-undefined.html
287-
[27060]: https://github.com/rust-lang/rust/issues/27060
303+
[27060]: https://github.com/rust-lang/rust/issues/27060

0 commit comments

Comments
 (0)