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

Compiler internal error when defining local iterator variable. #17632

Open
lscrd opened this issue Apr 3, 2021 · 1 comment
Open

Compiler internal error when defining local iterator variable. #17632

lscrd opened this issue Apr 3, 2021 · 1 comment

Comments

@lscrd
Copy link

lscrd commented Apr 3, 2021

When defining an iterator variable, the program compiles successfully if the variable is global but causes an internal compiler error if it is local.

Example

The following program compiles and execute without any problem:

iterator iter(n: int): int {.closure.} =
  for i in 1..n:
    yield i

var it = iterator: int = (for i in iter(5): yield i)

echo it()
echo it()

But moving the definition of "it" in a procedure makes the compiler encounter an internal error: internal error: expr: var not init iter_4607114

iterator iter(n: int): int {.closure.} =
  for i in 1..n:
    yield i

proc p() =
  var it = iterator: int = (for i in iter(5): yield i)

  echo it()
  echo it()

p()

Current Output

internal error: expr: var not init iter_4607114
The error exists with devel compiler too.

Expected Output

No internal error, of course.

Possible Solution

Adding {.closure.} to the iterator defining the variable doesn’t solve the issue.
But changing the closure iterator to an inline iterator solves it.

Additional Information

There are previous issues related to iterator variable but as far as I know they are considered as closed.

$ nim -v
Nim Compiler Version 1.4.4 [Linux: amd64]
@lscrd lscrd changed the title Compiler internal error when defining local variable iterator. Compiler internal error when defining local iterator variable. Apr 3, 2021
@ghost
Copy link

ghost commented Apr 3, 2021

Maybe related:
#15896
#16416

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

2 participants