Skip to content

[REGRESSION] iterator composition doesn't work, preventing lazy map/filter/etc libraries #8188

@timotheecour

Description

@timotheecour

Here's a simplified scenario.
It's a major blocker for any kind of lazy map/filter library, such as https://github.com/def-/nim-iterutils/blob/53f4a30e57f01cd8cf9d5b1dd22053eac82b25f2/src/iterutils.nim#L303 "TODO: Currently fail" where it shows map/filter etc cannot be composed.

proc myiter*(n:int): auto =
  iterator it: int {.closure.} =
    for x in 0..n:
      yield x
  return it

proc iterFun*[T](a:T): auto =
  iterator it: int {.closure.} =
    for x in a():
      yield x
  return it

proc test()=
  let it1 = myiter(5)
  let it2 = iterFun(it1)

  # nothing is printed here:
  for a in it2(): echo a

  echo "done"

test()

this prints "done"
it should print 0 1 2 3 4 5 done

EDIT this is a regression, I just verified this worked in 0.9.4 (which was nimrod)

$git_clone_D/temp/nim/Nim3/bin/nimrod c -r t26_iterator_compose2.nim
0
1
2
3
4
5
done

$git_clone_D/temp/nim/Nim3/bin/nimrod --version
Nimrod Compiler Version 0.9.4 (2018-07-02) [MacOSX: amd64]
Copyright (c) 2006-2014 by Andreas Rumpf

NOTE: I also verified https://github.com/def-/nim-iterutils used to work with 0.9.4:

git clone https://github.com/def-/nim-iterutils
cd nim-iterutils
git checkout 40b83d2de580b93c5ef96100a72df1ea4981a7a8
$git_clone_D/temp/nim/Nim3/bin/nimrod c -r src/iterutils.nim

NOTE: to build 0.9.4, unfortunately choosenim didn't work, see but I posted in dom96/choosenim#66 (I show there how I built 0.9.4 , not sure if there's a simpler way)

EDIT
I just tried with v0.13.0, that version already has the regression.

Not sure how to build nim for versions between v0.9.4 and v0.13.0 because https://github.com/nim-lang/csources.git doesn't have any git tag between these and ./bin/nimrod c koch fails for csources.git:v0.9.4 with Nim:v0.12.0

EDIT fixed in c554c2a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions