Skip to content

step_by + skip produce incorrect results #55985

Description

@newpavlov

The following snippet demonstrates the problem:

println!("first");
for i in (0..640).step_by(128) {
    println!("{}", i)
}
println!("second");
for i in (0..640).step_by(128).skip(1) {
    println!("{}", i)
}
println!("third");
for i in (0..640).step_by(128).map(|i| i).skip(1) {
    println!("{}", i)
}

It prints:

first
0
128
256
384
512
second
128
129
257
385
513
third
128
256
384
512

I expect second iterator to produce the same result as for the third one.

Activity

  1. jethrogb commented on Nov 15, 2018

    @jethrogb
    Contributor

    Regression from 1.28.0 to 1.29.0. Introduced in #51601

  2. m1el commented on Nov 15, 2018

    @m1el
    Contributor

    The problem is interaction between StepBy::nth and StepBySpecIterator::spec_next. spec_next ignores the inner working of StepBy.

    Test-case reduced to using StepBy without Skip.

    fn main() {
        let mut it = (0_usize..640).step_by(128);
        it.nth(0);
        assert_eq!(it.next(), Some(128));
    }
  3. Emerentius commented on Nov 19, 2018

    @Emerentius
    Contributor

    From a cursory look, it seems to me that the problem is that nth and next are out of sync. StepBy specifically allows two different kind of semantics for the underlying iterator, either next(), nth(step-1), nth(step-1), … or advance_n_and_return_first. If both next and nth follow the same one, the bug shouldn't occur.

    In other words, if next is specialized and nth overrides the default, then nth needs to be specialized as well.

  4. added a commit that references this issue on Nov 20, 2018
    31fa301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libs-api[DEPRECATED; DO NOT USE]regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions