Skip to content

EarlyOtherwiseBranch introduces access to a dead local variable #159618

Open

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

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    A-mir-optArea: MIR optimizations
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    on Jul 20, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 20, 2026
  3. scottmcm commented on Jul 20, 2026

    @scottmcm
    Member

    Hmm, reminds me of #122387 -- looks like this has had hoisting problems before

  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 21, 2026
  5. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 21, 2026
  6. rustbot commented on Jul 21, 2026

    @rustbot
    Collaborator

    Assigning P-high (discussion on Zulip).

  7. 1sgtpepper commented on Jul 26, 2026

    @1sgtpepper

    @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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