Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Invalid C code generation with `iterator` with `when nimvm`: `error: ‘c__r_u3’ undeclared (first use in this function); did you mean ‘c__r_u10’`

Open tersec opened this issue 4 months ago • 3 comments

Nim Version

Nim Compiler Version 2.2.4 [Linux: amd64]
Compiled at 2025-08-25
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: f7145dd26efeeeb6eeae6fff649db244d81b212d
active boot switches: -d:release
Nim Compiler Version 2.2.5 [Linux: amd64]
Compiled at 2025-08-25
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: c339651ae1d6af6c84f5489f6fe1fdea44ba1fa1
active boot switches: -d:release
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-08-25
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: b527db9ddd33fd16a8afd8467344fec81a54c84d
active boot switches: -d:release
gcc (Debian 14.3.0-5) 14.3.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Description

iterator v(): int =
  when nimvm:
    yield 0
  else:
    yield 0
for _ in v():
  for c in v():
    (; let _: proc() = proc() = discard c)

Current Output

.....................................................................
CC: system/exceptions.nim
CC: std/private/digitsutils.nim
CC: system/dollars.nim
CC: system.nim
CC: r.nim
/tmp/tmp.YWdRaJqN4e/@mr.nim.c: In function ‘colonanonymous___r_u5’:
/tmp/tmp.YWdRaJqN4e/@mr.nim.c:82:7: error: ‘c__r_u3’ undeclared (first use in this function); did you mean ‘c__r_u10’?
   82 | (void)c__r_u3;
      |       ^~~~~~~
      |       c__r_u10
/tmp/tmp.YWdRaJqN4e/@mr.nim.c:82:7: note: each undeclared identifier is reported only once for each function it appears in

Expected Output

No invalid C code generated

Known Workarounds

No response

Additional Information

No response

tersec avatar Aug 26 '25 05:08 tersec

Workaround:

import std/sugar

iterator v(): int =
  when nimvm:
    yield 0
  else:
    yield 0
for _ in v():
  for c in v():
    capture c:
      (; let _: proc() = proc() = discard c)

ringabout avatar Oct 24 '25 14:10 ringabout

Workaround:

import std/sugar

iterator v(): int = when nimvm: yield 0 else: yield 0 for _ in v(): for c in v(): capture c: (; let _: proc() = proc() = discard c)

Well, the general point is that it's presumptively always a Nim bug if pure Nim code generates invalid C. If for whatever reason it's not possible to generate correct C, then the Nim compiler should reject that code in a more intentional way, and it should be defined as invalid Nim code. Conversely, if it is valid Nim code, it should compile correctly.

tersec avatar Oct 24 '25 14:10 tersec

related to https://github.com/nim-lang/Nim/issues/15353 #12375

ringabout avatar Oct 27 '25 14:10 ringabout