Skip to content

Parameters in generic associated types do not propagate if unused #144858

@SuperSamus

Description

@SuperSamus

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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-inferenceArea: Type inferenceC-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions