-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-GATsArea: Generic associated types (GATs)Area: Generic associated types (GATs)A-inferenceArea: Type inferenceArea: Type inferenceC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.
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`.
Metadata
Metadata
Assignees
Labels
A-GATsArea: Generic associated types (GATs)Area: Generic associated types (GATs)A-inferenceArea: Type inferenceArea: Type inferenceC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.