Skip to content

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

@AhoyISki

Description

@AhoyISki

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceA-trait-systemArea: Trait systemC-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