Type inference should success with generic trait and placeholder
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Here's an example (play):
trait Tr<Ctx> {
fn foo();
}
trait DefaultCtx {}
impl<T: Default, Ctx: DefaultCtx> Tr<Ctx> for T {
fn foo() {}
}
struct OneDefaultCtx;
impl DefaultCtx for OneDefaultCtx {}
fn main() {
<i32 as Tr<_>>::foo();
}
The context type Ctx cannot be inferred even though:
- i32 implements Default
- There's at least one type (OneDefaultCtx) that implements DefaultCtx
- The trait method foo() doesn't use Ctx in its signature
Is this inability to infer Ctx an intentional design choice in Rust (perhaps due to coherence rules, ambiguity, or other language design principles), or is it a current limitation of the compiler's type inference that might be improved in the future?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Rust Playground example and reproduce the generic-trait placeholder inference behavior using the nightly 2024 edition configuration shown. Investigate whether the result follows from coherence, ambiguity, or an intentional inference rule. Done means documenting the cause and whether the compiler behavior is a limitation that can be improved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100