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

**URL:** <https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489>\
**Category:** language design\
**Created:** [May 19, 2016, 9:05pm UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489 "2016-05-19T21:05:14Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![StefanoD](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stefanod/32/2569_2.png) [@StefanoD](https://internals.rust-lang.org/u/StefanoD)\
**Post date:** [May 21, 2016, 11:17am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/21 "2016-05-21T11:17:04Z")

</div>

- 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.

---

<div class="post-metadata">

**Author:** ![Manishearth](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/manishearth/32/50_2.png) [@Manishearth](https://internals.rust-lang.org/u/Manishearth)\
**Post date:** [May 22, 2016, 1:13am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/22 "2016-05-22T01:13:37Z")

</div>

> [@StefanoD](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![StefanoD](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stefanod/32/2569_2.png) [@StefanoD](https://internals.rust-lang.org/u/StefanoD)\
**Post date:** [May 22, 2016, 7:26am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/23 "2016-05-22T07:26:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [May 22, 2016, 8:20am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/24 "2016-05-22T08:20:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Manishearth](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/manishearth/32/50_2.png) [@Manishearth](https://internals.rust-lang.org/u/Manishearth)\
**Post date:** [May 22, 2016, 11:18am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/25 "2016-05-22T11:18:15Z")

</div>

> [@StefanoD](#):
>
> 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.

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

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

---

<div class="post-metadata">

**Author:** ![nkaretnikov](https://avatars.discourse-cdn.com/v4/letter/n/ecd19e/32.png) [@nkaretnikov](https://internals.rust-lang.org/u/nkaretnikov)\
**Post date:** [May 28, 2016, 5:56pm UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/26 "2016-05-28T17:56:53Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:26am UTC](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489/27 "2019-03-25T08:26:12Z")

</div>

Closed on March 25, 2019, 8:26am UTC

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

[Previous page](https://internals.rust-lang.org/t/make-index-out-of-bounds-a-compile-time-error/3489.md?page=1)
