Make 'index out of bounds' a compile-time error

  • This can be implemented in rust 2.0

  • If you do more embedded and algorithmic stuff, you regulary manipulate arrays.

  • A lint doesn’t make any optimization.

what do you mean by this? Where does optimization come in? When I say a lint, I just mean that the check will warn instead of halting compilation (but you can configure it to halt)

Regarding 2.0: That's really far off and I don't think its productive to discuss if this can go in 2.0 now, since we don't know what Rust will be like then.

As already stated in this thread: If you in some cases know, there aren’t OOB’s, you don’t have to do bound checks.

But LLVM can notice and optimize that just fine, especially when array length and index are both constant. LLVM already includes many effective ways to detect when a condition is always met, and unlike the subject of this thread they aren’t limited to bound checks. To improve upon those analyses and be able to give useful hints to the optimizer, you would need some really powerful static analysis tools that reason across functions and about non-constant values and can see through complicated index calculations.

That has nothing to do with the proposal. This optimization already happens.

If this error were a lint instead literally nothing else would change.

Someone mentioned dependent types… Just leave this here, so I won’t forget: I wonder whether something like Cryptol’s size-polymorphic dependent type system would make sense for Rust or not. Cryptol has type inference. You can learn more at http://cryptol.net/files/ProgrammingCryptol.pdf.

I guess that it’s pointless to discuss the topic here any further. A detailed RFC could be helpful, though, but I don’t have time to write one at the moment, so maybe later.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.