@@ -2158,7 +2158,7 @@ pub const fn validity_invariants_of<T>() -> &'static [Invariant] {
2158
2158
let invariants: & ' static [ u8 ] = validity_invariants_of :: < T > ( ) ;
2159
2159
let sz = invariants. len ( ) / core:: mem:: size_of :: < Invariant > ( ) ;
2160
2160
2161
- // SAFETY: we know this is valid.
2161
+ // SAFETY: we know this is valid because the intrinsic promises an aligned slice .
2162
2162
unsafe { core:: slice:: from_raw_parts ( invariants. as_ptr ( ) . cast ( ) , sz) }
2163
2163
}
2164
2164
@@ -2428,17 +2428,21 @@ pub(crate) const unsafe fn assert_validity_of<T>(_: *const T) -> bool {
2428
2428
/// Asserts that the value at `value` is valid at type T.
2429
2429
/// Best effort, and is UB if the value is invalid.
2430
2430
pub ( crate ) unsafe fn assert_validity_of < T > ( value : * const T ) -> bool {
2431
+ // We have to do this, since we call assert_validity_of inside MaybeUninit::assume_init
2432
+ // and if we had used ptr::read_unaligned, that would be a recursive call.
2431
2433
#[ repr( packed) ]
2432
2434
struct Unaligned < T > ( T ) ;
2433
2435
2434
- // SAFETY:
2436
+ // SAFETY: The pointer dereferences here are valid if `value` is valid.
2437
+ // though TODO: introduce a new size for "pointer", since reading a pointer as an int *is* UB.
2435
2438
unsafe {
2436
2439
let invariants = validity_invariants_of :: < T > ( ) ;
2437
2440
for invariant in invariants {
2438
2441
let off = invariant. offset as usize ;
2439
2442
let start = invariant. start ;
2440
2443
let end = invariant. end ;
2441
2444
2445
+ // TODO: Maybe replace this with an enum?
2442
2446
let ( value, max) : ( u128 , u128 ) = match invariant. size {
2443
2447
1 => ( ( * ( value. cast :: < u8 > ( ) . add ( off) ) ) . into ( ) , u8:: MAX . into ( ) ) ,
2444
2448
2 => (
0 commit comments