Skip to content

Commit d8ca372

Browse files
committed
Fix usize limitations
Closes #102 .
1 parent b732072 commit d8ca372

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

reference/src/layout/scalars.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@ They have the same layout as the [pointer types] for which the pointee is
4141
> `unsigned` is at least as large as a short, allowed to have padding bits, etc.
4242
> but it is not necessarily pointer-sized.
4343
44-
The layout of `usize` determines the following:
45-
46-
- the maximum size of Rust objects (`size_of` and `size_of_val` return `usize`),
47-
- the maximum number of elements in an array (`[T; N: usize]`),
48-
- how much a pointer of a certain type can be offseted (limited by `usize::max_value()`).
49-
50-
The maximum size of any single value must fit within `usize` to [ensure that
51-
pointer diff is
52-
representable](https://github.com/rust-rfcs/unsafe-code-guidelines/pull/5#discussion_r212703192).
44+
The layout of `usize` determine the following:
45+
46+
- the maximum size of Rust values is _implementation-defined_, but can at most
47+
be `usize::max_value` since `mem::size_of` and `mem::size_of_val` return
48+
`usize`,
49+
- the maximum number of elements in an array is _implementation-defined_, but
50+
can at most be `usize::max_value()` since `[T; N: usize]`,
51+
- the maximum value by which a pointer can be offseted is
52+
_implementation-defined_, but can at most be `usize::max_value()` since
53+
`ptr.add(count: usize)`.
54+
55+
> **Note**: in the current Rust implementation:
56+
>
57+
> * the maximum size of Rust values is limited to `isize::max_value()`. The LLVM
58+
> `getelementptr` instruction uses signed-integer field offsets. Rust calls
59+
> `getelementptr` with the `inbounds` flag which assumes that field offsets do
60+
> not overflow,
61+
> * the maximum number of elements in an array is `usize::max_value()`,
62+
> * the maximum value by which a pointer can be offseted is `usize::max_value()`.
5363
5464
[pointer types]: ./pointers.md
5565

@@ -74,6 +84,7 @@ the corresponding C fixed-width integer types are expected.
7484
The specification of native C integer types, `char`, `short`, `int`, `long`,
7585
... as well as their `unsigned` variants, guarantees a lower bound on their size,
7686
e.g., `short` is _at least_ 16-bit wide and _at least_ as wide as `char`.
87+
7788
Their exact sizes are _implementation-defined_.
7889

7990
Libraries like `libc` use knowledge of this _implementation-defined_ behavior on

0 commit comments

Comments
 (0)