Commit 00ce472
committed
Auto merge of #96820 - r-raymond:master, r=cuviper
Make RwLockReadGuard covariant
Hi, first time contributor here, if anything is not as expected, please let me know.
`RwLockReadGoard`'s type constructor is invariant. Since it behaves like a smart pointer to an immutable reference, there is no reason that it should not be covariant. Take e.g.
```
fn test_read_guard_covariance() {
fn do_stuff<'a>(_: RwLockReadGuard<'_, &'a i32>, _: &'a i32) {}
let j: i32 = 5;
let lock = RwLock::new(&j);
{
let i = 6;
do_stuff(lock.read().unwrap(), &i);
}
drop(lock);
}
```
where the compiler complains that &i doesn't live long enough. If `RwLockReadGuard` is covariant, then the above code is accepted because the lifetime can be shorter than `'a`.
In order for `RwLockReadGuard` to be covariant, it can't contain a full reference to the `RwLock`, which can never be covariant (because it exposes a mutable reference to the underlying data structure). By reducing the data structure to the required pieces of `RwLock`, the rest falls in place.
If there is a better way to do a test that tests successful compilation, please let me know.
Fixes #80392File tree
4 files changed
+53
-10
lines changed- library/std/src/sync
- rwlock
- src/test
- codegen
- debuginfo
4 files changed
+53
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
104 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
105 | 111 | | |
106 | 112 | | |
107 | 113 | | |
| |||
511 | 517 | | |
512 | 518 | | |
513 | 519 | | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
514 | 523 | | |
515 | | - | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
516 | 528 | | |
517 | 529 | | |
518 | 530 | | |
519 | 531 | | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
520 | 535 | | |
521 | 536 | | |
522 | 537 | | |
| |||
555 | 570 | | |
556 | 571 | | |
557 | 572 | | |
558 | | - | |
| 573 | + | |
| 574 | + | |
559 | 575 | | |
560 | 576 | | |
561 | 577 | | |
| |||
564 | 580 | | |
565 | 581 | | |
566 | 582 | | |
| 583 | + | |
567 | 584 | | |
568 | 585 | | |
569 | 586 | | |
570 | 587 | | |
571 | 588 | | |
572 | 589 | | |
573 | 590 | | |
| 591 | + | |
574 | 592 | | |
575 | 593 | | |
576 | 594 | | |
577 | 595 | | |
578 | 596 | | |
579 | 597 | | |
580 | 598 | | |
| 599 | + | |
581 | 600 | | |
582 | | - | |
| 601 | + | |
583 | 602 | | |
584 | 603 | | |
585 | 604 | | |
| |||
588 | 607 | | |
589 | 608 | | |
590 | 609 | | |
| 610 | + | |
591 | 611 | | |
592 | 612 | | |
593 | 613 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 18 | + | |
| 19 | + | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
| |||
0 commit comments