Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nwin opened this issue Aug 4, 2016 · 4 comments
Closed

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

nwin opened this issue Aug 4, 2016 · 4 comments

Comments

@nwin
Copy link
Contributor

nwin commented Aug 4, 2016

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.

@nagisa
Copy link
Member

nagisa commented Aug 4, 2016

Constant propagation should help with this.

@eddyb
Copy link
Member

eddyb commented Aug 4, 2016

@nwin rustc never actually checked, this warning is a new MIR feature.
We do need constant propagation to make it not trigger in cases like this.

nagisa added a commit to nagisa/rust that referenced this issue Aug 18, 2016
Based on the shiny new dataflow framework, comes the constant propagation on MIR! Propagates
constants, evaluates a number of binary, unary and cast operations and simplifies CFG based on the
evaluations done.

Code comments coming in subsequent PRs.

Fixes rust-lang#35316
@Mark-Simulacrum
Copy link
Member

This is still the case today; however, I'm not sure that there's much purpose in keeping this issue around since it doesn't track anything specific other than "errors in unreachable code need to not happen," which as far as I can tell is really only fixable with constant propagation -- which this doesn't track.

@Mark-Simulacrum
Copy link
Member

I'm going to close -- there's nothing inherently wrong with warning about unreachable code, and this issue isn't going to influence constant propagation being introduced into MIR/rustc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants