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

C codegen error (error: ‘colonenv_’ undeclared) when using same iterator twice #18739

Closed
arkanoid87 opened this issue Aug 23, 2021 · 2 comments

Comments

@arkanoid87
Copy link
Contributor

C codegen error (error: ‘colonenv_’ undeclared) when using same iterator twice

happens both in stable & devel

Example

comment last line to compile successfully

https://play.nim-lang.org/#ix=3wLt

import std/[
  sequtils
]

type
  VisitOrder = enum
    voLeaf,
    voRoot
  MyNode = object
    name: string
    children: seq[MyNode]
    visited: bool


iterator walk(node: MyNode, visitOrder = voLeaf): MyNode =
  ## Walk tree, level order, direction by visitOrder parameter

  var resultSeq = newSeq[MyNode]()

  func callback(node: MyNode) =
    resultSeq.add node

  func visit(node: var MyNode) =
    callback(node)
    node.visited = true

  func resetVisited(node: var MyNode) =
    node.visited = false
    for c in node.children.mitems:
      c.resetVisited

  func allVisited(node: MyNode): bool =
    result = node.visited
    for c in node.children:
      result = result and c.allVisited

  func leafOrder(node: var MyNode) =
    if not node.visited and node.children.allIt(it.visited): # is leaf
      node.visit
    else:
      for c in node.children.mitems:
        c.leafOrder

  func rootOrder(node: var MyNode) =
    func aux(node: var MyNode) =
      for c in node.children.mitems:
        c.visit
      for c in node.children.mitems:
        c.aux
    node.visit
    node.aux

  var mutableMyNode = node
  mutableMyNode.resetVisited
  while not mutableMyNode.allVisited:
    case visitOrder
    of voLeaf: mutableMyNode.leafOrder
    of voRoot: mutableMyNode.rootOrder
  
  for n in resultSeq:
    yield n


if isMainModule:
  let root = MyNode(
    name: "A1",
    children: @[
      MyNode(
        name: "B1",
        children: @[
          MyNode(name: "C1")
        ]
      ),
      MyNode(
        name: "B2",
        children: @[
          MyNode(name: "C2"),
          MyNode(
            name: "C3",
            children: @[
              MyNode(name: "D1")
            ]
          )
        ]
      )
    ]
  )

  for n in root.walk(voLeaf): echo n
  for n in root.walk(voLeaf): echo n # commenting this line avoids C codegen error

Current Output

CC: experiments.nim
/home/arkanoid/snap/codium/common/.cache/nim/experiments_d/@mexperiments.nim.c: In function ‘NimMainModule’:
/home/arkanoid/snap/codium/common/.cache/nim/experiments_d/@mexperiments.nim.c:1417:25: error: ‘colonenv_’ undeclared (first use in this function); did you mean ‘colonenv__2’?
 1417 |    asgnRef((void**) (&(*colonenv_).resultSeq1), newSeq_experiments_19(((NI) 0)));
      |                         ^~~~~~~~~
      |                         colonenv__2
/home/arkanoid/snap/codium/common/.cache/nim/experiments_d/@mexperiments.nim.c:1417:25: note: each undeclared identifier is reported only once for each function it appears in
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3   -I'/home/arkanoid/.choosenim/toolchains/nim-#devel/lib' -I/home/arkanoid/nim/src -o /home/arkanoid/snap/codium/common/.cache/nim/experiments_d/@mexperiments.nim.c.o /home/arkanoid/snap/codium/common/.cache/nim/experiments_d/@mexperiments.nim.c' failed with exit code: 1

Expected Output

compilation successful
nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-08-23
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 2d84f6e7c836bf18c0ecbe07fb413166861196ac
active boot switches: -d:release
@ghost
Copy link

ghost commented Aug 23, 2021

might be a duplicate of #14165

@ringabout
Copy link
Member

might be a duplicate of #14165

Indeed

After fixing #14165, the program gives

(name: "C1", children: @[], visited: false)
(name: "C2", children: @[], visited: false)
(name: "D1", children: @[], visited: false)
(name: "B1", children: @[(name: "C1", children: @[], visited: true)], visited: false)
(name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: false)
(name: "B2", children: @[(name: "C2", children: @[], visited: true), (name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: true)], visited: false)
(name: "A1", children: @[(name: "B1", children: @[(name: "C1", children: @[], visited: true)], visited: true), (name: "B2", children: @[(name: "C2", children: @[], visited: true), (name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: true)], visited: true)], visited: false)
(name: "C1", children: @[], visited: false)
(name: "C2", children: @[], visited: false)
(name: "D1", children: @[], visited: false)
(name: "B1", children: @[(name: "C1", children: @[], visited: true)], visited: false)
(name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: false)
(name: "B2", children: @[(name: "C2", children: @[], visited: true), (name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: true)], visited: false)
(name: "A1", children: @[(name: "B1", children: @[(name: "C1", children: @[], visited: true)], visited: true), (name: "B2", children: @[(name: "C2", children: @[], visited: true), (name: "C3", children: @[(name: "D1", children: @[], visited: true)], visited: true)], visited: true)], visited: false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants