@@ -414,8 +414,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
414414/// This function is only safe to call if the following conditions hold:
415415///
416416/// - If `T` is `Sized`, this function is always safe to call.
417- /// - If the unsized tail of `T` is:
418- /// - a [slice], then the length of the slice tail must be an initialized
417+ /// - If the * unsized tail* of `T` is:
418+ /// - a [slice] `[U]` , then the length of the slice tail must be an initialized
419419/// integer, and the size of the *entire value*
420420/// (dynamic tail length + statically sized prefix) must fit in `isize`.
421421/// For the special case where the dynamic tail length is 0, this function
@@ -424,15 +424,23 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
424424// then we would stop compilation as even the "statically known" part of the type would
425425// already be too big (or the call may be in dead code and optimized away, but then it
426426// doesn't matter).
427- /// - a [trait object], then the vtable part of the pointer must point
428- /// to a valid vtable acquired by an unsizing coercion , and the size
427+ /// - a [trait object] `dyn Trait` , then the vtable part of the pointer must point
428+ /// to a valid vtable for `Trait` , and the size
429429/// of the *entire value* (dynamic tail length + statically sized prefix)
430430/// must fit in `isize`.
431431/// - an (unstable) [extern type], then this function is always safe to
432432/// call, but may panic or otherwise return the wrong value, as the
433433/// extern type's layout is not known. This is the same behavior as
434434/// [`size_of_val`] on a reference to a type with an extern type tail.
435- /// - otherwise, it is conservatively not allowed to call this function.
435+ /// - No other kind of unsized tail currently exists. If more kinds of unsized tails get
436+ /// introduced in the future, the documentation of this function will have to be extended
437+ /// before it can be used for such types.
438+ ///
439+ /// Here, *unsized tail* refers to the type obtained by recursively descending through the last
440+ /// field of a tuple or struct until we arrived at a built-in unsized type.
441+ ///
442+ /// As a consequence of these rules, it is the case that whenever it is allowed to convert `val`
443+ /// into a shared reference, then it is also allowed to invoke this function.
436444///
437445/// [`size_of::<T>()`]: size_of
438446/// [trait object]: ../../book/ch17-02-trait-objects.html
@@ -441,7 +449,6 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
441449/// # Examples
442450///
443451/// ```
444- /// #![feature(layout_for_ptr)]
445452/// use std::mem;
446453///
447454/// assert_eq!(4, size_of_val(&5i32));
@@ -452,7 +459,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
452459/// ```
453460#[ inline]
454461#[ must_use]
455- #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
462+ #[ stable( feature = "layout_for_ptr" , since = "CURRENT_RUSTC_VERSION" ) ]
463+ #[ rustc_const_stable( feature = "layout_for_ptr" , since = "CURRENT_RUSTC_VERSION" ) ]
456464pub const unsafe fn size_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
457465 // SAFETY: the caller must provide a valid raw pointer
458466 unsafe { intrinsics:: size_of_val ( val) }
@@ -563,35 +571,47 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
563571///
564572/// - If `T` is `Sized`, this function is always safe to call.
565573/// - If the unsized tail of `T` is:
566- /// - a [slice], then the length of the slice tail must be an initialized
574+ /// - a [slice] `[U]` , then the length of the slice tail must be an initialized
567575/// integer, and the size of the *entire value*
568576/// (dynamic tail length + statically sized prefix) must fit in `isize`.
569577/// For the special case where the dynamic tail length is 0, this function
570578/// is safe to call.
571- /// - a [trait object], then the vtable part of the pointer must point
572- /// to a valid vtable acquired by an unsizing coercion, and the size
579+ // NOTE: the reason this is safe is that if an overflow were to occur already with size 0,
580+ // then we would stop compilation as even the "statically known" part of the type would
581+ // already be too big (or the call may be in dead code and optimized away, but then it
582+ // doesn't matter).
583+ /// - a [trait object] `dyn Trait`, then the vtable part of the pointer must point
584+ /// to a valid vtable for `Trait`, and the size
573585/// of the *entire value* (dynamic tail length + statically sized prefix)
574586/// must fit in `isize`.
575587/// - an (unstable) [extern type], then this function is always safe to
576588/// call, but may panic or otherwise return the wrong value, as the
577589/// extern type's layout is not known. This is the same behavior as
578590/// [`align_of_val`] on a reference to a type with an extern type tail.
579- /// - otherwise, it is conservatively not allowed to call this function.
591+ /// - No other kind of unsized tail currently exists. If more kinds of unsized tails get
592+ /// introduced in the future, the documentation of this function will have to be extended
593+ /// before it can be used for such types.
594+ ///
595+ /// Here, *unsized tail* refers to the type obtained by recursively descending through the last
596+ /// field of a tuple or struct until we arrived at a built-in unsized type.
597+ ///
598+ /// As a consequence of these rules, it is the case that whenever it is allowed to convert `val`
599+ /// into a shared reference, then it is also allowed to invoke this function.
580600///
581601/// [trait object]: ../../book/ch17-02-trait-objects.html
582602/// [extern type]: ../../unstable-book/language-features/extern-types.html
583603///
584604/// # Examples
585605///
586606/// ```
587- /// #![feature(layout_for_ptr)]
588607/// use std::mem;
589608///
590609/// assert_eq!(4, unsafe { mem::align_of_val_raw(&5i32) });
591610/// ```
592611#[ inline]
593612#[ must_use]
594- #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
613+ #[ stable( feature = "layout_for_ptr" , since = "CURRENT_RUSTC_VERSION" ) ]
614+ #[ rustc_const_stable( feature = "layout_for_ptr" , since = "CURRENT_RUSTC_VERSION" ) ]
595615pub const unsafe fn align_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
596616 // SAFETY: the caller must provide a valid raw pointer
597617 unsafe { intrinsics:: align_of_val ( val) }
0 commit comments