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

Rust detects a conflict between non-conflicting implementations of a trait #92894

Closed
p-avital opened this issue Jan 14, 2022 · 8 comments
Closed
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@p-avital
Copy link

p-avital commented Jan 14, 2022

Hello,

When trying to do some type-fu in a much larger project, I stumbled on a conflict detection which shouldn't happen. Here's a minimal version of the issue:

struct A;
struct B;
trait Specializer {
    type Key;
}
trait Conflict {
    fn conflict(&self);
}
struct C<T>(T);
// A first implementation, note that `A` is a concrete type.
impl<T: Specializer<Key=A>> Conflict for C<T> {
    fn conflict(&self) {}
}
// A second implementation, note that `B` is also a concrete type.
// Since `T` can only impl `Specializer` once, no type can impl both `Specializer<Key=B>` and `Specializer<Key=A>`
impl<T: Specializer<Key=B>> Conflict for C<T> {
    fn conflict(&self) {}
}

Rust responds with

error[E0119]: conflicting implementations of trait `Conflict` for type `C<_>`

but the implementations don't have any intersection, and shouldn't conflict.

You can also check it out on this playground, where you can see that the conflict is also mis-detected on nightly.

@p-avital p-avital added the C-bug Category: This is a bug. label Jan 14, 2022
@Patrick-Poitras
Copy link
Contributor

@rustbot label +A-impl-trait +T-compiler

@rustbot rustbot added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 16, 2022
@p-avital
Copy link
Author

Hello,

Since I haven't received much feedback for this issue, I'm just trying to prop it up a little.

Does anyone have an idea where I could try to hunt for this bug?

Thank you :)

@Patrick-Poitras
Copy link
Contributor

Patrick-Poitras commented Jan 19, 2022

Potential duplicate of #20400?

May be related to #31844

@p-avital
Copy link
Author

It does seem to be a duplicate of #20400 ... So I guess no hope of that being fixed in stable anytime soon :(

I was indeed hoping this trick would let me achieve (very) limited specialization, hence the naming in the minimal example

@Ten0
Copy link

Ten0 commented Jan 26, 2022

I was indeed hoping this trick would let me achieve (very) limited specialization, hence the naming in the minimal example

You have this trick that enables to work around this issue: https://stackoverflow.com/questions/40392524/conflicting-trait-implementations-even-though-associated-types-differ/40408431#40408431

I think this particular issue can be closed as duplicate.

@cdecompilador
Copy link

And how would be the workaround for a different trait, be it AsRef? Where for example you want to implement Foo for AsRef<str> and AsRef<i32>?

@Ten0
Copy link

Ten0 commented Feb 1, 2022

And how would be the workaround for a different trait, be it AsRef? Where for example you want to implement Foo for AsRef<str> and AsRef<i32>?

The conflicting impl error is legitimate in this case: a struct could impl both AsRef<str> and AsRef<i32>. (In other examples of this thread, associated types actually guarantee these don't really conflict).

@Dylan-DPC
Copy link
Member

Closing this as duplicate of #20400, and there is a valid case where the implements conflict.

@Dylan-DPC Dylan-DPC closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. 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

6 participants