nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.62k stars 1.47k forks source link

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

Open japplegame opened 3 years ago

japplegame commented 3 years ago

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)

metagn commented 3 years ago

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

Araq commented 3 years ago

No, const should work too.

japplegame commented 3 years ago

@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.

PMunch commented 3 years ago

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

auxym commented 2 years ago

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