rust-lang / rust-lang/rust

Type inference for `<u32>.try_into().unwrap() + <i32>` can't determine what to convert the `u32` into

Open
#149,825 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code (see also https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=209cf227d30c62d7caadc4b950f6afd3)

fn main() {
    let u: u32 = 0;
    let mut i: i32 = 0;

    i = u.try_into().unwrap() + i;
    
    println!("{}", i)
}

I expected to see this happen: u is converted to i32 as per the rest of the expression.

Instead, this happened: the type-checker couldn't determine what type to convert it into

Compiling playground v0.0.1 (/playground)
error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed
 --> src/main.rs:5:11
  |
5 |     i = u.try_into().unwrap() + i;
  |           ^^^^^^^^
  |
  = note: multiple `impl`s satisfying `_: TryFrom<u32>` found in the `core` crate:
          - impl TryFrom<u32> for NonZero<u32>;
          - impl TryFrom<u32> for char;
          - impl TryFrom<u32> for i16;
          - impl TryFrom<u32> for i32;
          - impl TryFrom<u32> for i8;
          - impl TryFrom<u32> for isize;
          - impl TryFrom<u32> for u16;
          - impl TryFrom<u32> for u8;
          - impl TryFrom<u32> for usize;
  = note: required for `u32` to implement `TryInto<_>`
help: try using a fully qualified path to specify the expected types
  |
5 -     i = u.try_into().unwrap() + i;
5 +     i = <u32 as TryInto<T>>::try_into(u).unwrap() + i;
  |
Meta

rustc --version --verbose:

Stable, 1.91.1 (Playground)

(no backtrace)

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 example from the Playground link in src/main.rs using stable Rust 1.91.1, then trace how type inference handles the TryInto call and surrounding i32 addition. Done means the compiler accepts the intended conversion or provides an agreed diagnostic, with regression coverage for this exact example.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.