-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit 350d3e6
Suggest
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;
|
```derive(Trait)
or T: Trait
from transitive obligation in some cases1 parent 3811f40 commit 350d3e6
File tree
5 files changed
+192
-11
lines changed- compiler
- rustc_hir_typeck/src/method
- rustc_trait_selection/src/error_reporting/traits
- tests/ui/suggestions
5 files changed
+192
-11
lines changedLines changed: 95 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
37 | 37 |
| |
38 | 38 |
| |
39 | 39 |
| |
40 | + | ||
40 | 41 |
| |
41 | 42 |
| |
42 | 43 |
| |
| |||
1368 | 1369 |
| |
1369 | 1370 |
| |
1370 | 1371 |
| |
1371 | - | ||
1372 | + | ||
1373 | + | ||
1374 | + | ||
1375 | + | ||
1376 | + | ||
1377 | + | ||
1378 | + | ||
1379 | + | ||
1380 | + | ||
1381 | + | ||
1382 | + | ||
1383 | + | ||
1384 | + | ||
1385 | + | ||
1386 | + | ||
1387 | + | ||
1388 | + | ||
1389 | + | ||
1390 | + | ||
1391 | + | ||
1392 | + | ||
1393 | + | ||
1394 | + | ||
1395 | + | ||
1396 | + | ||
1397 | + | ||
1398 | + | ||
1399 | + | ||
1400 | + | ||
1401 | + | ||
1402 | + | ||
1403 | + | ||
1404 | + | ||
1405 | + | ||
1406 | + | ||
1407 | + | ||
1408 | + | ||
1409 | + | ||
1410 | + | ||
1411 | + | ||
1412 | + | ||
1413 | + | ||
1414 | + | ||
1415 | + | ||
1416 | + | ||
1417 | + | ||
1418 | + | ||
1419 | + | ||
1420 | + | ||
1421 | + | ||
1422 | + | ||
1423 | + | ||
1424 | + | ||
1425 | + | ||
1426 | + | ||
1427 | + | ||
1428 | + | ||
1429 | + | ||
1430 | + | ||
1431 | + | ||
1432 | + | ||
1433 | + | ||
1434 | + | ||
1435 | + | ||
1436 | + | ||
1437 | + | ||
1438 | + | ||
1439 | + | ||
1440 | + | ||
1441 | + | ||
1442 | + | ||
1443 | + | ||
1444 | + | ||
1445 | + | ||
1446 | + | ||
1447 | + | ||
1448 | + | ||
1449 | + | ||
1450 | + | ||
1451 | + | ||
1452 | + | ||
1453 | + | ||
1454 | + | ||
1455 | + | ||
1456 | + | ||
1457 | + | ||
1458 | + | ||
1459 | + | ||
1460 | + | ||
1461 | + | ||
1462 | + | ||
1463 | + | ||
1464 | + | ||
1465 | + | ||
1372 | 1466 |
| |
1373 | 1467 |
| |
1374 | 1468 |
| |
|
Lines changed: 10 additions & 10 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
249 | 249 |
| |
250 | 250 |
| |
251 | 251 |
| |
252 | - | ||
252 | + | ||
253 | 253 |
| |
254 | - | ||
254 | + | ||
255 | 255 |
| |
256 | 256 |
| |
257 | 257 |
| |
| |||
286 | 286 |
| |
287 | 287 |
| |
288 | 288 |
| |
289 | - | ||
289 | + | ||
290 | 290 |
| |
291 | 291 |
| |
292 | 292 |
| |
| |||
300 | 300 |
| |
301 | 301 |
| |
302 | 302 |
| |
303 | - | ||
303 | + | ||
304 | 304 |
| |
305 | 305 |
| |
306 | 306 |
| |
| |||
328 | 328 |
| |
329 | 329 |
| |
330 | 330 |
| |
331 | - | ||
331 | + | ||
332 | 332 |
| |
333 | 333 |
| |
334 | 334 |
| |
| |||
348 | 348 |
| |
349 | 349 |
| |
350 | 350 |
| |
351 | - | ||
351 | + | ||
352 | 352 |
| |
353 | 353 |
| |
354 | 354 |
| |
| |||
381 | 381 |
| |
382 | 382 |
| |
383 | 383 |
| |
384 | - | ||
384 | + | ||
385 | 385 |
| |
386 | 386 |
| |
387 | 387 |
| |
| |||
413 | 413 |
| |
414 | 414 |
| |
415 | 415 |
| |
416 | - | ||
416 | + | ||
417 | 417 |
| |
418 | 418 |
| |
419 | 419 |
| |
| |||
439 | 439 |
| |
440 | 440 |
| |
441 | 441 |
| |
442 | - | ||
442 | + | ||
443 | 443 |
| |
444 | 444 |
| |
445 | - | ||
445 | + | ||
446 | 446 |
| |
447 | 447 |
| |
448 | 448 |
| |
|
Lines changed: 25 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + |
Lines changed: 24 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | + |
Lines changed: 38 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + | ||
28 | + | ||
29 | + | ||
30 | + | ||
31 | + | ||
32 | + | ||
33 | + | ||
34 | + | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | + |
0 commit comments