Skip to content

-Zmir-opt-level miscompiles code with panic #131195

@shao-hua-li

Description

@shao-hua-li

I tried this code:

pub fn myfunc() -> i32 {
    let mut c :i32 = 1;
    c = 1 ;
    if !c != 0 {
        return 1;
    }
    panic!("Reached end of non-void function");
}

pub fn main() {
    let e = myfunc();
    println!("e={}", e);
}

I expected to see this happen: always panic

Instead, this happened: only -Zmir-opt-level=2 panic

For mir-opt-level 0, 1 and 3, they all normally return

% rustc -Awarnings -Zmir-opt-level=2 test.rs && ./test
thread 'main' panicked at test.rs:7:5:
Reached end of non-void function
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
%
% rustc -Awarnings -Zmir-opt-level=0 test.rs && ./test
e=1

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (18b1161ec 2024-10-02)
binary: rustc
commit-hash: 18b1161ec9eeab8927f91405bca0ddf59a4a26c9
commit-date: 2024-10-02
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Metadata

Metadata

Assignees

Labels

A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical 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