rust-lang / rust-lang/rust

EarlyOtherwiseBranch introduces access to a dead local variable

Open
#159,618 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt C-bug I-miscompile P-high T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

#![feature(custom_mir, core_intrinsics)]
use std::intrinsics::mir::*;
#[custom_mir(dialect = "runtime")]
fn main() {
    mir! {
        let a: u64;
        let b: u64;
        { a = 1; StorageDead(b); match a { 0 => bb1, _ => bb3 }}
        bb1 = { match b { 0 => bb1, _ => bb3 } }
        bb2 = { Goto(bb4) }
        bb3 = { Goto(bb4) }
        bb4 = { Return() }
    }
}

rustc -Zmir-opt-level=0 -Zmir-enable-passes=+EarlyOtherwiseBranch a.rs -Zunpretty=mir

fn main() -> () {
    let mut _0: ();
    let mut _1: u64;
    let mut _2: u64;
    let mut _3: bool;
    let mut _4: bool;

    bb0: {
        _1 = const 1_u64;
        StorageDead(_2);
        _3 = Ne(copy _1, copy _2); // undefined behavior: _2 is dead
        switchInt(move _3) -> [0: bb3, otherwise: bb2];
    }

    bb1: {
        _4 = Ne(copy _2, copy _2);
        switchInt(move _4) -> [0: bb4, otherwise: bb2];
    }

    bb2: {
        return;
    }

    bb3: {
        switchInt(copy _1) -> [0: bb1, otherwise: bb2];
    }

    bb4: {
        switchInt(copy _2) -> [0: bb1, otherwise: bb2];
    }
}

Related to #159591. Pending fix appears to be quite specific to that issue, and doesn't resolve the one here.

Meta

rustc --version --verbose:

rustc 1.99.0-nightly (9f36de775 2026-07-19)
binary: rustc
commit-hash: 9f36de775bc636c8e88c31a173c2bcb6995956a0
commit-date: 2026-07-19
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 22.1.8

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

Reproduce the issue with the supplied custom MIR example and rustc command, then trace the EarlyOtherwiseBranch MIR optimization pass. Add regression coverage for this case and verify that the emitted MIR no longer accesses the dead local variable.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.