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

[type-layout] Document minimum size and alignment #1482

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ as [dynamically sized types]. Since all values of a `Sized` type share the same
size and alignment, we refer to those shared values as the size of the type and
the alignment of the type respectively.

### Minimum Size and Alignment

Every type, including [dynamically sized types], has a "minimum size" and "minimum
alignment." Every value of a type must have a size at least as large as the type's
minimum size and an alignment at least as large as the type's minimum alignment.
Some notable cases of minimum size and alignment are:
- For [`Sized`] types, the type's minimum size and minimum alignment
are always equal to the type's size and alignment, respectively
- For a [slice type](#slice-layout), `[T]`, the minimum size is 0 bytes (corresponding
to a slice with 0 elements), and the minimum alignment is the alignment of `T`
- For a [trait object](#trait-object-layout), the minimum size is 0 and the minimum
alignment is 1
- For a struct type with a dynamically-sized field, the minimum size is taken to be
the size of the struct when the dynamically-sized field has *its* minimum size and
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not clear on what "it" refers to in this sentence

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the answer is value-dependent, e.g., for Foo<dyn Blah> the alignment depends on the value stored in the vtable -- put another way, it's determined by the "erased information"

Copy link
Member

@RalfJung RalfJung Jul 3, 2024

Choose a reason for hiding this comment

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

You mean "its"? It is straight-forward recursion. If a struct has field types T, U, V where V is dyn-sized, then the min size of the struct is computed using the min size and min align of V.

Copy link
Contributor

Choose a reason for hiding this comment

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

That formulation seems much clearer :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Though I still am not clear on what "the min align of V" is in the context I mentioned i.e., I would appreciate a note on whether it is value-dependent (I believe so)

Copy link
Member

Choose a reason for hiding this comment

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

The minimum alignment is not value-dependent, no. Minimum size and minimum alignment are static properties that every type possesses.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see.

its minimum alignment. The struct's minimum alignment is taken to be the alignment
of the struct when the dynamically-sized field has *its* minimum alignment. When
relying on this property of dynamically-sized structs, be careful not to assume too
much! For example, this property provides *no* guarantees about
[`repr(Rust)`](#the-rust-representation) types, as such a type can have arbitrarily
large size and alignment regardless of the sizes and alignments of its fields

Every type's minimum size is less than or equal to `isize::MAX`.

## Primitive Data Layout

The size of most primitives is given in this table.
Expand Down