Open
Description
Code
Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=cda41942df9b5feddbbf2417959e717a
use std::collections::HashMap;
#[derive(Clone)]
struct Ctx<A> {
a_map: HashMap<String, B<A>>,
}
#[derive(Clone)]
// help: consider annotating `B<A>` with `#[derive(Clone)]`
struct B<A> {
// ----------- doesn't satisfy `B<A>: Clone`
a: A,
}
fn foo<A>(ctx: &mut Ctx<A>) {
let a_map = ctx.a_map.clone();
// ^^^^^ method cannot be called on `HashMap<String, B<A>>` due to unsatisfied trait bounds
}
Current output
error[E0599]: the method `clone` exists for struct `HashMap<String, B<A>>`, but its trait bounds were not satisfied
--> src/lib.rs:15:27
|
10 | struct B<A> {
| ----------- doesn't satisfy `B<A>: Clone`
...
15 | let a_map = ctx.a_map.clone();
| ^^^^^ method cannot be called on `HashMap<String, B<A>>` due to unsatisfied trait bounds
|
::: /Users/USERNAME/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:214:1
|
214 | pub struct HashMap<K, V, S = RandomState> {
| ----------------------------------------- doesn't satisfy `HashMap<String, B<A>>: Clone`
|
= note: the following trait bounds were not satisfied:
`B<A>: Clone`
which is required by `HashMap<String, B<A>>: Clone`
help: consider annotating `B<A>` with `#[derive(Clone)]`
|
10 | #[derive(Clone)]
|
For more information about this error, try `rustc --explain E0599`.
error: could not compile `test_clone_err` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `test_clone_err` (lib test) due to previous error
Desired output
error[E0599]: the method `clone` exists for mutable reference `&mut Ctx<A>`, but its trait bounds were not satisfied
--> src/lib.rs:16:9
|
4 | struct Ctx<A> {
| ------------- doesn't satisfy `Ctx<A>: Clone`
...
16 | ctx.clone();
| ^^^^^ method cannot be called on `&mut Ctx<A>` due to unsatisfied trait bounds
|
note: trait bound `A: Clone` was not satisfied
--> src/lib.rs:3:10
|
3 | #[derive(Clone)]
| ^^^^^ unsatisfied trait bound introduced in this `derive` macro
help: consider restricting the type parameter to satisfy the trait bound
|
15 | fn foo<A>(ctx: &mut Ctx<A>) where A: Clone {
| ++++++++++++++
For more information about this error, try `rustc --explain E0599`.
error: could not compile `test_clone_err` (lib test) due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `test_clone_err` (lib) due to previous error
Rationale and extra context
The root cause is that P
in foo<P>
has not been restricted by the Clone
bound, but the compiler suggests users to add #[derive(Clone)]
on struct B<A>
, which has already been done.
Other cases
No response
Anything else?
rustc
versions:
rustc 1.70.0-nightly (88fb1b922 2023-04-10)
rustc 1.71.0-nightly (d0f204e4d 2023-04-16)
I have tested the same code under both of the versions and the outputs are the same.