Skip to content

Commit ea5be8b

Browse files
committed
Add more todos/safety comments
1 parent 11c833e commit ea5be8b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics/validity_invariants_of.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct Invariant {
1515
end: u128,
1616
}
1717

18+
// TODO: Don't add duplicate invariants (maybe use a HashMap?)
1819
fn add_invariants<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, invs: &mut Vec<Invariant>, offset: Size) {
1920
let x = tcx.layout_of(ParamEnvAnd { param_env: ParamEnv::reveal_all(), value: ty });
2021

library/core/src/intrinsics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ pub const fn validity_invariants_of<T>() -> &'static [Invariant] {
21582158
let invariants: &'static [u8] = validity_invariants_of::<T>();
21592159
let sz = invariants.len() / core::mem::size_of::<Invariant>();
21602160

2161-
// SAFETY: we know this is valid.
2161+
// SAFETY: we know this is valid because the intrinsic promises an aligned slice.
21622162
unsafe { core::slice::from_raw_parts(invariants.as_ptr().cast(), sz) }
21632163
}
21642164

@@ -2428,17 +2428,21 @@ pub(crate) const unsafe fn assert_validity_of<T>(_: *const T) -> bool {
24282428
/// Asserts that the value at `value` is valid at type T.
24292429
/// Best effort, and is UB if the value is invalid.
24302430
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.
24312433
#[repr(packed)]
24322434
struct Unaligned<T>(T);
24332435

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.
24352438
unsafe {
24362439
let invariants = validity_invariants_of::<T>();
24372440
for invariant in invariants {
24382441
let off = invariant.offset as usize;
24392442
let start = invariant.start;
24402443
let end = invariant.end;
24412444

2445+
// TODO: Maybe replace this with an enum?
24422446
let (value, max): (u128, u128) = match invariant.size {
24432447
1 => ((*(value.cast::<u8>().add(off))).into(), u8::MAX.into()),
24442448
2 => (

0 commit comments

Comments
 (0)