Skip to content

Specialization does not work with inference of multiple parameterized types #38167

@ipetkov

Description

@ipetkov
#![feature(specialization)]
use std::marker::PhantomData;

trait Trait {
    type A;
    type B;

    fn foo(&self, a: Self::A, b: Self::B);
}

struct Foo<A, B> {
    a: PhantomData<A>,
    b: PhantomData<B>,
}

impl<A, B> Foo<A, B> {
    fn new() -> Self {
        Foo {
            a: PhantomData,
            b: PhantomData,
        }
    }
}

impl<A, B> Trait for Foo<A, B> {
    type A = A;
    type B = B;
    default fn foo(&self, _: A, _: B) {
        println!("default impl");
    }
}

// Specialized
impl<A, B: Eq> Trait for Foo<A, B> {
    fn foo(&self, _: A, _: B) {
        println!("specialized");
    }
}

fn main() {
    let a = "a";
    let b = "b";

    let f = Foo::new(); // Need to specify concrete type here to compile
    f.foo(a, b);
}

Expected Result

Program should compile and print out "specialized"

Actual Result

rustc 1.15.0-nightly (2217bd771 2016-11-25)
error[E0282]: unable to infer enough type information about `_`
  --> <anon>:44:13
   |
44 |     let f = Foo::new(); // Need to specify concrete type here to compile
   |             ^^^^^^^^ cannot infer type for `_`
   |
   = note: type annotations or generic parameter binding required

error: aborting due to previous error

May be related to #36262

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions