Skip to content

Commit b0d7414

Browse files
authoredJul 24, 2024
Rollup merge of rust-lang#126152 - RalfJung:size_of_val_raw, r=saethlin
size_of_val_raw: for length 0 this is safe to call For motivation, see rust-lang/unsafe-code-guidelines#465, specifically around [here](rust-lang/unsafe-code-guidelines#465 (comment)). Cc `@rust-lang/opsem`
2 parents 6242470 + a7bec56 commit b0d7414

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
 

‎core/src/alloc/layout.rs

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ impl Layout {
183183
/// - a [slice], then the length of the slice tail must be an initialized
184184
/// integer, and the size of the *entire value*
185185
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
186+
/// For the special case where the dynamic tail length is 0, this function
187+
/// is safe to call.
186188
/// - a [trait object], then the vtable part of the pointer must point
187189
/// to a valid vtable for the type `T` acquired by an unsizing coercion,
188190
/// and the size of the *entire value*

‎core/src/mem/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
359359
/// - a [slice], then the length of the slice tail must be an initialized
360360
/// integer, and the size of the *entire value*
361361
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
362+
/// For the special case where the dynamic tail length is 0, this function
363+
/// is safe to call.
364+
// NOTE: the reason this is safe is that if an overflow were to occur already with size 0,
365+
// then we would stop compilation as even the "statically known" part of the type would
366+
// already be too big (or the call may be in dead code and optimized away, but then it
367+
// doesn't matter).
362368
/// - a [trait object], then the vtable part of the pointer must point
363369
/// to a valid vtable acquired by an unsizing coercion, and the size
364370
/// of the *entire value* (dynamic tail length + statically sized prefix)
@@ -506,6 +512,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
506512
/// - a [slice], then the length of the slice tail must be an initialized
507513
/// integer, and the size of the *entire value*
508514
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
515+
/// For the special case where the dynamic tail length is 0, this function
516+
/// is safe to call.
509517
/// - a [trait object], then the vtable part of the pointer must point
510518
/// to a valid vtable acquired by an unsizing coercion, and the size
511519
/// of the *entire value* (dynamic tail length + statically sized prefix)

0 commit comments

Comments
 (0)
Please sign in to comment.