Why this specialization doesn't compile?

Hi,
I don't get why the compiler cannot compile that code.
I read https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md, especially
https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md#the-algorithmic-version for trait1 and trait2, but I am still confused.

Shouldn't negative bounds help to distinguish the impls? and make sure no overlaps occur?

I tried to throw everything I could.

Note: that code is just for playing around.

#![feature(min_specialization)]
#![feature(rustc_attrs)]
#![feature(negative_bounds)]

struct A(u32);

#[rustc_specialization_trait]
trait Trait1 {}

#[rustc_specialization_trait]
trait Trait2 {}

impl<T> From<T> for A
where
    T: Trait2 + !Trait1,
{
    fn from(item: T) -> Self {
        A(3)
    }
}

impl<T> From<T> for A
where
    T: Trait1 + !Trait2,
{
    fn from(item: T) -> Self {
        A(2)
    }
}

(Playground)

thanks

From this comment it sounds like negative_bounds just exists for testing purposes, so you can’t expect it to have all of the effects that you might intuitively expect it to have. It’s not an unstable Rust feature; it’s not even an experimental unstable feature that isn’t properly documented (e.g. via RFC) yet, it’s also not an incomplete feature that’s still requiring some work but is supposed to be finished eventually.

2 Likes

AFAICT, there's no actual specialization going on here. You've marked the traits as candidates for it, but not actually utilized it (while the RFC doesn't seem to mention it, min_spec uses default sprinkled around to mark overrideable items). I'm not super familiar with specialization, but I've never seen it used without that.

1 Like

Ok guys, so right now, as I understand, there is no way to make it work.

it's not a problem, I am just playing with the language.

thanks