Nim icon indicating copy to clipboard operation
Nim copied to clipboard

`nim cpp -r` invalid code generation regression with closure iterators and try/catch-like constructions

Open tersec opened this issue 1 year ago • 0 comments

Description

type K = object
  h: iterator(f: K): K

iterator d(g: K): K {.closure.} =
  defer:
    discard

discard K(h: d)
type K = object
  h: iterator(f: K): K

iterator d(g: K): K {.closure.} =
  try:
    discard
  finally:
    discard

discard K(h: d)
type K = object
  h: iterator(f: K): K

iterator d(g: K): K {.closure.} =
  try:
    discard
  except CatchableError:
    discard

discard K(h: d)

Using nim cpp -r with each.

Nim Version

No errors:

Nim Compiler Version 1.6.19 [Linux: amd64]
Compiled at 2024-02-14
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: d08bba579da7df36c51d987c04085628d81cb92f
active boot switches: -d:release

Errors:

Nim Compiler Version 2.0.3 [Linux: amd64]
Compiled at 2024-02-14
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 3be251468365250a1ad2e86bb2fa634ff7297b61
active boot switches: -d:release
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2024-02-14
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 35ec9c31bd0bd413f1740feea90f3b97ad5d1b65
active boot switches: -d:release

Current Output

For first:
.cache/nim/r0_d/@mr0.nim.cpp: In function ‘void d__r48_u5(tyObject_K__9b7r7jUF9cVZy2V0J0u3OvAw, tyObject_K__9b7r7jUF9cVZy2V0J0u3OvAw*, void*)’:
.cache/nim/r0_d/@mr0.nim.cpp:191:25: error: jump to label ‘STATE1’
  191 |                         STATE1: ;
      |                         ^~~~~~
.cache/nim/r0_d/@mr0.nim.cpp:172:38: note:   from here
  172 |                         case 1: goto STATE1;
      |                                      ^~~~~~
.cache/nim/r0_d/@mr0.nim.cpp:175:44: note:   crosses initialization of ‘std::__exception_ptr::exception_ptr T4_’
  175 |                         std::exception_ptr T4_;
      |                                            ^~~
Error: execution of an external compiler program 'g++ -c -std=gnu++17 -funsigned-char  -w -fmax-errors=3 -fpermissive -pthread   -Inimdevel/lib -I/tmp -o .cache/nim/r0_d/@mr0.nim.cpp.o .cache/nim/r0_d/@mr0.nim.cpp' failed with exit code: 1

Others are similar, error: jump to label ‘STATE1’ across std::__exception_ptr::exception_ptr boundary.

Expected Output

No invalid C++ code generation

Possible Solution

No response

Additional Information

No response

tersec avatar Feb 14 '24 15:02 tersec