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

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

Open
ghananigans opened this issue Jun 6, 2022 · 1 comment
Open
Labels
A-coherence Area: Coherence A-traits Area: Trait system A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ghananigans
Copy link

ghananigans commented Jun 6, 2022

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.

@ghananigans ghananigans added the C-bug Category: This is a bug. label Jun 6, 2022
@ghananigans
Copy link
Author

cc: @brunowonka @zeling

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@fmease fmease added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-traits Area: Trait system A-visibility Area: Visibility / privacy. T-types Relevant to the types team, which will review and decide on the PR/issue. A-coherence Area: Coherence and removed C-bug Category: This is a bug. needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coherence Area: Coherence A-traits Area: Trait system A-visibility Area: Visibility / privacy. C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

3 participants