Commit 9fe5cb5
committed
Auto merge of rust-lang#56161 - RalfJung:vecdeque-stacked-borrows, r=SimonSapin
VecDeque: fix for stacked borrows
`VecDeque` violates a version of stacked borrows where creating a shared reference is not enough to make a location *mutably accessible* from raw pointers (and I think that is the version we want). There are two problems:
* Creating a `NonNull<T>` from `&mut T` goes through `&T` (inferred for a `_`), then `*const T`, then `NonNull<T>`. That means in this stricter version of Stacked Borrows, we cannot actually write to such a `NonNull` because it was created from a shared reference! This PR fixes that by going from `&mut T` to `*mut T` to `*const T`.
* `VecDeque::drain` creates the `Drain` struct by *first* creating a `NonNull` from `self` (which is an `&mut VecDeque`), and *then* calling `self.buffer_as_mut_slice()`. The latter reborrows `self`, asserting that `self` is currently the unique pointer to access this `VecDeque`, and hence invalidating the `NonNull` that was created earlier. This PR fixes that by instead using `self.buffer_as_slice()`, which only performs read accesses and creates only shared references, meaning the raw pointer (`NonNull`) remains valid.
It is possible that other methods on `VecDeque` do something similar, miri's test coverage of `VecDeque` is sparse to say the least.
Cc @nikomatsakis @gankro2 files changed
+8
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1026 | 1026 | | |
1027 | 1027 | | |
1028 | 1028 | | |
1029 | | - | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
1030 | 1033 | | |
1031 | 1034 | | |
1032 | 1035 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2848 | 2848 | | |
2849 | 2849 | | |
2850 | 2850 | | |
2851 | | - | |
| 2851 | + | |
2852 | 2852 | | |
2853 | 2853 | | |
2854 | 2854 | | |
2855 | 2855 | | |
2856 | 2856 | | |
2857 | 2857 | | |
2858 | | - | |
| 2858 | + | |
2859 | 2859 | | |
2860 | 2860 | | |
2861 | 2861 | | |
| |||
3058 | 3058 | | |
3059 | 3059 | | |
3060 | 3060 | | |
3061 | | - | |
| 3061 | + | |
3062 | 3062 | | |
3063 | 3063 | | |
3064 | 3064 | | |
3065 | 3065 | | |
3066 | 3066 | | |
3067 | 3067 | | |
3068 | 3068 | | |
3069 | | - | |
| 3069 | + | |
3070 | 3070 | | |
3071 | 3071 | | |
0 commit comments