Skip to content
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

Unreachable code warning even when no code follows #89779

Closed
tamird opened this issue Oct 11, 2021 · 5 comments
Closed

Unreachable code warning even when no code follows #89779

tamird opened this issue Oct 11, 2021 · 5 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tamird
Copy link
Contributor

tamird commented Oct 11, 2021

#85556 introduced an unreachable code warning after uninhabited expressions; unfortunately that warning fires even when no code follows the expression.

This makes it impossible to use patterns such as:

fn foo() -> Result<std::convert::Infallible, u64> {
    Err(5)
}

fn main() { 
    match foo().expect("should") {}
}

where the match is being used in the manner suggested by @nikomatsakis in a blog post.

@tamird tamird added the C-bug Category: This is a bug. label Oct 11, 2021
@tamird tamird changed the title Unreachable code warning er Unreachable code warning even when no code follows Oct 11, 2021
@Mark-Simulacrum
Copy link
Member

The match expression in this case I think is the one being linted against, and it is indeed dead in some sense. We might want to special case empty matches as sort of a "is uninhabited" assertion in code, I suppose...

@darakshan
Copy link

darakshan commented Oct 23, 2021

Here's a simple case that doesn't involve match. And code does follow

fn main() {
    if false {
        stop().unwrap();
    }
    println!("Hello, world!");
}

fn stop() -> Result<std::convert::Infallible, u64> {
    Err(5)
}

The compiler warns that the println!() statement is unreachable, but the program works and prints as expected. I'm using stable-aarch64-apple-darwin rustc 1.56.0 (09c42c4 2021-10-18). The code that uncovered this contains a call to nix::unistd::execv.

@Mark-Simulacrum
Copy link
Member

Yes, that seems like a genuine bug. Thanks!

My guess is we're seeing that the println! is unreachable if the code path goes through the if expression, but of course, that can't actually happen here.

cc @FabianWolff

@a-ba
Copy link

a-ba commented Nov 4, 2021

I have a case where all branches are reachable (appeared with rust 1.56)

enum Never {}

fn terminate() -> Never
{
    std::process::exit(1);
}

fn main() {
    if std::env::args().count() > 1 {
        println!("terminate");
        terminate();
    }
    println!("Hello, world!");
}
/src/tmp/unreachable$ cargo run 
warning: unreachable expression
  --> src/main.rs:13:5
   |
11 |         terminate();
   |         ----------- any code following this expression is unreachable
12 |     }
13 |     println!("Hello, world!");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable expression
   |
   = note: `#[warn(unreachable_code)]` on by default
note: this expression has type `Never`, which is uninhabited
  --> src/main.rs:11:9
   |
11 |         terminate();
   |         ^^^^^^^^^^^
   = note: this warning originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `unreachable` (bin "unreachable") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/unreachable`
Hello, world!

@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage-legacy labels Jan 24, 2024
@fmease
Copy link
Member

fmease commented Jan 24, 2024

#89779 (comment) and #89779 (comment) no longer trigger unreachable_code. Closing as fixed then (assuming we rule the reproducer from the issue description as wont-fix). Please comment if I should reopen this issue.

@fmease fmease closed this as completed Jan 24, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants