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.55k stars 1.47k forks source link

Compiler crash when trying to create a closure within a loop over `os.walkDir` which captures an outside varaible #15353

Open Quelklef opened 4 years ago

Quelklef commented 4 years ago

Example

import os
for unused_var in walkDir("."):
  let outside = "x"
  let closure = proc(): string = outside

Current Output

Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: in [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
/usercode/in.nim(5, 7) Hint: 'closure' is declared but not used [XDeclaredButNotUsed]
Hint: gcc -c  -w   -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   -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   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_pathnorm.nim.c.o /usercode/nimcache/stdlib_pathnorm.nim.c [Exec]
Hint: gcc -c  -w   -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   -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   -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 'colonanonymous___joSqnaeyrUqFaMMdrvJu0A':
/usercode/nimcache/@min.nim.c:495:22: error: 'outside__XaBgCwy9aLkuI2Qj5G8ldCA_2' undeclared (first use in this function)
  result = copyString(outside__XaBgCwy9aLkuI2Qj5G8ldCA_2);
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usercode/nimcache/@min.nim.c:495:22: note: each undeclared identifier is reported only once for each function it appears in
Error: execution of an external program failed: 'gcc -c  -w   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c'

Expected Output

No compiler crash

Possible Solution

Additional Information

$ nim -v
Nim Compiler Version 0.3.5 [Linux: amd64]
git hash: cbd78ec1f68e78a88e4a83a5009bc4c8c28c79b3
Quelklef commented 4 years ago

It's not clear to me why walkDir is special, but the os module was pretty resistant to me trying to crack it open and find out.