Skip to content

Commit eae3f3c

Browse files
committed
Struct with no fields
1 parent bd8e2fe commit eae3f3c

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

reference/src/layout/structs-and-tuples.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ Structs can have various `#[repr]` flags that influence their layout:
8484

8585
### Default layout ("repr rust")
8686

87-
**The default layout of structs is not specified.** As of this
88-
writing, we have not reached a full consensus on what limitations
89-
should exist on possible field struct layouts, so effectively one must
90-
assume that the compiler can select any layout it likes for each
91-
struct on each compilation, and it is not required to select the same
92-
layout across two compilations. This implies that (among other things)
93-
two structs with the same field types may not be laid out in the same
94-
way (for example, the hypothetical struct representing tuples may be
95-
laid out differently from user-declared structs).
87+
With the exception of the guarantees provided below, **the default layout of
88+
structs is not specified.**
89+
90+
As of this writing, we have not reached a full consensus on what limitations
91+
should exist on possible field struct layouts, so effectively one must assume
92+
that the compiler can select any layout it likes for each struct on each
93+
compilation, and it is not required to select the same layout across two
94+
compilations. This implies that (among other things) two structs with the same
95+
field types may not be laid out in the same way (for example, the hypothetical
96+
struct representing tuples may be laid out differently from user-declared
97+
structs).
9698

9799
Known things that can influence layout (non-exhaustive):
98100

@@ -122,6 +124,26 @@ compiler will not reorder it, to allow for the possibility of
122124
unsizing. E.g., `struct Foo { x: u16, y: u32 }` and `struct Foo<T> {
123125
x: u16, y: T }` where `T = u32` are not guaranteed to be identical.
124126

127+
#### Structs with no fields
128+
129+
Structs with default layout and no fields are [1-ZST].
130+
131+
#### Structs with 1-ZST fields
132+
133+
For the purposes of struct layout [1-ZST] fields are ignored.
134+
135+
For example:
136+
137+
```rust
138+
type Zst1 = ();
139+
struct S1(i32, Zst1); // same layout as i32
140+
141+
type Zst2 = [u16; 0];
142+
struct S2(Zst2, Zst1); // same layout as Zst2
143+
144+
struct S3(Zst1); // same layout as Zst1
145+
```
146+
125147
#### Zero-sized structs
126148

127149
For `repr(Rust)`, `repr(packed(N))`, `repr(align(N))`, and `repr(C)`
@@ -141,22 +163,6 @@ struct Zst2(Zst1, Zst0);
141163
# }
142164
```
143165

144-
#### Structs with 1-ZST fields
145-
146-
For the purposes of struct layout [1-ZST] fields are ignored.
147-
148-
For example,
149-
150-
```rust
151-
type Zst1 = ();
152-
struct S1(i32, Zst1);
153-
154-
type Zst2 = [u16; 0];
155-
struct S2(Zst2, Zst1);
156-
```
157-
158-
the layout of `S1` is the same as that of `i32` and the layout of `S2` as that of `Zst2`.
159-
160166
#### Unresolved questions
161167

162168
During the course of the discussion in [#11] and [#12], various

0 commit comments

Comments
 (0)