Skip to content

Conflicting implementation should not exist after generic trait paramters have been constrained to associated types #123450

Open
@ZhennanWu

Description

Consider this code. Currently it cannot compile.

pub trait Foo {}

pub trait ImplBy {
    type Impl;
}

pub trait FooBy<I> {}

impl<T> Foo for T
where
    T: ImplBy,
    T: FooBy<T::Impl>,
{
}

struct Bar;
impl ImplBy for Bar {
    type Impl = ();
}
impl Foo for Bar {}
// conflicting implementations of trait `Foo` for type `Bar`
// downstream crates may implement trait `FooBy<_>` for type `Bar`

The type solver assumed downstream can impl FooBy<Bar::Impl> for type Bar. But this is easy to reject with orphan rules. Bar::Impl must be in the same scope as Bar itself. Therefore FooBy<Bar::Impl> cannot be implemented on Bar outside its origin crate.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceA-trait-systemArea: Trait systemC-discussionCategory: Discussion or questions that doesn't represent real issues.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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