Skip to content

Internal error with auto return in closure iterator #5859

@mratsim

Description

@mratsim

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions