`Option::map` with `NonZero::min` turns `None` into `Some`
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
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 内容生成。
描述
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rust-lang/rust 的其他 Issue
-
C-bug needs-triage
难度 2/5 1-3 小时 新手友好度 88/100
rust-lang/rust#166722 · 1 个 reaction ·
维护者通常 1 天内回复
-
A-attributes relnotes relnotes-needs-review relnotes-tracking-issue T-compiler T-lang
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复
-
A-run-make C-bug T-bootstrap
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
relnotes relnotes-tracking-issue T-libs
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复
-
A-str A-Unicode relnotes relnotes-tracking-issue T-libs
难度 1/5 1 小时以内 新手友好度 70/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
tamatebako/tebako#1155 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 79/100
swc-project/swc-next-issues#1148 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
registrystack/registry-stack#2828 ·
维护者通常 1 天内回复
-
agent:triaged arch-audit bug priority:p1
难度 2/5 1-3 小时 新手友好度 68/100
SocketDev/socket-patch#2364 · 1 条评论 ·
维护者通常 1 天内回复
-
bug P2
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复