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
If the [size is 0][zero-sized], then the pointer is trivially never "dangling" (even if it is a null pointer).
101
101
102
102
r[undefined.dangling.dynamic-size]
103
-
Note that dynamically sized types (such as slices and strings) point to their entire range, so it is important that the length metadata is never too large.
103
+
Note that dynamically sized types (such as slices and strings) point to their entire range, so it is important that the length [metadata] is never too large.
104
104
105
105
r[undefined.dangling.alloc-limit]
106
106
In particular, the dynamic size of a Rust value (as determined by `size_of_val`) must never exceed `isize::MAX`, since it is impossible for a single allocation to be larger than `isize::MAX`.
@@ -142,10 +142,10 @@ r[undefined.validity.union]
142
142
* For a `union`, the exact validity requirements are not decided yet. Obviously, all values that can be created entirely in safe code are valid. If the union has a [zero-sized] field, then every possible value is valid. Further details are [still being debated](https://github.com/rust-lang/unsafe-code-guidelines/issues/438).
143
143
144
144
r[undefined.validity.reference-box]
145
-
* A reference or [`Box<T>`] must be aligned and non-null, it cannot be [dangling], and it must point to a valid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the metadata). Note that the last point (about pointing to a valid value) remains a subject of some debate.
145
+
* A reference or [`Box<T>`] must be aligned and non-null, it cannot be [dangling], and it must point to a valid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the [metadata]). Note that the last point (about pointing to a valid value) remains a subject of some debate.
146
146
147
147
r[undefined.validity.wide]
148
-
* The metadata of a wide reference, [`Box<T>`], or raw pointer must match the type of the unsized tail:
148
+
* The [metadata] of a wide reference, [`Box<T>`], or raw pointer must match the type of the unsized tail:
149
149
*`dyn Trait` metadata must be a pointer to a compiler-generated vtable for `Trait`. (For raw pointers, this requirement remains a subject of some debate.)
150
150
* Slice (`[T]`) metadata must be a valid `usize`. Furthermore, for wide references and [`Box<T>`], slice metadata is invalid if it makes the total size of the pointed-to value bigger than `isize::MAX`.
> `rustc` achieves this with the unstable `rustc_layout_scalar_valid_range_*` attributes.
157
157
158
158
r[undefined.validity.const-provenance]
159
-
***In [const contexts]**: In addition to what is described above, further provenance-related requirements apply during const evaluation. Any value that holds pure integer data (the `i*`/`u*`/`f*` types as well as `bool` and `char`, enum discriminants, and slice metadata) must not carry any provenance. Any value that holds pointer data (references, raw pointers, function pointers, and `dyn Trait` metadata) must either carry no provenance, or all bytes must be fragments of the same original pointer value in the correct order.
159
+
***In [const contexts]**: In addition to what is described above, further provenance-related requirements apply during const evaluation. Any value that holds pure integer data (the `i*`/`u*`/`f*` types as well as `bool` and `char`, enum discriminants, and slice [metadata]) must not carry any provenance. Any value that holds pointer data (references, raw pointers, function pointers, and `dyn Trait` metadata) must either carry no provenance, or all bytes must be fragments of the same original pointer value in the correct order.
160
160
161
161
This implies that transmuting or otherwise reinterpreting a pointer (reference, raw pointer, or function pointer) into a non-pointer type (such as integers) is undefined behavior if the pointer had provenance.
Copy file name to clipboardExpand all lines: src/dynamically-sized-types.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ r[dynamic-sized.restriction]
8
8
Such types can only be used in certain cases:
9
9
10
10
r[dynamic-sized.pointer-types]
11
-
*[Pointer types] to <abbrtitle="dynamically sized types">DSTs</abbr> are sized but have twice the size of pointers to sized types
12
-
* Pointers to slices and `str`also store the number of elements.
13
-
* Pointers to trait objects also store a pointer to a vtable.
11
+
*[Pointer types] to <abbrtitle="dynamically sized types">DSTs</abbr> are sized but have twice the size of pointers to sized types, since they also store *metadata*:
12
+
* Pointers to slices store the number of elements; pointers to `str` store the length in bytes.
13
+
* Pointers to trait objects store a pointer to a vtable.
14
14
15
15
r[dynamic-sized.question-sized]
16
16
* <abbrtitle="dynamically sized types">DSTs</abbr> can be provided as type arguments to generic type parameters having the special `?Sized` bound. They can also be used for associated type definitions when the corresponding associated type declaration has a `?Sized` bound. By default, any type parameter or associated type has a `Sized` bound, unless it is relaxed using `?Sized`.
0 commit comments