Open leonardo-m opened 3 years ago
Perhaps this is the job of explicit code annotations of pre/post-conditions, loop (in)variants, etc.
The usual hack of adding pos = std::cmp::min(pos, arr.len() - 1);
eliminates the bounds check.
Do you mean code like this?
fn copy_to_front_evens2(arr: &mut [u32; 100]) {
let mut pos = 0; // Tortoise
for i in 0 .. arr.len() { // Hare
if arr[i] % 2 == 1 {
pos = pos.min(arr.len() - 1);
arr[pos] = arr[i];
pos += 1;
}
}
}
If you mean code like that, then unless I'm missing something it still contains an extra test on pos
each loop, so it isn't a significant enough improvement over the original version with a panic.
My bad, I only checked for the absence of the callq
.
@rustbot label +I-slow +A-llvm
Another common case where LLVM doesn't infer the safety of the array access:
Compiled with -O it performs a bound test in
arr[pos]
:I don't know if a back-end like LLVM is supposed to infer complex situations as this one. Perhaps this is the job of explicit code annotations of pre/post-conditions, loop (in)variants, etc.