rust-lang / rust-lang/rust

Type inference fails when using associated type in traits

Open
#138,057 1 comment 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

Hello
I have noticed type inference fails in a case that seem rather obvious to solve.
Here is a link to a minimal reproduction project: https://github.com/StealthyKamereon/repro_bug_try_into/tree/master

The failure occurred when matching on the function TryInto::try_into: the inference is able to resolve the type of the Ok variant but not the Err one.

I tried this code:

fn repro() -> A {
    let b = B;
    // let a: A = match b.try_into() { // Compiles just fine
    let a = match b.try_into() { // Doesn't compile
        Ok(a) => a,
        Err(e) => { // e is unknown
            println!("Error converting: {:?}", e.to_string()); // e.to_string fails
            return A;
        }
    };
    a
}

struct A;

struct B;

#[derive(Debug)]
struct ConversionError;

impl Display for ConversionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error")
    }
}

impl TryFrom<B> for A {
    type Error = ConversionError;
    fn try_from(_value: B) -> Result<Self, Self::Error> {
        Ok(A)
    }
}

I expected to see this happen: The compiler should be able to know that e is ConversionError because a is bound to A by the function signature.

Instead, this happened: The compiler cannot resolve the type of e.

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-pc-windows-msvc
release: 1.85.0
LLVM version: 19.1.7
Backtrace

error[E0282]: type annotations needed
  --> src\main.rs:14:48
   |
14 |             println!("Error converting: {:?}", e.to_string());
   |                                                ^ cannot infer type

For more information about this error, try `rustc --explain E0282`.
error: could not compile `repro_bug_try_into` (bin "repro_bug_try_into") due to 1 previous error

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 with the minimal reproduction project linked in the issue, especially src/main.rs, and run cargo build to reproduce E0282 at e.to_string(). Trace the compiler's handling of TryInto, the associated Error type, and match inference. Done means this reproduction infers the error type without an explicit result annotation and passes its existing build.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.