Nim: SIGSEGV with iterator

Created on 7 Oct 2018  路  4Comments  路  Source: nim-lang/Nim

# D20181006T142957
when defined(case1):
  # SIGSEGV
  import os
  proc foo(): auto =
    result = iterator (): auto =
      for path in walkPattern("*"):
        yield 0

when defined(case2):
  # ok
  import os
  iterator foo2(): auto =
    for path in walkPattern("*"):
      yield 0

when defined(case3):
  # SIGSEGV
  # reduced from os.walkPattern. any change seems to remove SIGSEGV
  iterator walkPattern2*(): int =
    defer:
      var ret: int
      let ret2 = addr(ret)
    yield 1

  proc foo(): auto =
    result = iterator (): auto =
      for ai in walkPattern2():
        yield 0

Crash

Most helpful comment

Workaround: don't use auto
Life pro-tip: don't use auto

All 4 comments

Workaround: don't use auto
Life pro-tip: don't use auto

there are cases where explicit types is better (eg fwd declaration) but when type is complex, auto is more DRY

just tested it, not reproducable anymore.

indeed, doesn't crash anymore; just tested it, any use of the iterators produced by foo() will result CT error:
Error: expression 'temp()' has no type (or is ambiguous)

when true # was: defined(case3):
  # SIGSEGV
  # reduced from os.walkPattern. any change seems to remove SIGSEGV
  iterator walkPattern2*(): int =
    defer:
      var ret: int
      let ret2 = addr(ret)
    yield 1

  proc foo(): auto =
    result = iterator (): auto =
      for ai in walkPattern2():
        yield 0
    let temp = foo()
    for ai in temp():
      echo ai

but that's just a duplicate of https://github.com/nim-lang/Nim/issues/8284 so closing this was good

Was this page helpful?
0 / 5 - 0 ratings