Skip to content

Trait argument from associated type has conflicting impl #99940

Open
@conradludgate

Description

@conradludgate

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=5a6bc60775d41ce5ef3ff9656d489c51

#![feature(try_trait_v2)]
struct Flip<T>(T);
impl<T> std::ops::FromResidual<Flip<T>> for Option<T>
{
    fn from_residual(residual: Flip<T>) -> Self {
        Some(residual.0)
    }
}

I expected to see this happen: Code compiles successfully

Instead, this happened:

error[[E0119]](https://doc.rust-lang.org/nightly/error-index.html#E0119): conflicting implementations of trait `std::ops::FromResidual<Flip<_>>` for type `std::option::Option<_>`
 --> src/lib.rs:3:1
  |
3 | impl<T> std::ops::FromResidual<Flip<T>> for Option<T>
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> FromResidual for Option<T>;

As you you see, the stdlib has

impl<T> FromResidual for Option<T> {}

Which makes use of a default type arg on FromResidual - <Self as Try>::Residual. For Option<T>, that is Option<Infallible>.

So apparently, FromResidual<Option<Infallible>> conflicts with FromResidual<Flip<T>>.

Playing devil's advocate, we could assume that FromResidual is being conservative with its conflicts because Try::Residual could change, but it could never change to a type I own.

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (3924dac7b 2022-07-29)
binary: rustc
commit-hash: 3924dac7bb29bc8eb348059c901e8f912399c857
commit-date: 2022-07-29
host: aarch64-apple-darwin
release: 1.64.0-nightly
LLVM version: 14.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions