Skip to content

Coherence issue when using crate-local traits with type parameters #97811

Open
@ghananigans

Description

@ghananigans

When using crate-local traits with type parameters, it seems rust incorrectly thinks that downstream crates may implement the trait (which we know to be impossible as the traits are crate-local).

See this playground.

Also captured here for readability:

pub(crate) struct DummyCtx {}

pub(crate) trait FooContext<C> {}

trait LocalTrait {}

pub(crate) trait BarContext<C>: LocalTrait {}

impl<C> FooContext<C> for DummyCtx {}

// Doesn't work:
impl<C: BarContext<F>, F> FooContext<F> for C {}

// Works:
// impl<C: BarContext<F> + LocalTrait, F> FooContext<F> for C {}

Expected both impls to compile but only the impl that restricts C to LocalTrait compiles even though BarContext<F> is a supertrait over LocalTrait which (IIUC) makes the LocalTrait restriction in C: BarContext<F> + LocalTrait redundant.

Meta

Can observe failure on 2021 edition stable (see rust playground link)

This playground is another example of this but without a LocalTrait.

Note that using crate-local traits without any type parameters work just fine playground.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coherenceArea: CoherenceA-trait-systemArea: Trait systemA-visibilityArea: Visibility / privacyC-enhancementCategory: An issue proposing an enhancement or a PR with one.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