`Option::map` with `NonZero::min` turns `None` into `Some`

未关闭
#166,505 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
12/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

Start by reproducing the snippet with rustc -C opt-level=1 and -C opt-level=0 to confirm the optimization-dependent result. The report links an upstream LLVM issue (llvm-project#245656), which is where the root cause most likely lives, so read that thread first. Done would mean the assertion passes at every opt level, probably via a fix or workaround in the upstream LLVM change or in how the Rust side lowers this pattern.

由索引模型根据 Issue 内容生成。

描述

A-LLVM C-bug I-miscompile regression-from-stable-to-stable T-compiler
Code

I tried this code:

use std::num::NonZeroU64;

#[inline(never)]
fn f(a: Option<NonZeroU64>, b: NonZeroU64) -> bool {
    a.map(|a| a.min(b)).is_some()
}

fn main() {
    let none = std::hint::black_box(None);
    assert!(!f(none, NonZeroU64::MIN));
}

I expected to see this happen: This assertion should pass because the None value should not be turned into a NonZeroU64.

Instead, this happened: The assertion fails because of LLVM optimizations. Opt levels 0, 2, and 3 work while 1, s, and z are wrong.

You can see the different outcomes on 1.94, 1.95, and nightly here: https://godbolt.org/z/17nKG3bev

This is due to https://github.com/llvm/llvm-project/issues/245656

Version it worked on

It most recently worked on: 1.94.1

Version with regression

rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: aarch64-apple-darwin
release: 1.95.0
LLVM version: 22.1.2

This is still wrong on 1.100 and nightly 2026-11-23.

Backtrace
Backtrace

thread 'main' (21403425) panicked at repro.rs:10:5:
assertion failed: !f(none, NonZeroU64::MIN)
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/0ef1bc386059596dada8fb65b50425aab2ea637a/library/std/src/panicking.rs:676:5
   1: core::panicking::panic_fmt
             at /rustc/0ef1bc386059596dada8fb65b50425aab2ea637a/library/core/src/panicking.rs:80:14
   2: core::panicking::panic
             at /rustc/0ef1bc386059596dada8fb65b50425aab2ea637a/library/core/src/panicking.rs:150:5
   3: repro::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

主要语言
Rust
星标
123k
派生
16.6k
平均合并
2 天 17 小时
30 天内合并 PR
532

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rust 的其他 Issue

查看 rust-lang/rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。