Skip to content

"Unreachable definition" warning after infallible expression in if-block #111370

Open
@fnetz

Description

@fnetz

The following snippet generates an "unreachable definition" warning at let _x = 1 (godbolt at 1.69):

fn infallible() -> std::convert::Infallible {
    loop {}
}

fn main() {
    if false {
        infallible();
    }

    let _x = 1;
}

Full output:

warning: unreachable definition
  --> <source>:10:9
   |
7  |         infallible();
   |         ------------ any code following this expression is unreachable
...
10 |     let _x = 123;
   |         ^^ unreachable definition
   |
note: this expression has type `Infallible`, which is uninhabited
  --> <source>:7:9
   |
7  |         infallible();
   |         ^^^^^^^^^^^^
   = note: `#[warn(unreachable_code)]` on by default

Runtime behaviour is as expected (it exits), so this warning seems wrong.

This occurs in rustc 1.71.0-nightly (c4190f2 2023-05-07) all the way back to rustc 1.56.

A similar issue (with an expression instead of a definition) has been brought up in #89779. However, that specific case disappeared in rustc 1.60.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-control-flowArea: Control flowA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unreachable_codeLint: unreachable_codeT-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