You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add repr attribute template, and add combination rules
This consolidates the rules for how repr values can be combined. This
adds a few previously undocumented combinations, and I think all
permutations should now be covered.
-`align` and `packed` modifiers --- [layout.repr.alignment]
141
176
177
+
If multiple values are specified, then the combination of all the specified representations is used as specified in [layout.repr.attribute.combinations].
142
178
179
+
r[layout.repr.attribute.allowed-positions]
180
+
The `repr` attribute may only be applied to a [struct], [enum], or [union].
181
+
182
+
> [!NOTE]
183
+
> There are some restrictions on which `repr` values can be applied to specific kinds of items. For example, the primitive representations can only be applied to enumerations. The sections for each repr value describes their restrictions.
184
+
185
+
r[layout.repr.attribute.duplicates]
186
+
If the `repr` attribute is specified multiple times on an item, then the combination of all the specified values is used as specified in [layout.repr.attribute.combinations].
187
+
188
+
r[layout.repr.attribute.combinations]
189
+
Combinations of `repr` values on the same type are handled as follows:
190
+
191
+
r[layout.repr.attribute.combinations.transparent]
192
+
-`transparent` --- May not be combined with any other value.
193
+
r[layout.repr.attribute.combinations.rust]
194
+
-`Rust` --- May not be combined with primitive representations or `C`.
195
+
r[layout.repr.attribute.combinations.primitive]
196
+
- Multiple primitive representations are not allowed.
197
+
<!-- Note: Currently a future-compatible warning. -->
198
+
r[layout.repr.attribute.combinations.primitive-c]
199
+
- A primitive representation with `C` is described in [layout.repr.primitive-c].
-`align` and `packed` may only be applied to a type with the `Rust` or `C` representation.
207
+
r[layout.repr.attribute.combinations.align-max]
208
+
- If multiple align values are given, the maximum value is used.
209
+
r[layout.repr.attribute.combinations.packed-min]
210
+
- If multiple packed values are given, the minimum value is used.
211
+
r[layout.repr.attribute.combinations.rust-c-dupe]
212
+
- Duplicate instances of the `Rust` or `C` value are ignored.
143
213
144
214
r[layout.repr.inter-field]
145
215
The representation of a type can change the padding between fields, but does not change the layout of the fields themselves. For example, a struct with a `C` representation that contains a struct `Inner` with the `Rust` representation will not change the layout of `Inner`.
@@ -351,10 +421,13 @@ r[layout.repr.primitive]
351
421
### Primitive representations
352
422
353
423
r[layout.repr.primitive.intro]
354
-
The *primitive representations* are the representations with the same names as the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128`, and `isize`.
424
+
The *primitive representations* are used to change the representation of an enumeration. They have the same names as the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128`, and `isize`.
355
425
356
-
r[layout.repr.primitive.constraint]
357
-
Primitive representations can only be applied to enumerations and have different behavior whether the enum has fields or no fields. It is an error for [zero-variant enums] to have a primitive representation. Combining two primitive representations together is an error.
426
+
r[layout.repr.primitive.enum-only]
427
+
Primitive representations can only be applied to enumerations and have different behavior whether the enum has fields or no fields.
428
+
429
+
r[layout.repr.primitive.zero-variant]
430
+
It is an error for [zero-variant enums] to have a primitive representation.
358
431
359
432
r[layout.repr.primitive.enum]
360
433
#### Primitive Representation of Field-less Enums
@@ -475,7 +548,9 @@ r[layout.repr.alignment]
475
548
### The alignment modifiers
476
549
477
550
r[layout.repr.alignment.intro]
478
-
The `align` and `packed` modifiers can be used to respectively raise or lower the alignment of `struct`s and `union`s. `packed` may also alter the padding between fields (although it will not alter the padding inside of any field). On their own, `align` and `packed` do not provide guarantees about the order of fields in the layout of a struct or the layout of an enum variant, although they may be combined with representations (such as `C`) which do provide such guarantees.
551
+
The `align` and `packed` modifiers can be used to respectively raise or lower the alignment of `struct`s and `union`s. `packed` may also alter the padding between fields (although it will not alter the padding inside of any field).
552
+
553
+
On their own, `align` and `packed` do not provide guarantees about the order of fields in the layout of a struct or the layout of an enum variant, although they may be combined with representations (such as `C`) which do provide such guarantees.
479
554
480
555
> [!EXAMPLE]
481
556
> ```rust
@@ -511,11 +586,14 @@ The alignments of each field, for the purpose of positioning fields, is the smal
The `align` and `packed` modifierscannotbeappliedonthesametypeanda `packed` typecannottransitivelycontainanother `align`edtype. `align` and `packed` mayonlybeappliedtothe [`Rust`] and [`C`] representations.
589
+
r[layout.repr.alignment.pack-transitive-aligned]
590
+
A `packed` typemaynottransitivelycontainanother `align`edtype.
0 commit comments