I feel like this borders what an unsafe
attribute should be. The programmer 'knows' that the panic path is dead code. The compiler optimization proves it but just like any other optimization that is irrelevant to the typing of the program and it would be really surprising to me if this out of all properties gets to have feedback from the optimizer stage. Really we must prove it to the type system and this operation is an axiomatic assertion about possible runtime behaviors, i.e. unsafe { … }
.
Incidentally that is quite a prospect in of itself. For a pure
function, calls to impure functions are unsafe. That is, not all invocations of impure functions are actually impure yet the pure caller must prove that their invocations are. Maybe this helps with rolling it such a feature out, due to having an escape hatch? Instead of being fully blocked on upstream decisions about purity we are merely inconvenienced.
#[pure]
fn this_is_pure(a: u32, b: u32) -> u32 {
let div = 33 - b.leading_zeros();
// Uh oh, a / div is not statically pure!
// Safety: panic path is not reachable, div > 0
unsafe { a / div }
}
Of course, addition panicking in some profiles will be quite the pitfall if this pattern were used in practice.
But well, this strays a bit far from the point. Really removing panic
needs a soudness assertion as reaching the hook is straight up UB. Reordering the evaluations of a safe function shouldn't in the common case, except it could be a precondition for a related unsafe
block where it remains to figure out how to define the scope of reordering such that other crate's soundness properties can not be violated.