-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Generic trait with generic supertrait bound by associated type can't be used in trait bound with associated type #100177
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
Comments
@rustbot claim |
The diagnostic needs to be suppressed in this case, but also I don't know why this doesn't work but it's probably due to normalization and substitution somewhere happening in the wrong order, since we clearly know what the expected/normalized param-env bound looks like 🤔 |
Ugh this is a really strange param env normalization bug. |
@robinhundt were you able to find any workaround for this? I'm running into this exact same issue, and I can't find a way to work around it while still expressing the relationships between the traits that I want to express. |
@Anders429 Well, kind of. As I wrote in the initial OP, I wanted to unify trait Channel<Item>: Sink<Item> + TryStream<Ok = Item> {}
impl<Item, S> Channel<Item> for S where S: Sink<Item> + TryStream<Ok = Item> {} which allows me to refer to the stream error as It appears I simplified code in the OP a bit too far. My solution with the trait GenericTrait<T> {}
trait ParamToAssoc {
type Assoc;
}
impl<T, G> ParamToAssoc for G where G: GenericTrait<T> {
type Assoc = T;
} fails with an unconstrained type parameter error in the Maybe your use case is similar to mine and this helps you 😊 |
Well, it seems my solution of just referring to the associated type of the super trait does not work completely... 😢 trait Sink<Item> {
type Error;
fn foo(&mut self) -> Result<Item, Self::Error> {
todo!()
}
}
trait Channel<Item>: Sink<Item> {}
// This works
fn use_channel<C>(ch: &mut C) -> Result<(), C::Error>
where
C: Channel<()>,
<C as Sink<()>>::Error: Send,
{
// ch.foo()
todo!()
}
trait UsingChannel {
type Item;
fn use_channel<C>(ch: &mut C) -> Result<Self::Item, C::Error>
where
// Commenting out the following line in the declaration and the impl
// makes the code compile
<C as Sink<Self::Item>>::Error: Send,
C: Channel<Self::Item>;
}
struct Foo;
// --------- The Problem is in this impl ---------
impl UsingChannel for Foo {
type Item = ();
fn use_channel<C>(ch: &mut C) -> Result<Self::Item, C::Error>
where
C: Channel<Self::Item>,
// commenting out this line makes the code compile
<C as Sink<Self::Item>>::Error: Send,
{
// ch.foo()
todo!()
}
} the code compiles without the This makes it currently impossible for me to write an abstraction over |
I've tried to further reduce the issue. Unfortunately, not even the following simplified code compiles: trait Sink<Item> {
type Error;
}
trait UsingSink {
type Item;
fn use_sink<S>(ch: &mut S) -> Result<Self::Item, S::Error>
where
S: Sink<Self::Item>,
// Commenting out the following line in the declaration and the impl
// makes the code compile
S::Error: Send;
}
struct Foo;
// --------- The Problem is in this impl ---------
impl UsingSink for Foo {
type Item = ();
fn use_sink<S>(ch: &mut S) -> Result<Self::Item, S::Error>
where
S: Sink<Self::Item>,
// commenting out this line makes the code compile
S::Error: Send,
{
todo!()
}
} This reduces the usability of the For this simple case, a workaround exists by introducing a new generic parameter for the |
I'm trying to create a
Channel
abstraction overSink
andStream
and encountered the following issue:Given the following (simplified without Sink and Stream) code: Playground
The current output is:
The issue is present in stable, beta and nightly.
When either removing the generic parameter on
GenericParameter<I>
, theSelf::T
bound ontrait Channel<I>: GenericTrait<Self::T>
or theSelf::Msg
bound on the send methodthe 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.
The text was updated successfully, but these errors were encountered: