-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stacked borrows fails on {ChunksMut,ChunksExactMut}::__iterator_get_unchecked()
#94231
Comments
chunks_mut() + zip()
{ChunksMut,ChunksExactMut}::__iterator_get_unchecked()
There's probably more of these kinds of problems that @saethlin has been looking into recently. |
Huge thanks for reporting this @andylizi! This is definitely an issue with the implementation in The existing definition of rust/library/core/src/slice/iter.rs Lines 1556 to 1559 in 1103d2e
Cannot support the current implementation of __iterator_get_unchecked :rust/library/core/src/slice/iter.rs Lines 1632 to 1644 in 1103d2e
The problem is that this method is trying to hand out subslices of a &mut [T] while also holding on to the original. Because it wraps a &mut [T] , ChunksMut guarantees that its inner slice always shrinks when it returns a slice. It looks like this guarantee was accidentally introduced and accidentally upheld in implementation by avoiding unsafe ... until __iterator_get_unchecked was added.
I'm working on a patch that fixes the aliasing problem. It's perhaps worth noting that the specialization on |
All right, I moved this to the Rust repo. |
Nominating for libs; it might be worth a conversation about whether this is proving too unreliable to keep doing in the current way. |
There's an existing test that covers the combination of rust/library/core/tests/slice.rs Lines 398 to 410 in bd1a869
|
Fix slice::ChunksMut aliasing Fixes rust-lang/rust#94231, details in that issue. cc `@RalfJung` This isn't done just yet, all the safety comments are placeholders. But otherwise, it seems to work. I don't really like this approach though. There's a lot of unsafe code where there wasn't before, but as far as I can tell the only other way to uphold the aliasing requirement imposed by `__iterator_get_unchecked` is to use raw slices, which I think require the same amount of unsafe code. All that would do is tie the `len` and `ptr` fields together. Oh I just looked and I'm pretty sure that `ChunksExactMut`, `RChunksMut`, and `RChunksExactMut` also need to be patched. Even more reason to put up a draft.
Description
Running this code in Miri will produce the following output:
With
-Zmiri-track-pointer-tag=1832,1585,12319
A few things to note here:
arr1.chunks(8).zip(arr2.chunks_mut(8))
will fail onchunk2[0]
instead.zip()
is required. This is probably due toZip
uses__iterator_get_unchecked()
internally rather thannext()
.chunks_mut()
andchunks_exact_mut()
can both reproduce.array_chunks_mut()
can't reproduce.Environment
The text was updated successfully, but these errors were encountered: