Annotation to lint for when bounds checks occur?

Having run-time checks to see performance is tricky, since the runtime checks themselves will lead to different codegen as you suspect. At that point, it's likely possible that you can create your own stdlib replacement which does something like that (akin to replacing malloc in C to have some kind of counter). That being said, allocation is something I can easily reason about without having to dive into asm. If I touch a vec or map of some kind, I generally hoist it above hot loops since I know it will be slow. External tooling, such as valgrind/memcheck can also check some parts of allocations. What you're proposing I think can generally live externally.

Well, I'm primarily using vanilla vim... so it's a bit annoying. My main problem is that I want to prove to myself that code has optimized a certain way, without having to second-guess if I need to check the asm.

Does it emit this because of compatibility with the C abstract machine and some ordering requirement (I'm imagining some case where indexing an array has side-effects which can't be ignored)? If you instead hoisted the line a[2] above the other 2 indexing ops, would it emit 1 check? For cases where I am doing something like this in a nested loop indexing one vector into another, I will sometimes add an assert that all values in a vector are less than the length of the other vector, but I can't know for certain if the compiler uses that information to optimize out the bounds check.

After more time spent thinking, I feel like run-time restricted value/pattern types (i.e all values in forall (a: usize) in (A: Vec<usize>), a < B.len(), B must not be mut) could solve my specific problem if they are guaranteed to remove bounds checks. Of course, implementation of something like that is non-trivial, and I have no idea how it would be structured or if it even is more generally useful. I also don't know if just slapping a ton of asserts in the code would/could have the same effect.