rust-lang / rust-lang/rust

`unreachable_code` detection and/or lint seems broken

Open
#126,808 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints C-bug L-unreachable_code T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:
playground

fn foo(s:&str) {
    println!("{}",s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    { // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }
    //No warnings if you comment these out:
    #[allow(unreachable_code)]
    foo("Hello, world!");
}

I expected to see this happen: no warnings

Instead, this happened: seeing unreachable code warning while the lint to allow it implies that it shouldn't warn!

 Compiling playground v0.0.1 (/playground)
warning: unreachable statement
  --> src/main.rs:7:5
   |
5  |       panic!("foo");
   |       ------------- any code following this expression is unreachable
6  |       #[allow(unreachable_code)]
7  | /     { // Warning points to this block
8  | |         foo("oh hi");
9  | |         foo("Hello, world!");
10 | |     }
   | |_____^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s

Now look what happens if nothing else follows:
playground

fn foo(s:&str) {
    println!("{}",s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    { // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }
    //No warnings if you comment these out:
    //#[allow(unreachable_code)]
    //foo("Hello, world!");
}

output:

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
     Running `target/debug/playground`

that's right, no warnings!
But since the warning was for the block above, why would changing the next statements affect it?!

Am I missing something? or something seems very broken.

If I put a comma after the block, same issue:
playground

fn foo(s: &str) {
    println!("{}", s);
}
fn main() {
    panic!("foo");
    #[allow(unreachable_code)]
    {
        // Warning points to this block
        foo("oh hi");
        foo("Hello, world!");
    }; //remove this comma to fix it
}

output:

Compiling playground v0.0.1 (/playground)
warning: unreachable statement
  --> src/main.rs:7:5
   |
5  |       panic!("foo");
   |       ------------- any code following this expression is unreachable
6  |       #[allow(unreachable_code)]
7  | /     {
8  | |         // Warning points to this block
9  | |         foo("oh hi");
10 | |         foo("Hello, world!");
11 | |     }; //remove this comma to fix it
   | |______^ unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.62s

Remove the comma and no warnings again. But maybe this kinda makes sense with the comma, unclear.

Meta

rustc --version --verbose:
lastest nightly or stable on playground, or even rustc 1.78.0-nightly (9b00956e5 2024-04-29) (gentoo), or latest nightly on gentoo:

$ rustv
rustc 1.81.0-nightly (684b3553f 2024-06-20)
binary: rustc
commit-hash: 684b3553f70148ded97a80371c2405984d4f6aa7
commit-date: 2024-06-20
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
cargo 1.81.0-nightly (3ed207e41 2024-06-18)
release: 1.81.0-nightly
commit-hash: 3ed207e416fb2f678a40cc79c02dcf4f936a21ce
commit-date: 2024-06-18
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Gentoo Linux 2.15.0 [64-bit]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three Rust Playground examples with the reported stable and nightly versions, comparing the block with and without a following statement or semicolon. Trace the compiler's unreachable_code lint handling for attributed blocks and expression statements; done means the allow attribute consistently suppresses the warning without changing unrelated unreachable-code diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.