Skip to content

Support multiple blanket impls with differing bounds for traits #442

@soltanmm

Description

@soltanmm

This currently fails to compile:

trait Trait { fn asdf() {} }

trait A {}
trait B {}

impl<T> Trait for T where T: A {}
impl<T> Trait for T where T: B {}

with error:

<anon>:8:1: 8:34 error: conflicting implementations for trait `Trait` [E0119]
<anon>:8 impl<T> Trait for T where T: A {}
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:9:1: 9:34 note: note conflicting implementation here
<anon>:9 impl<T> Trait for T where T: B {}
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

Rust Playpen Link

Naturally, this error makes sense. A type can impl both traits A and B. I was hoping that such a check could be deferred, though, thus resolving these 'conflicts' later when something actually asking for Trait pops up. Ex. a type that impls only A would get the blanket impl for A, and if multiple impls apply (e.g. the type impls both A and B) then the compiler errors out.

Some ideas

It was suggested on IRC that one may allow both blanket impls to apply by using UFCS path syntax to disambiguate trait bounds and narrow down the possibly applicable impls to a single one. Ex. a type that impls both A and B could get both blanket impls but be disambiguated when calling via <Type as A>::asdf().

But really I'd be totally cool with only one blanket impl applying to any one type at any time.

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