Skip to content

Incorrect lower bound on size hint for <[T]>::splitn if n==0 #87978

Closed
@steffahn

Description

@steffahn

I’m creating this issue in order to better document the bug in std that #87974 fixes once it’s merged.

fn this_should_hold_for_non_buggy_iterators(i: impl Iterator) {
    let (lower_hint, count) = dbg!(i.size_hint().0, i.count());
    assert!(lower_hint <= count)
}

fn main() {
    this_should_hold_for_non_buggy_iterators([()].splitn(0, |_| false));
    // [src/main.rs:3] i.size_hint().0 = 1
    // [src/main.rs:3] i.count() = 0
    // thread 'main' panicked at 'assertion failed: lower_hint <= count', src/main.rs:4:5
}

(playground)

@rustbot label T-libs, A-slice, A-iterators

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsA-sliceArea: `[T]`C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions