-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Trait bounds on associated types of subtraits don't work correctly #54149
Comments
This is another issue with probably the same underlying cause as #24159. Here's how to fix your code: trait MyTrait1 {
type X;
fn get_val(&self) -> Self::X;
}
trait MyTrait2: MyTrait1 where <Self as MyTrait1>::X: Into<u32> {}
struct MyStruct(u32);
impl MyTrait1 for MyStruct {
type X = u32;
fn get_val(&self) -> Self::X { self.0 }
}
impl MyTrait2 for MyStruct {}
fn myfunc<T>(v: &T)
where T: MyTrait2,
<T as MyTrait1>::X: Into<u32>,
{
println!("{}", Into::<u32>::into(v.get_val()));
}
fn main() {
let s = MyStruct(10);
myfunc(&s);
} Funnily enough there is no error when you remove |
Current output:
A possible solution is to incorporate a new type param to restrict fn myfunc<I: Into<u32>, T: MyTrait2<X = I>>(v: &T) {
println!("{}", Into::<u32>::into(v.get_val()));
} |
Triage: no change. |
Current output suggests a fix:
|
Why does |
The following code does not compile:
It fails with
In fact, I haven't been able to come up with any syntax for
myfunc
which uses a trait bound usingMyTrait2
that will compile once the trait boundX
exists.Changing the declaration of
MyTrait2
totrait MyTrait2: MyTrait1<X=u32> {}
does work, so it's clearly possible for super traits to put constraints on the associated types of subtraits. However I've only been able to create constraints that require the associated types to be of a particular type, not to require that they implement particular traits. I also triedtrait MyTrait2: MyTrait1<X: Into<u32>> {}
, however that just yielded a syntax error.It's quite possible that I'm misunderstanding the meaning of the syntax in the original example above, and that I should create an RFC for supporting what I'm trying to accomplish above.
The text was updated successfully, but these errors were encountered: