Nim icon indicating copy to clipboard operation
Nim copied to clipboard

The compiler optimizes out unused constants marked with the `exportc` pragma

Open japplegame opened this issue 3 years ago • 5 comments

The compiler must never optimize out constants marked with the expotrc pragma. For example:

const foo {. exportc .} = 10

Expected C output:

N_LIB_PRIVATE NIM_CONST NI foo = ((NI) 10);

Current output: Nothing (optimized out)

japplegame avatar Apr 08 '21 22:04 japplegame

This isn't optimized out, it's ignored. exportc should error on const. You should be using let.

metagn avatar Apr 09 '21 06:04 metagn

No, const should work too.

Araq avatar Apr 09 '21 06:04 Araq

@hlaaftana, look in the corresponding thread: https://forum.nim-lang.org/t/7763 let does not guarantee that the initializing value will be evaluated at compile time and can be successfully placed in the`rodata ' section.

japplegame avatar Apr 09 '21 07:04 japplegame

If exportc should work for const then I would say codegenDecl should as well. Would probably solve my gripes with https://github.com/nim-lang/Nim/issues/17497

PMunch avatar Apr 09 '21 07:04 PMunch

Adding on, if exportc should work on consts, then should we also be able to take the addr of a const, or at least an exported const? If so this is related to this RFC: https://github.com/nim-lang/RFCs/issues/257

auxym avatar Sep 09 '22 16:09 auxym