rust-lang / rust-lang/rust

LLVM loop miscompilation

Open
#153,222 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM C-bug I-miscompile I-unsound P-high T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm filing this issue on behalf of @pitust, who discovered this issue.

This is caused by https://github.com/llvm/llvm-project/issues/183906

The following code constructs a NonZero<i8> that has a value of 0 at run time when compiled in release mode, which is unsound.

use std::num::NonZero;

#[inline(never)]
pub fn func() -> NonZero<i8> {
    let mut i = -5;
    let mut j = -4;
    loop {
        let x = loop {
            if let Some(v) = NonZero::new(j) {
                break v;
            }
            j *= 3;
        };
        i += 1;
        j += 1;
        if i == 0 {
            return x;
        }
    }
}

fn main() {
    println!("{}", func());
}
Meta

Reproducible on the playground with version 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39)

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 using the provided Rust example on the playground with version 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39). Start by reading the linked LLVM issue 183906 and comparing its findings with this example; the work is complete when the release build no longer produces an invalid NonZero value.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.