Skip to content

Infinitely recursive instantiation of TakeWhile hangs compilation #21403

@Xirdus

Description

@Xirdus

Minimal example: http://is.gd/dJjKjV

fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
    g(i.take_while(|_| false))
}

fn g<T: Iterator<Item=i32>>(i: T) -> i32 {
    f(i.take_while(|_| false))
}

fn main() {
    f([2].iter().cloned());
}

If you change take_while() to take(), it gives the following error:

<anon>:1:1: 1:1 error: overflow evaluating the requirement `*const i32 : core::marker::Sized` [E0275]
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: consider adding a `#![recursion_limit="128"]` attribute to your crate
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: required because it appears within the type `core::slice::Iter<i32>`
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: required because it appears within the type `core::iter::Cloned<core::slice::Iter<i32>>`
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: required because it appears within the type `core::iter::Take<core::iter::Cloned<core::slice::Iter<i32>>>`
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: required because it appears within the type `core::iter::Take<core::iter::Take<core::iter::Cloned<core::slice::Iter<i32>>>>`
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {
         ^
<anon>:1:1: 1:1 note: required because it appears within the type `core::iter::Take<core::iter::Take<core::iter::Take<core::iter::Cloned<core::slice::Iter<i32>>>>>`
<anon>:1 fn f<T: Iterator<Item=i32>>(i: T) -> i32 {

(and so on)

The error seems to be a correct behavior. The only difference between take() and take_while() is that the latter takes lambda as argument. Why does it behave differently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions