You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suggest derive(Trait) or T: Trait from transitive obligation in some cases
With code like the following
```rust
struct Ctx<A> {
a_map: HashMap<String, B<A>>,
}
struct B<A> {
a: A,
}
```
the derived trait will have an implicit restriction on `A: Clone` for both types.
When referenced as follows:
```rust
fn foo<Z>(ctx: &mut Ctx<Z>) {
let a_map = ctx.a_map.clone(); //~ ERROR E0599
}
```
suggest constraining `Z`:
```
error[E0599]: the method `clone` exists for struct `HashMap<String, B<Z>>`, but its trait bounds were not satisfied
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:16:27
|
LL | struct B<A> {
| ----------- doesn't satisfy `B<Z>: Clone`
...
LL | let a_map = ctx.a_map.clone();
| ^^^^^ method cannot be called on `HashMap<String, B<Z>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`B<Z>: Clone`
which is required by `HashMap<String, B<Z>>: Clone`
help: consider restricting type parameter `Z`
|
LL | fn foo<Z: std::clone::Clone>(ctx: &mut Ctx<Z>) {
| +++++++++++++++++++
```
When referenced as follows, with a specific type `S`:
```rust
struct S;
fn bar(ctx: &mut Ctx<S>) {
let a_map = ctx.a_map.clone(); //~ ERROR E0599
}
```
suggest `derive`ing the appropriate trait on the local type:
```
error[E0599]: the method `clone` exists for struct `HashMap<String, B<S>>`, but its trait bounds were not satisfied
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:21:27
|
LL | struct B<A> {
| ----------- doesn't satisfy `B<S>: Clone`
...
LL | let a_map = ctx.a_map.clone();
| ^^^^^ method cannot be called on `HashMap<String, B<S>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`B<S>: Clone`
which is required by `HashMap<String, B<S>>: Clone`
help: consider annotating `S` with `#[derive(Clone)]`
|
LL + #[derive(Clone)]
LL | struct S;
|
```
0 commit comments