Pointers Are Complicated II, or: We need better language specs

Which is not a problem in and by itself though, right? As long as no other optimizations that consider pointers to have provinence are carried out after the "x-x" is replaced by "0". Anyways, this replacing with 0 infact doesn’t seem to be restricted to late optimization passes only:

pub unsafe fn foo(x: *const i8) -> i8 {
    *x.wrapping_sub(x as _).wrapping_add(x as _)
}

fn main() {
    let x = 42;
    println!("{}", unsafe {foo(&x)});
}

Which should be safe (right?? I’m not actually 100% sure; the docs of those wrapping operations are a bit ambiguous about whether the pointer value is allowed to leave the object boundaries and go back into bound afterwards) only works on Debug (you get illegal instructions on Release build).

1 Like