rust-lang / rust-lang/rust

bad type inference for `NonZero::{MIN,MAX}`

Open
#143,758 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::num::NonZero;

fn main() {
    let x: NonZero<u8> = NonZero::MAX;
}

I expected to see this happen: NonZero::MAX is inferred to be NonZero::<u8>::MAX

Instead, this happened: compilation error

Meta

rustc --version --verbose:

cargo 1.88.0 (873a06493 2025-05-10)
release: 1.88.0
commit-hash: 873a0649350c486caf67be772828a4f36bb4734c
commit-date: 2025-05-10
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: NixOS 25.11.0 [64-bit]

(also tested on latest beta and nightly, using Playground)

Backtrace

error[E0034]: multiple applicable items in scope
 --> src/lib.rs:4:35
  |
4 |     let x: NonZero<u8> = NonZero::MAX;
  |                                   ^^^ multiple `MAX` found
  |
  = note: candidate #1 is defined in an impl for the type `NonZero<i128>`
  = note: candidate #2 is defined in an impl for the type `NonZero<i16>`
  = note: candidate #3 is defined in an impl for the type `NonZero<i32>`
  = note: candidate #4 is defined in an impl for the type `NonZero<i64>`
  = note: and 8 others

For more information about this error, try `rustc --explain E0034`.

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

Start by reproducing the example in src/lib.rs with rustc and compare the behavior of NonZero::MAX and NonZero::MIN across the reported toolchains. Trace how the compiler resolves these associated constants and verify that the annotated NonZero binding compiles without the multiple-applicable-items error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.