Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false positive "trait already implemented" (E0371) #68636

Open
kdy1 opened this issue Jan 29, 2020 · 2 comments
Open

false positive "trait already implemented" (E0371) #68636

kdy1 opened this issue Jan 29, 2020 · 2 comments
Labels
A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kdy1
Copy link
Contributor

kdy1 commented Jan 29, 2020

While investigating #68564, I found a real bug.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c959b9e4ee176c4430586425307787ec

pub trait Fold<T: ?Sized> {
    fn fold(&mut self, node: T) -> T;
}

struct A;
struct B;

impl Fold<A> for dyn Fold<B> {}

It results in an error even on stable.

Fold<A> and Fold<B> is treated as a separate trait (#68564) while resolving, but treated as a same trait while verifying impl blocks.

   Compiling playground v0.0.1 (/playground)
error[E0371]: the object type `(dyn Fold<B> + 'static)` automatically implements the trait `Fold`
  --> src/lib.rs:11:1
   |
11 | impl Fold<A> for dyn Fold<B> {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Fold<B> + 'static)` automatically implements trait `Fold`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0371`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.
kdy1 added a commit to kdy1/swc that referenced this issue Jan 29, 2020
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 29, 2020
@kdy1
Copy link
Contributor Author

kdy1 commented Jan 29, 2020

While chatting on rust discord, someone suggested that it conflicts if a type implements Fold<A> and Fold<B>. I'm opening #68564 again and I'll explain more on the issue.

@sffc
Copy link

sffc commented Mar 22, 2021

I'm having this issue. My workaround is to make an intermediate trait, like this:

// Same functions as Fold<B>
trait FoldB {
    fn fold(&mut self, node: B) -> B;
}

// Auto-implement FoldB for Fold<B>
impl<T> FoldB for T where T: Fold<B> {
    fn fold(&mut self, node: B) -> B {
        Fold::<B>::fold(self, node)
    }
}

impl Fold<A> for dyn FoldB {
    // works!
}

Updated playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3ad915417abd9945d362ee6f04475a79

@Enselic Enselic changed the title false positive E037 false positive "trait already implemented" (E0371) Dec 17, 2023
@Enselic Enselic added the A-traits Area: Trait system label Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants