Skip to content

Box<Iterator> (and &mut Iterator) does not implement Iterator #20953

Closed
@BurntSushi

Description

@BurntSushi

This code fails to compile:

fn main() {
    let mut shrinker = Box::new(vec![1].into_iter()) as Box<Iterator<Item=i32>>;
    println!("{:?}", shrinker.next());
    for v in shrinker { println!("{:?}", v); }
}

error message:

[andrew@Liger play] rustc scratch2.rs 
scratch2.rs:32:14: 32:22 error: `for` loop expression has type `Box<core::iter::Iterator>` which does not implement the `Iterator` trait; maybe try .iter()
scratch2.rs:32     for v in shrinker { println!("{:?}", v); }
                            ^~~~~~~~
error: aborting due to previous error

A similar error occurs if &mut Iterator is used instead of Box<Iterator>. However, if you comment out the for loop, the call to next works just fine. I thought maybe it had something to do with auto-deref, so I tried dereferencing the iterator:

for v in *shrinker { println!("{:?}", v); }

but rustc dumps core:

[andrew@Liger play] rustc scratch2.rs 
rustc: /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/llvm/lib/IR/Instructions.cpp:1086: void llvm::StoreInst::AssertOK(): Assertion `getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"' failed.
Aborted (core dumped)

I'm not sure, but this may be related to #20605? rustc dumps core when I try &mut Iterator too.

rustc version:

[andrew@Liger play] rustc --version
rustc 1.0.0-nightly (44a287e6e 2015-01-08 17:03:40 -0800)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions