Specialization and partially overlapping impls

Consider this code:

#![feature(specialization)]

trait Foo {
    fn bar();
}

trait A {}
trait B {}

impl<T: A> Foo for T {
    fn bar() { println!("A") }
}
impl<T: B> Foo for T {
    fn bar() { println!("B") }
}

impl<T: A + B> Foo for T {
    fn bar() { println!("A + B") }
}

Currently this doesn't build on nightly (nor in the chalk repl) because the impls for T: A and T: B overlaps, however the overlapping part is specialized in another impl, so this shouldn't be ambiguous. Unfortunately the specialization RFC doesn't seem to cover this case. Is there a workaround for this or should I propose in IRLO to expand the RFC to cover cases like this?

1 Like

The is the so called lattice rule that was discussed in the specialization RFC. It's not implemented and there are no plans to implement it. You can't easily work around this. The best you can do is Rust Playground. But this doesn't statically reject if neither A or B are implemented.

2 Likes

Somehow I missed that. Too bad there's no plan to implement it.

It's also impossible to use for traits with associated types with bounds.

It's a long discussion, I can't blame anyone for missing any part of it. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.