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

Invalid C code for inline iterator with inner proc #14165

Closed
DomoSokrat opened this issue Apr 29, 2020 · 6 comments
Closed

Invalid C code for inline iterator with inner proc #14165

DomoSokrat opened this issue Apr 29, 2020 · 6 comments

Comments

@DomoSokrat
Copy link

While trying out the different nim solutions to the Hamming numbers task from RosettaCode I found that one variant did not compile. Reducing it to the max gave me this:

iterator log_nodups_hamming(): int {.inline.} =
  let lb3 = 1
  proc mul3(): int = lb3
  yield mul3()

for h in log_nodups_hamming():
  break
for h in log_nodups_hamming():
  break

Output

/mnt/space/data/nim/.cache/hn_5_test/d/@mhn_5_test.nim.c: In function ‘NimMainModule’:
/mnt/space/data/nim/.cache/hn_5_test/d/@mhn_5_test.nim.c:214:5: error: ‘colonenv_’ undeclared (first use in this function); did you mean ‘colonenv__2’?
  214 |   (*colonenv_).lb31 = ((NI) 1);
      |     ^~~~~~~~~
      |     colonenv__2
/mnt/space/data/nim/.cache/hn_5_test/d/@mhn_5_test.nim.c:214:5: note: each undeclared identifier is reported only once for each function it appears in

This is the expansion of the second loop.

Additional Information

Used versions

$ nim -v
Nim Compiler Version 1.2.0 [Linux: amd64]
Compiled at 2020-04-03
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release
  • The 1.2.1 nightly from 28.4. and the 1.3.1 nightly from 29.4. show the same problem.
@alaviss
Copy link
Collaborator

alaviss commented Apr 30, 2020

Regression happened between 0.19.0 and 0.20.0. I did two bisect run and got different results:

@ghost
Copy link

ghost commented Aug 23, 2021

@timotheecour your #18536 seems to be a duplicate of this issue, can you please decide which one of the two to close?

@timotheecour
Copy link
Member

ok, closing the other one in favor of this one; there is some complementary information so I'm pasting the issue here:


reduced from #18535 (comment) /cc @brightly-salty

Example

# iterator envPairs*(): int {.closure.} = # Error: internal error: environment misses: foo
iterator envPairs*(): int =
  var foo: seq[int]
  proc fun() =
    foo = @[]
  fun()
  yield 3

proc main() =
  for a in envPairs():
    discard
  for a in envPairs():
    discard
static: main() # nim c in vm: ok
# static: main() # nim js in vm: Error: cannot evaluate at compile time: foo
main() # nim c: @mt12609.nim.c:372:5: error: use of undeclared identifier 'colonenv_'; did you mean 'colonenv__2'?
# main() # nim js: # Error: internal error: symbol has no generated name: foo

Current Output

/@mt12609.nim.c:372:5: error: use of undeclared identifier 'colonenv_'; did you mean 'colonenv__2'?
                (*colonenv_).foo1 = (tySequence__qwqHTkRvwhrRyENtudHQ7g*)0;
                  ^~~~~~~~~
                  colonenv__2
/Users/timothee/git_clone/nim/timn/build/nimcache/@mt12609.nim.c:364:65: note: 'colonenv__2' declared here
                tyObject_Env_t12609dotnim_envPairs___Dwg4Sd9awieN34NfR6DSgaw* colonenv__2;
                                                                              ^
/Users/timothee/git_clone/nim/timn/build/nimcache/@mt12609.nim.c:375:46: error: use of undeclared identifier 'colonenv_'; did you mean 'colonenv__2'?
                T4_.ClP_0 = fun_t4950544857_3; T4_.ClE_0 = colonenv_;

Expected Output

works

Additional Information

1.5.1 73ce40a

@Cloudperry
Copy link

I'm not sure, but I think I ran into this same bug using an anonymous proc passed to another proc. The error message is exactly the same except for the names. I will post an example here if I manage to get it minimized, but its hard because my bug happened with regex-nim and removing regex-nim usage in my program makes it compile.

By the way the original example posted here now doesn't compile with arc on Nim 1.4.8.

@ringabout
Copy link
Member

Hi @Cloudperry, could you post your example?

@Cloudperry
Copy link

Unfortunately it happened in one of my super small projects and I didn't have the code in any version control system. I got rid of that code because I thought that an example with regex-nim wouldn't be that useful. I tried to make the same program again from what I remember but I didn't manage to reproduce it.

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

Successfully merging a pull request may close this issue.

6 participants