Skip to content

MIR enabled rustc nightly warns about error in unreachable code #35316

Closed
@nwin

Description

@nwin

Compiling the following code using rustc+mir

fn main() {
    let a = [1];
    if 1 == 1 {
        println!("{}", a[0])
    } else {
        println!("{}", a[1])
    }
}

emits the following warning

warning: this expression will panic at run-time
 --> <anon>:6:24
  |
6 |         println!("{}", a[1])
  |                        ^^^^ index out of bounds: the len is 1 but the index is 1
<std macros>:2:27: 2:58 note: in this expansion of format_args!
<std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
<anon>:6:9: 6:29 note: in this expansion of println! (defined in <std macros>)

although the else branch is unreachable. Switching off MIR by using -Z orbit=off causes the warning to disappear.

Edit: It seems that rustc lost the ability to detect this unreachable branch. Using if true { instead causes the warning to disappear as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions