rust-lang / rust-lang/rust

Type inference should success with generic trait and placeholder

Open
#149,626 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-bug needs-triage
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:

  1. i32 implements Default
  2. There's at least one type (OneDefaultCtx) that implements DefaultCtx
  3. 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.