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

Internal error with auto return in closure iterator #5859

Closed
mratsim opened this issue May 21, 2017 · 4 comments
Closed

Internal error with auto return in closure iterator #5859

mratsim opened this issue May 21, 2017 · 4 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented May 21, 2017

I stumbled upon the following internal error with the following code:

proc flatIterator[T](s: openarray[T]): auto {.noSideEffect.}=
    result = iterator(): auto =
        when (T is not seq|array):
            for item in s:
                yield item
        else:
            yield 123456

let it = flatIterator(@[@[1,2],@[3,4]])

Error

flatten_iterator.nim(2, 14) Error: internal error: expr: var not init result_104065
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c <file>

Changing the signature to int works

proc flatIterator[T](s: openarray[T]): auto {.noSideEffect.}=
    result = iterator(): int = #<------------ Now it compiles
        when (T is not seq|array):
            for item in s:
                yield item
        else:
            yield 123456

let it = flatIterator(@[@[1,2],@[3,4]])

Context:
Following @zah suggestion in #5708. I'm trying to create a flat iterator that iterates on arbitrary nested openarrays. Inline iterators cannot be recursive (#555) so I'm trying through a closure iterator.

@mratsim
Copy link
Collaborator Author

mratsim commented May 21, 2017

Another error with genTypeInfo, if it's not related I will open another case:

proc flatIterator[N,T](s: array[N, T], U: typedesc): iterator(): U {.noSideEffect.}=
    result = iterator(): U =
        when T is array:
            let it = flatIterator(s, U)
            for item in it():
                yield item
        else:
            for item in s:
                yield item

let it = flatIterator([[1,2],[3,4]], int)

Error:

Error: internal error: genTypeInfo(tyTypeDesc)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c <file>

@mratsim
Copy link
Collaborator Author

mratsim commented May 23, 2017

For reference this is the working code I use that avoids all those issues

iterator flatIter[T](s: openarray[T]): auto {.noSideEffect.}=
  ## Inline iterator on any-depth seq or array
  ## Returns values in order
  for item in s:
    when item is array|seq:
      for subitem in flatIter(item):
        yield subitem
    else:
      yield item

@ghost
Copy link

ghost commented Jun 15, 2018

@mratsim for info - this compiles with devel:

proc flatIterator[N,T](s: array[N, T], U: typedesc): iterator(): U {.noSideEffect.}=
    result = iterator(): U =
        when T is array:
            let it = flatIterator(s, U)
            for item in it():
                yield item
        else:
            for item in s:
                yield item

let it = flatIterator([[1,2],[3,4]], int)

@ghost
Copy link

ghost commented May 25, 2019

All examples here compile (except for the one that was said to work :-). The infinite recursion blows the stack though, but if modified works fine.

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