nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.63k stars 1.47k forks source link

Can't call closure iterator from inside an async function #16416

Open kayabaNerve opened 3 years ago

kayabaNerve commented 3 years ago

Function echo outputs the wrong string.

Example

iterator x(): int {.closure.} =
  discard

proc y() {.async.} =
  for z in x():
    discard

waitFor y()

Current Output

/usercode/in.nim(7, 7) Error: internal error: expr: var not init x_17965377
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp c <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-compiler for details

Expected Output

Additional Information

Present since at least Nim 1.2.4, is present in latest (1.4.2). Instead of calling x, using var foo = x and then calling foo causes a codegen error.

Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
Hint: used config file '/playground/nim/config/config.nims' [Conf]
..................................
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_assertions.nim.c.o /usercode/nimcache/stdlib_assertions.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_dollars.nim.c.o /usercode/nimcache/stdlib_dollars.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_system.nim.c.o /usercode/nimcache/stdlib_system.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_math.nim.c.o /usercode/nimcache/stdlib_math.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_strutils.nim.c.o /usercode/nimcache/stdlib_strutils.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_options.nim.c.o /usercode/nimcache/stdlib_options.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_times.nim.c.o /usercode/nimcache/stdlib_times.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_os.nim.c.o /usercode/nimcache/stdlib_os.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_heapqueue.nim.c.o /usercode/nimcache/stdlib_heapqueue.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_deques.nim.c.o /usercode/nimcache/stdlib_deques.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_cstrutils.nim.c.o /usercode/nimcache/stdlib_cstrutils.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_asyncfutures.nim.c.o /usercode/nimcache/stdlib_asyncfutures.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_monotimes.nim.c.o /usercode/nimcache/stdlib_monotimes.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_nativesockets.nim.c.o /usercode/nimcache/stdlib_nativesockets.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_selectors.nim.c.o /usercode/nimcache/stdlib_selectors.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_asyncdispatch.nim.c.o /usercode/nimcache/stdlib_asyncdispatch.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c [Exec]
/usercode/nimcache/@min.nim.c: In function 'yIter__aqJRG7LPaI9c3tRfsLoZqKA':
/usercode/nimcache/@min.nim.c:562:77: error: request for member 'ClE_0' in something not a structure or union
     asgnRef((void**) (&(*colonenvP_).foo1.ClE_0), x__ekSWG86elE9byusFc3hPHFw.ClE_0);
                                                                             ^
/usercode/nimcache/@min.nim.c:563:58: error: request for member 'ClP_0' in something not a structure or union
     (*colonenvP_).foo1.ClP_0 = x__ekSWG86elE9byusFc3hPHFw.ClP_0;
                                                          ^
Error: execution of an external program failed: 'gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c'
metagn commented 3 years ago

This gives the same var not init error:

iterator x(): int {.closure.} =
  discard

proc y() =
  iterator z(): int {.closure.} =
    for a in x():
      yield a

  let foo = z

y()
/usercode/in.nim(6, 9) Error: internal error: expr: var not init x_4602093