rust-lang / rust-lang/rust

rustc is unable to evaluate a singular conditional pathway predicated on the length of an array literal

Open
#119,908 3 comments 0 reactions 1 assignee View on GitHub

@barabadzhi is already working on this.

Since Jan 13, 2024.

A-const-prop A-lints C-bug L-unconditional_panic T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

rustc is unable to evaluate a singular conditional pathway predicated on the length of an array literal.

This behavior prevents an obviously correct code to compile
(conditional should be eliminated altogether, instead of throwing a compile error in this case):

fn main() {
    let x: [i32; 0] = [];
    if !x.is_empty() {
        x[0];
    }
}

thanks @compiler-errors 👍🏼

Reproducible example playground.

Meta

rustc --version --verbose:

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6

(stable, beta & nightly are all affected seemingly independent of the platform or edition, incl. 2024)

Error output
Compiling playground v0.0.1 (/playground)
error: this operation will panic at runtime
 --> src/main.rs:4:9
  |
4 |         x[0];
  |         ^^^^ index out of bounds: the length is 0 but the index is 0
  |
  = note: `#[deny(unconditional_panic)]` on by default

error: could not compile `playground` (bin "playground") due to previous error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.