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

Conflicting implementation : impl<T> From<T> for T can never conflict #124913

Closed
Kayanski opened this issue May 8, 2024 · 2 comments
Closed

Conflicting implementation : impl<T> From<T> for T can never conflict #124913

Kayanski opened this issue May 8, 2024 · 2 comments
Labels
A-coherence Area: Coherence C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@Kayanski
Copy link

Kayanski commented May 8, 2024

Hello ! I found something that I think should be allowed in Rust.

I have 2 crates.

  • trait-def with a trait definition and a first implementation :
  pub trait Test {
        type Exec;
  }
  pub struct First;
  pub struct FirstArgument;

  impl Test for First {
      type Exec = FirstArgument;
  }
  • over with a second implementation of the trait for another type
  pub struct Second;
  pub struct SecondArgument;

  impl Test for Second {
      type Exec = SecondArgument;
  }

Until here, everything is alright. However, when trying to implement a simple From, things go sideways :

impl From<<First as Test>::Exec> for SecondArgument {
    fn from(value: <First as Test>::Exec) -> Self {
        Self
    }
}

It says :

error[E0119]: conflicting implementations of trait `std::convert::From<SecondArgument>` for type `SecondArgument`
   |
13 | impl From<<First as Test>::Exec> for SecondArgument {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<T> std::convert::From<T> for T;

This is a very simplified example and I can't just implement From for SecondArgument for unrelated reasons.

I know you have to be careful about implementations of From with traits, but here everything is set in stone, I'm not using generic types. One argument could be that <First as Test>::Exec could be equal to Second Argument, but this is not possible because trait-def can't import the over crate for circular dependency reasons.

I have this repo that illustrates the situation (https://github.com/Kayanski/rust-trait-impl-reproduce)

@Kayanski Kayanski added the C-bug Category: This is a bug. label May 8, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 8, 2024
@saethlin saethlin added T-types Relevant to the types team, which will review and decide on the PR/issue. A-coherence Area: Coherence and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 11, 2024
@saethlin
Copy link
Member

@rust-lang/types is it correct that the above scenario should be allowed if the solver were clever enough?

@lcnr
Copy link
Contributor

lcnr commented May 12, 2024

yes it should 😊 it's difficult though as we have to avoid cyclic reasoning. it's currently not a main priority of the types team (as it can be avoided by manually normalizing when writing the impl), but definitely something we'd like to fix at some point

closing as a duplicate of #85576

@lcnr lcnr closed this as completed May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coherence Area: Coherence C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants