Skip to content

[Nightly Regression] False positive warning with constants in conditional #47354

Closed
@leonardo-m

Description

@leonardo-m

This is modified code from a real regression in my code:

#![allow(unused_variables)]
fn main() {
    const N: usize = 1_000;
    const M: usize = 4;
    const V: [u32; M] = [1, 2, 3, 4];

    let x = if N <= M {
        V[N - 1]
    } else {
        0
    };
}
warning: this expression will panic at run-time
 --> C:\lavoro\bugs\test.rs:9:9
  |
9 |         INIT[N - 1]
  |         ^^^^^^^^^^^ index out of bounds: the len is 4 but the index is 999



rustc 1.25.0-nightly (f62f77403 2018-01-10)
binary: rustc
commit-hash: f62f774035735a06c880c48c0b9017fcc0577e33
commit-date: 2018-01-10
host: x86_64-pc-windows-gnu
release: 1.25.0-nightly
LLVM version: 4.0

D language and C++17 avoid such problems using "static if" and "if constexpr".

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-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