Skip to content

Implementations conflict when using associated type across crates #51445

@bvinc

Description

@bvinc

crate bar:

pub struct Bar;
pub struct BarNext;

pub trait SomeTrait {
        type Next;
}

impl SomeTrait for Bar {
        type Next = BarNext;
}

crate foo:

extern crate bar;

struct Foo;

struct Baz;
struct BazNext;

pub trait MyTrait<T> {
}

impl MyTrait<BazNext> for Foo {
}

// Why does this conflict with the previous impl?  It should resolve to the type BarNext
impl MyTrait<<::bar::Bar as ::bar::SomeTrait>::Next> for Foo {
}

// Using the type BarNext directly does not conflict with the first impl
// impl MyTrait<::bar::BarNext> for Foo {
// }

fn main() {
    println!("Hello, world!");
}

Output:

error[E0119]: conflicting implementations of trait `MyTrait<BazNext>` for type `Foo`:
  --> src/main.rs:15:1
   |
11 | impl MyTrait<BazNext> for Foo {
   | ----------------------------- first implementation here
...
15 | impl MyTrait<<::bar::Bar as ::bar::SomeTrait>::Next> for Foo {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Foo`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.
error: Could not compile `foo`.

I can only reproduce this issue when using the <Bar as SomeTrait>::Next syntax, and only when Bar is in an external crate. Reproduced in current stable and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions