Comparisons should support constants
When we declare a constant variable that has an initializer, e.g. bool constant x = 0 < 2, we evaluate the actual constant value in compiler time. Currently, there is no support to evaluate constants in comparisons, so the aforementioned example is evaluated in run time.
We need to calculate comparisons with constants during compilation. The eval_const_number function should become eval_const_expression to evaluate boolean operators too. This function should be called at fn var_decl in src/sema/variables.rs so that the initializer of a constant is evaluated at compile time.
Hi there! I'd like to work on this issue but I have a couple questions first: (1) From what I can tell, Solang doesn't currently support the 'constant' keyword. Is that the case, or am I missing something? If so happy to add. (2) Really cursory testing and code examination suggests that constant folding also doesn't fold expressions like "bool x = 2 < 5." I'll implement this too, unless e.g., it's not worth the compile time overhead.
Thanks so much for your help and great work, and apologies if these questions are unclear.
Hi @mlfbrown, thanks for your message!
(1) From what I can tell, Solang doesn't currently support the 'constant' keyword. Is that the case, or am I missing something? If so happy to add.
We support the constant keyword, but I mistakenly typed the wrong syntax the issue description. The proper way to use this is bool constant x = 0 < 2; I have updated the description already.
(2) Really cursory testing and code examination suggests that constant folding also doesn't fold expressions like "bool x = 2 < 5." I'll implement this too, unless e.g., it's not worth the compile time overhead.
Solidity only supports constants at contract level or outside them. The evaluation of their value is done during an AST traversal in Solang, not in constant folding. Any improvement to constant folding is welcome, and if you do so, please open a different PR because it is not related to this issue.
Great, thanks for the info.
Solidity only supports constants at contract level or outside them.
This is actually what was getting me on constant keyword support, not your syntax!
I'll definitely keep constant folding changes to a separate PR. Thanks!