Skip to content

Chain iterator adapter changed behavior in nightly #71375

Closed
@arturoc

Description

@arturoc

Recently the Chain adapter has been changed to fuse the iterators it holds by destroying them, this introduces a subtle behavior change that is difficult to debug and as far as I see not documented.

For example the following:

use std::sync::mpsc;

fn main() {
    let (tx,rx) = mpsc::channel();
    let mut it = vec![1, 2, 3].into_iter().chain(rx.try_iter());
    
    tx.send(4).unwrap();
    tx.send(5).unwrap();
    assert_eq!(it.next(), Some(1));
    assert_eq!(it.next(), Some(2));
    assert_eq!(it.next(), Some(3));
    assert_eq!(it.next(), Some(4));
    assert_eq!(it.next(), Some(5));
    assert_eq!(it.next(), None);
    
    tx.send(6).unwrap();
    assert_eq!(it.next(), Some(6));
    
    tx.send(7).unwrap();
    assert_eq!(it.next(), Some(7));
}

runs correctly on current stable (1.42) but panics in nightly cause the iterator is now fused so it can't be called anymore after it returns None for the first time.

stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a36e52425fa425eac3b7c24341b73d49

nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4958f8dccb7594a0fa06a26ebc3d442b

Perhaps a possible solution would be to fuse the first iterator in the chain but not the second?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions