Skip to content

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

Open
@barabadzhi

Description

@barabadzhi

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

Metadata

Metadata

Assignees

Labels

A-const-propArea: Constant propagationA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unconditional_panicLint: unconditional_panicT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions