Skip to content

Type inference should success with generic trait and placeholder #149626

@mu001999

Description

@mu001999

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions