Overlapping inherent impl and associated types


#1

Hi! Currently, writing this compiles without warning:

struct Foo<T>(T);

impl Foo<f32> {
    fn doit(&self) { }
}

impl Foo<u32> {
    fn doit(&self, a: u32) { }
}

while the following triggers the overlapping_inherent_impls warning even there is no chance of overlap because T cannot implement Bar twice with different associated types:

struct Foo;

trait Bar {
    type Assoc;
}

impl<T> Foo<T> where T: Bar<Assoc = f32> {
    fn doit(&self) { }
}

impl<T> Foo<T> where T: Bar<Assoc = u32> {
    fn doit(&self, a: u32) { }
}

#22889 seems to suggest that this one of the “cases of overlap [that] really shouldn’t be happening today”, but I don’t see why. This has profound implications in some library design of mine so I’d like to know whether scenarios like the one with associated types will actually become an error in the future.


#2

Currently, these bounds aren’t considered disjoint. There’s an RFC to change that


#3

I see, thanks!

So I guess I can either use type parameters (which are not ergonomic at all in my case), or stick with associated types and hope the RFC gets accepted and implemented before the overlapping_inherent_impls warning becomes a hard error!