Hi folks,
The following code (Rust Playground) is giving me a surprising error:
trait T {
type AssocCopy: Copy;
}
#[derive(Clone, Copy)]
struct E<G: T> {
ac: G::AssocCopy,
}
fn foo<G: T>()
where
E<G>: Copy,
{
unimplemented!()
}
#[derive(Clone, Copy)]
struct A;
struct O;
impl T for O {
type AssocCopy = A;
}
fn main() {
foo::<O>()
}
Error:
27 | foo::<O>()
| ^ the trait `Copy` is not implemented for `O`, which is required by `E<O>: Copy`
|
note: required for `E<O>` to implement `Copy`
...
(see the playground link for the full error)
I find this confusing (and wrong, I believe): why should O
need to implement Copy
? The only Copy
bounds in this code are on T
's associated type AssocCopy
, not on T
itself.
This is my minimally reproducible case. In my original version, it gave a different error, which was roughly:
| foo(&O)
| ---- ^ the trait `std::marker::Copy` is not implemented for `E<_>`
| |
| required by a bound introduced by this call
Any suggestions? I'm guessing that the inference is too tricky for the compiler, but I don't know what annotations to add to fix it.
Thanks for any help!