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

Constant evaluation #649

Merged
merged 11 commits into from
Feb 4, 2022
Merged

Constant evaluation #649

merged 11 commits into from
Feb 4, 2022

Conversation

Y-Nak
Copy link
Member

@Y-Nak Y-Nak commented Feb 1, 2022

This PR adds some features and fixes some ICE related to constant evaluation.

Features

1. Support local constant

Example:

contract Foo:
    pub fn bar():
        const LOCAL_CONST: i32 = 1

2. Support constant expression

Example:

const GLOBAL: i32 = 8

contract Foo:
    pub fn bar():
        const LOCAL: i32 = GLOBAL * 8

3. Support constant generics expression

Example:

const GLOBAL: u256= 8
const USE_GLOBAL: bool = false
type MY_ARRAY = Array<i32, { GLOBAL / 4 }>

contract Foo:
    pub fn bar():
        let my_array: Array<i32, { GLOBAL if USE_GLOBAL else 4 }>

Bug fixes

Fix ICE when constant type is a mismatch

Example:

const GLOBAL: i32 = "FOO"

contract Foo:
    pub fn bar():
        let FOO: i32 = GLOBAL

Fix ICE when assigning value to constant twice

Example:

const BAR: i32 = 1

contract FOO:
    pub fn bar():
        BAR = 10

TODO

I opened a follow-up issue here

@Y-Nak Y-Nak marked this pull request as draft February 1, 2022 14:54
@codecov-commenter
Copy link

codecov-commenter commented Feb 1, 2022

Codecov Report

Merging #649 (64c7dfd) into master (3ec1711) will decrease coverage by 0.99%.
The diff coverage is 72.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #649      +/-   ##
==========================================
- Coverage   86.80%   85.81%   -1.00%     
==========================================
  Files          96       97       +1     
  Lines        8627     9075     +448     
==========================================
+ Hits         7489     7788     +299     
- Misses       1138     1287     +149     
Impacted Files Coverage Δ
crates/analyzer/src/db.rs 100.00% <ø> (ø)
crates/parser/src/ast.rs 88.95% <0.00%> (+0.12%) ⬆️
crates/analyzer/src/namespace/types.rs 71.18% <12.50%> (-5.97%) ⬇️
crates/analyzer/src/context.rs 65.21% <16.00%> (-14.79%) ⬇️
crates/lowering/src/ast_utils.rs 83.50% <20.00%> (-0.82%) ⬇️
crates/analyzer/src/errors.rs 92.85% <50.00%> (-7.15%) ⬇️
crates/analyzer/src/db/queries/module.rs 79.03% <51.35%> (-6.97%) ⬇️
crates/analyzer/src/namespace/scopes.rs 72.34% <66.66%> (-4.29%) ⬇️
crates/parser/src/grammar/functions.rs 90.39% <76.92%> (-1.27%) ⬇️
crates/analyzer/src/traversal/expressions.rs 81.58% <79.45%> (-0.63%) ⬇️
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ec1711...64c7dfd. Read the comment docs.

@Y-Nak Y-Nak mentioned this pull request Feb 3, 2022
4 tasks
@Y-Nak Y-Nak marked this pull request as ready for review February 3, 2022 14:21
@Y-Nak Y-Nak changed the title [WIP] Constant evaluation Constant evaluation Feb 3, 2022
@g-r-a-n-t g-r-a-n-t self-requested a review February 3, 2022 15:56
Copy link
Member

@g-r-a-n-t g-r-a-n-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work!

@Y-Nak
Copy link
Member Author

Y-Nak commented Feb 4, 2022

Fixed an invalid example in news fragments.

@g-r-a-n-t g-r-a-n-t merged commit 897f159 into ethereum:master Feb 4, 2022
@Y-Nak Y-Nak deleted the constant-evaluation branch February 4, 2022 04:53
let my_array: Array<i32, { GLOBAL if USE_GLOBAL else 4 }>
```

# Bug fixes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick but next time it would be better to create another file 649.bugfix.md because our changelog generator picks up these file name keywords to group the items accordingly. No big deal though, we can always apply changes to the changelog by hand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, Ok. I understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants