Skip to content

Out-of-bound array access not reported when checking the crate #81224

Closed as not planned
@scrabsha

Description

@scrabsha

It seems that rustc is able to check for invalid array indexing at compile-time under some circumstances.

For instance, let's consider a crate, with the following code in it.

fn main() {
    let a = [42];
    // Incorrect indexing
    println!("{}", a[1]);
}

cargo run and cargo build both report the following error:

error: this operation will panic at runtime
 --> src/main.rs:4:20
  |
4 |     println!("{}", a[1]);
  |                    ^^^^ index out of bounds: the length is 1 but the index is 1
  |
  = note: `#[deny(unconditional_panic)]` on by default

However, cargo check reports nothing.

Running cargo clean before each command does not change anything.

I have set up a repository, which may help people to test it by themselves.

Meta

While I discovered this on the stable channel, nightly and beta are also affected.

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.49.0

rustc +nightly --version --verbose :

rustc 1.51.0-nightly (c5a96fb79 2021-01-19)
binary: rustc
commit-hash: c5a96fb7973649807a7943e7395456db158dcab6
commit-date: 2021-01-19
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

rustc +beta --version --verbose:

rustc 1.50.0-beta.6 (ea20aa255 2021-01-14)
binary: rustc
commit-hash: ea20aa255b54708fb6a86f3146244eb20d079513
commit-date: 2021-01-14
host: x86_64-unknown-linux-gnu
release: 1.50.0-beta.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions