Skip to content

Impls discriminating on associated types do not cause rustc consider them separate #36432

Closed
@nagisa

Description

@nagisa

Consider code like this:

impl<C> fmt::Display for Stuff<C>
where C: SomeTrait<Type=()>
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        panic!()
    }
}

impl<C> fmt::Display for Stuff<C>
where C: SomeTrait<Type=Vec<C>>
{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        panic!()
    }
}

Play here

It is obvious, that C cannot in any way implement the same trait so the associated Type is both () and Vec<C> at the same time. The compiler should be able to infer that these trait implementations are completely independent. Alas, in the current nightly you get something like

error[E0119]: conflicting implementations of trait `std::fmt::Display` for type `Stuff<_>`:
  --> <anon>:24:1
   |
15 | impl<C> ::std::fmt::Display for Stuff<C>
   | - first implementation here
...
24 | impl<C> ::std::fmt::Display for Stuff<C>
   | ^ conflicting implementation for `Stuff<_>`

instead.

cc @nikomatsakis @eddyb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions