rust-lang / rust-lang/rust

rustc since 1.36 accepts invalid code after return

Open
#130,433 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I tried this code:

fn foo() -> u8 {
    return 0;
    let a: u8 = 1;
    a = 2; // error: second write to an immutable variable
    return a;
}
fn main() -> () {
    println!("{}", foo());
}

rustc-1.35 rejects the code and says:

error[E0384]: cannot assign twice to immutable variable `a`
 --> <source>:4:5
  |
3 |     let a: u8 = 1;
  |         - first assignment to `a`
4 |     a = 2;
  |     ^^^^^ cannot assign twice to immutable variable

Current rustc accepts the code

I acknowledge that the invalid write happens in a dead region, but still report it as all rustc versions reject the following code:

fn foo() -> u8 {
    if true {
        return 0;
    }
    let a: u8 = 1;
    a = 2; // error: second write to an immutable variable
    return a;
}
fn main() -> () {
    println!("{}", foo());
}

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 compiling the two Rust reproductions with rustc 1.35 and a current compiler, comparing diagnostics for the unreachable assignment. Trace the compiler's unreachable-code and immutable-assignment checks to find where the regression was introduced. Done means the invalid write is rejected again, with a regression test covering both examples.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.