Skip to content

bad type inference for NonZero::{MIN,MAX} #143758

@ada4a

Description

@ada4a

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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions