rust-lang / rust-lang/rust

"type annotations needed" on associated type with type parameter, even when said parameter is already known

Open
#145,609 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

trait Foo<T> {
    type Bar<_T>;

    fn bar() -> Self::Bar<T>;
}

impl<T> Foo<T> for usize {
    type Bar<_T> = u32;

    fn bar() -> Self::Bar<T> {
        Self::Bar::default()
    }
}

I expected to see this happen: The code would compile, since _T is known to be T in <usize as Foo<T>>::bar

Instead, this happened: The _T parameter is not inferred, and the code fails to compile with the following message:

error[E0282]: type annotations needed
  --> src/main.rs:13:9
   |
13 |         Self::Bar::default()
   |         ^^^^^^^^^ cannot infer type for type parameter `_T` declared on the associated type `Bar`

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

rustc --version --verbose:

rustc 1.91.0-nightly (425a9c0a0 2025-08-17)
binary: rustc
commit-hash: 425a9c0a0e365c0b8c6cfd00c2ded83a73bed9a0
commit-date: 2025-08-17
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

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 in src/main.rs with the reported rustc 1.91.0-nightly version, and review rustc --explain E0282 for the diagnostic context. Trace how the compiler handles the associated type Bar and its known parameter in the impl. Done means the example compiles without the type-annotation error and the behavior is covered by an appropriate regression test.

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.