Flatten and FlatMap have incorrect bounds for their FusedIterator impls #81248
Labels
A-iterators
Area: Iterators
C-bug
Category: This is a bug.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
I would expect the following code to either not compile (because
iter
shouldn't implementFusedIterator
) or to not panic (because it does, so its contract should guarantee that calling next after the firstNone
will always returnNone
), however it compiles and panics:Playground example
This is caused by the fact that the impl of
FusedIterator
forFlatten
only requires the outer iterator to implementFusedIterator
, not the inner one, however the implementation doesn't actually fusebackiter
, which is what causes the assert to fail. The same problem is present forFlatMap
.Possible solutions:
FlattenCompat
is implemented to guarantee it to be fused even if the inner iterators are not fused. This would silently change its behavior, however it wouldn't be a breaking change.Additionally I don't think there's anything that requires the outer iterator to always be manually fused in
FlattenCompat
. Should we also change it in the process?See also https://users.rust-lang.org/t/why-doesn-t-std-flatten-implement-std-fusediterator-regardless-if-the-outer-or-inner-iterators-do/54477
The text was updated successfully, but these errors were encountered: