Skip to content

Can't provide non-overlapping trait impls based on assoc type trait bound #23341

Closed
@cristicbz

Description

@cristicbz
use std::iter::IntoIterator;

trait Foo {
    fn foo(&self);
}

trait Bar {
    fn bar(&self);
}

impl<I> Foo for I
        where I: IntoIterator,
              I::Item: Bar {
    fn foo(&self) {
    }
}

impl Foo for Vec<u8> {
    fn foo(&self) {
    }
}

fn main() {}

fails with

<anon>:11:1: 16:2 error: conflicting implementations for trait `Foo` [E0119]
<anon>:11 impl<I> Foo for I
<anon>:12         where I: IntoIterator,
<anon>:13               I::Item: Bar {
<anon>:14     fn foo(&self) {
<anon>:15     }
<anon>:16 }
<anon>:18:1: 21:2 note: note conflicting implementation here
<anon>:18 impl Foo for Vec<u8> {
<anon>:19     fn foo(&self) {
<anon>:20     }
<anon>:21 }

See playpen.

Since u8 does not implement Bar, the impl-s are actually non-overlapping.

Possibly related to #20400.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions