Open
Description
We have seen a few cases in libstd where the rule that just creating a mutable reference already asserts uniqueness is a problem:
VecDeque
creating overlapping mutable references.BTreeMap
creating mutable references that overlap with shared references.LinkedList
creating overlapping mutable references.Vec::push
invalidating existing references into the vector. The typed-arena crate ran into the same problem.- shepmaster asking on Zulip why some code doesn't work.
- We have a huge problem with self-referential generators, see Stacked Borrows: asserting uniqueness too early? Should we allow the optimizer to add spurious stores? #133.
- Illegal aliasing of mutable references bodil/sized-chunks#8 would likely also not be an issue if we could delay uniqueness for long enough.
- Some uses of
ptr::replace
are forbidden even though they should probably be allowed.
In some cases, even just creating a shared ref is an issue, as it conflicts with a recently created mutable ref:
- miri:error printing the return value of align_to_mut rust#68549
- Miri failure in btree::map::test_iter_min_max test rust#73915
So maybe we assert uniqueness to aggressively here? Maybe mutable references should only become unique on first write, or so? I am not sure what that would look like though. (In principle this could also happen with shared references but I have not seen that.)
I'll use this issue to collect such cases.
However, there are also cases where people want the extra UB to get more optimizations: