Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparisons should support constants #755

Open
LucasSte opened this issue Apr 20, 2022 · 5 comments · May be fixed by #1650
Open

Comparisons should support constants #755

LucasSte opened this issue Apr 20, 2022 · 5 comments · May be fixed by #1650
Labels
good first issue Good for newcomers

Comments

@LucasSte
Copy link
Contributor

LucasSte commented Apr 20, 2022

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.

@LucasSte LucasSte added the good first issue Good for newcomers label Apr 20, 2022
@mlfbrown
Copy link

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.

@LucasSte
Copy link
Contributor Author

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.

@mlfbrown
Copy link

mlfbrown commented Jun 30, 2022

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!

@Nirhar
Copy link

Nirhar commented May 20, 2024

I'd like to work on this issue! If I understand correctly, the goal is to transform statements like bool constant x = 0 < 2 to bool constant x = true during AST parsing, am I correct?

@seanyoung
Copy link
Contributor

I'd like to work on this issue! If I understand correctly, the goal is to transform statements like bool constant x = 0 < 2 to bool constant x = true during AST parsing, am I correct?

Not exactly during parsing. The AST should contain the original expression as it appears in the source code. There is a function eval_const_expression() which evaluates constant expressions. This is where it should be added.

Nirhar added a commit to Nirhar/solang that referenced this issue Jun 7, 2024
…tion

This patch supports evaluation of comparison expressions with
constants as operands during compile time.

Fixes hyperledger-solang#755
Nirhar added a commit to Nirhar/solang that referenced this issue Jun 7, 2024
…tion

This patch supports evaluation of comparison expressions with
constants as operands during compile time.

Fixes hyperledger-solang#755

Signed-off-by: Nirhar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants