Skip to content

EarlyOtherwiseBranch introduces access to a dead local variable #159618

Description

@tmiasko
#![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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions