Skip to content

Generic trait with generic supertrait bound by associated type can't be used in trait bound with associated type #100177

Open
@robinhundt

Description

@robinhundt

I'm trying to create a Channel abstraction over Sink and Stream and encountered the following issue:

Given the following (simplified without Sink and Stream) code: Playground

trait GenericTrait<T> {}

trait Channel<I>: GenericTrait<Self::T> {
    type T;
}

trait Sender {
    type Msg;

    fn send<C>()
    where
        C: Channel<Self::Msg>;
}

impl<T> Sender for T {
    type Msg = ();

    fn send<C>()
    where
        C: Channel<Self::Msg>,
    {
    }
}

// This works
fn foo<I, C>(ch: C) where C: Channel<I> {}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `C: Channel<()>` is not satisfied
  --> src/lib.rs:18:5
   |
18 | /     fn send<C>()
19 | |     where
20 | |         C: Channel<Self::Msg>,
   | |______________________________^ the trait `Channel<()>` is not implemented for `C`
   |
help: consider further restricting this bound
   |
20 |         C: Channel<Self::Msg> + Channel<()>,
   |                               +++++++++++++

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `C: Channel<()>` is not satisfied
  --> src/lib.rs:20:12
   |
20 |         C: Channel<Self::Msg>,
   |            ^^^^^^^^^^^^^^^^^^ the trait `Channel<()>` is not implemented for `C`
   |
help: consider further restricting this bound
   |
20 |         C: Channel<Self::Msg> + Channel<()>,
   |                               +++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors

The issue is present in stable, beta and nightly.

When either removing the generic parameter on GenericParameter<I>, the Self::T bound on trait Channel<I>: GenericTrait<Self::T> or the Self::Msg bound on the send method

fn send<C>()
    where
        C: Channel<Self::Msg>;

the code compiles.

I think this is at least a diagnostics bug, as the error message is quite unhelpful :D This also seems like a bug/limitation of the trait system?

This might be related/the same as #58231 or #57905, but I wasn't exactly sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions