Skip to content

Commit 0b85a44

Browse files
authored
Merge pull request #217 from RalfJung/uninhabited-struct
note on uninhabited structs
2 parents 49f3c12 + 2f06a6e commit 0b85a44

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ struct Foo {
7272
(In fact, one may use such field names in patterns or in accessor
7373
expressions like `foo.0`.)
7474

75-
The degrees of freedom the compiler has when computing the layout of a struct or
76-
tuple is to determine the order of the fields, and the "gaps" (often called
77-
*padding*) before, between, and after the fields. The layout of these fields
78-
themselves is already entirely determined by their types, and since we intend to
79-
allow creating references to fields (`&s.f1`), structs do not have any
80-
wiggle-room there.
75+
The degrees of freedom the compiler has when computing the layout of an
76+
*inhabited* struct or tuple is to determine the order of the fields, and the
77+
"gaps" (often called *padding*) before, between, and after the fields. The
78+
layout of these fields themselves is already entirely determined by their types,
79+
and since we intend to allow creating references to fields (`&s.f1`), structs do
80+
not have any wiggle-room there.
8181

8282
This can be visualized as follows:
8383
```text
@@ -90,6 +90,10 @@ layout). The compiler freely picks an order for the fields to be in (this does
9090
not have to be the order of declaration in the source), and it picks the gaps
9191
between the fields (under some constraints, such as alignment).
9292

93+
For *uninhabited* structs or tuples like `(i32, !)` that do not have a valid
94+
inhabitant, the compiler has more freedom. After all, no references to fields
95+
can ever be taken. For example, such structs might be zero-sized.
96+
9397
How exactly the compiler picks order and gaps, as well as other aspects of
9498
layout beyond size and field offset, can be controlled by a `#[repr]` attribute:
9599

0 commit comments

Comments
 (0)