Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure iterator with inner iterator causes infinite loop #12848

Open
auxym opened this issue Dec 8, 2019 · 2 comments
Open

Closure iterator with inner iterator causes infinite loop #12848

auxym opened this issue Dec 8, 2019 · 2 comments

Comments

@auxym
Copy link
Contributor

auxym commented Dec 8, 2019

The following program runs infinitely:

Example

iterator count2(): int {.closure.} =
  var x = 1
  yield x
  inc x
  yield x

iterator iterinvoke[T](iter: iterator(): T {.closure.}): T {.closure.} =
  for x in iter(): yield x

for e in iterinvoke(count2): echo e

Current Output

1 is echo'd infinitely, seems like the inner iterator is not advanced.

Expected Output

1
2
<program ends>

Possible Solution

Removing {.closure.} from the iterinvoke definition does not trigger the infinite loop behavior.

Additional Information

@Araq let me know on IRC that this was an old bug that was fixed in the past, though I did not find the issue from a quick search.

$ nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]

Installed from choosenim devel on 2019-12-07, but I also got the same issue with 1.0.4 release.

@cooldome
Copy link
Member

cooldome commented Dec 8, 2019

This how it works

iterator count2(): int {.closure.} =
  var x = 1
  yield x
  inc x
  yield x

iterator iterinvoke[T](iter: iterator(): T {.closure.}): T {.closure.} =
  let t = iter
  for x in t(): yield x

for e in iterinvoke(count2): echo e

@pmetras
Copy link

pmetras commented Jan 20, 2020

Some problem as #12944

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants