rust-lang / rust-lang/rust

Parameters in generic associated types do not propagate if unused

Open
#144,858 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs A-inference C-discussion
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

trait Cooker {
    type Cooked<T>;
    fn cook<T>(&self, ingredients: T) -> Self::Cooked<T>;
    fn eat<T>(&self, meal: Self::Cooked<T>);
}

struct MeaningCook;

impl Cooker for MeaningCook {
    type Cooked<T> = u32;
    fn cook<T>(&self, _ingredients: T) -> Self::Cooked<T> {
        42
    }
    fn eat<T>(&self, _meal: Self::Cooked<T>) {
        todo!()
    }
}

fn cook_and_eat_generic<C: Cooker, T>(cooker: C, ingredients: T) {
    let meal = cooker.cook(ingredients);
    cooker.eat(meal); // Compiles
}

fn cook_and_eat_meaning<T>(cooker: MeaningCook, ingredients: T) {
    let meal = cooker.cook(ingredients);
    cooker.eat(meal); // Doesn't compile
}

I expected to see this happen: The code compiles

Instead, this happened: The non-generic function fails to compile, due to the unused generic parameter of the associated type. The only workaround that works is typing cooker.eat::<()>(meal); (or whatever other type).

Meta

rustc --version --verbose:

rustc 1.90.0-nightly (ba7e63b63 2025-07-29)
binary: rustc
commit-hash: ba7e63b63871a429533c189adbfb1d9a6337e000
commit-date: 2025-07-29
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8
Backtrace

error[E0282]: type annotations needed
  --> src/main.rs:28:12
   |
28 |     cooker.eat(meal); // Doesn't compile
   |            ^^^ cannot infer type of the type parameter `T` declared on the method `eat`
   |
help: consider specifying the generic argument
   |
28 |     cooker.eat::<T>(meal); // Doesn't compile
   |               +++++

For more information about this error, try `rustc --explain E0282`.

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 error from the example in src/main.rs, focusing on the generic associated type and the call to eat that produces E0282. Trace rustc's type inference for the unused associated-type parameter; done means the non-generic example compiles without explicitly specifying eat's generic argument.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.