<core::iter::Filter as Iterator>::next_chunk
virtually core::mem::forget
s all elements that fail the predicate
#126872
Labels
A-iterators
Area: Iterators
C-bug
Category: This is a bug.
requires-nightly
This issue requires a nightly compiler in some way.
The
Iterator::next_chunk
(see #98326) implementation forcore::iter::Filter
does not gracefully drop the elements failing the predicate in the original iterator; instead it forgets them.As an example, you can try this code:
(Playground)
I expected this program to generate the same message as the program without the last line in
main
, that is:(Playground)
However, instead, the first program doesn't print anything.
The origin of this behavior is trivial to find: in the lines 90--98 of the file for
mod core::iter::adapters::filter
of the current master (aabbf84), we have the following code (Github permalink), which is part of the implementation ofnext_chunk
:This
core::ptr::write
s each element that doesn't comply the element into the place out of the range ofguard.initialized
.The elements here, if not overwritten by this process, will be left undropped by the
Drop
implementation of[T; N]::IntoIter
.Meta
Playground version:
The text was updated successfully, but these errors were encountered: