Skip to content

Miscompile involving function inlining #150904

@robdockins

Description

@robdockins

Consider the following code:

fn main() {
    println!("{:?}", run_my_check(42, 1));
}

#[inline(never)]
fn run_my_check(v0: u64, v1: u64) -> Result<(), ()> {
    if do_check(v0, v1) {
        Ok(())
    } else {
        Err(())
    }
}

#[inline(always)]
fn do_check(v0: u64, v1: u64) -> bool {
    let mut ok = v0 == 42;
    ok &= v1 == 0;
    ok
}

I expected to see this happen: In all cases, when I compile and run this program, I expect it to print Err(())

Instead, this happened: With rust version 1.92, and when compiled with optimizations, it instead prints Ok(()).

% rustc --version --verbose 
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: aarch64-apple-darwin
release: 1.92.0
LLVM version: 21.1.3

% rustc main.rs && ./main   
Err(())

% rustc -O main.rs && ./main
Ok(())

This behavior is not observed with 1.91.1

% rustc --version --verbose 
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: aarch64-apple-darwin
release: 1.91.1
LLVM version: 21.1.2

% rustc main.rs && ./main   
Err(())

% rustc -O main.rs && ./main
Err(())

The behavior seems to be related to inlining somehow. Marking do_check as inline(never) resolves the issue. So does rewriting do_check to use immutable lets.

Metadata

Metadata

Assignees

Labels

A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeP-criticalCritical priorityS-has-bisectionStatus: A bisection has been found for this issueS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions