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

require initialization of structs and arrays at decl site #747

Merged
merged 1 commit into from
Jul 5, 2022

Conversation

g-r-a-n-t
Copy link
Member

@g-r-a-n-t g-r-a-n-t commented Jun 22, 2022

What was wrong?

see #731

How was it fixed?

Added array repeat expressions and checks for initialization of structs and arrays.

To-Do

  • OPTIONAL: Update Spec if applicable
  • Add entry to the release notes (may forgo for trivial changes)
  • Clean up commit history

@g-r-a-n-t g-r-a-n-t changed the title hacking require initialization of structs and arrays a decl site Jun 22, 2022
@g-r-a-n-t g-r-a-n-t mentioned this pull request Jun 22, 2022
3 tasks
@g-r-a-n-t g-r-a-n-t changed the title require initialization of structs and arrays a decl site require initialization of structs and arrays at decl site Jun 22, 2022
@g-r-a-n-t g-r-a-n-t marked this pull request as draft June 22, 2022 17:10
crates/analyzer/src/traversal/expressions.rs Outdated Show resolved Hide resolved
crates/analyzer/src/traversal/expressions.rs Outdated Show resolved Hide resolved
crates/mir/src/lower/function.rs Outdated Show resolved Hide resolved
crates/parser/src/grammar/expressions.rs Outdated Show resolved Hide resolved
@g-r-a-n-t g-r-a-n-t marked this pull request as ready for review July 1, 2022 03:55
@g-r-a-n-t g-r-a-n-t force-pushed the var-init-check branch 2 times, most recently from b909d29 to 363b32b Compare July 1, 2022 21:02
@g-r-a-n-t g-r-a-n-t requested review from Y-Nak and cburgdorf July 1, 2022 21:36
Copy link
Collaborator

@cburgdorf cburgdorf left a comment

Choose a reason for hiding this comment

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

Left some inline comments. Most important: Can you confirm we don't need that for tuple types? Wouldn't something like let val: (u8, u8, address) run into the same issue?

crates/analyzer/src/traversal/declarations.rs Outdated Show resolved Hide resolved
@@ -264,7 +264,7 @@ note:
┌─ aug_assign.fe:65:5
65 │ ╭ pub fn add_from_mem(a: u256, b: u256) -> u256 {
66 │ │ let my_array: Array<u256, 10>
66 │ │ let my_array: Array<u256, 10> = [0; 10]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding this repeat expression feels a bit like a crutch to me. It doesn't seem overly useful for the user since it only allows a single value to be repeated. Did you consider to just initialize the array with default values on behalf of the user. Maybe that gets complicated if the array is over a complex type such as Array<Customer, 10>. However, in the future we could have a Default trait and if a user wants to initialize an array of Array<Customer, 10> then it will automatically use the values provided by Customer::default() or else error if Customer doesn't implement the Default trait. I'm just thinking out loud here...I'm not really against introducing the repeat expression but if in the future we find a way to get rid of it again or make it more useful I wouldn't be sad to see it go 😄

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I just learned that one can use the same approach to initialize fixed size arrays in Rust. So, I guess it's fine. I still think that in the future it could be nice syntactical sugar if one could just write let x: Array<Customer, 10> and it would de-sugar into something like let x: Array<Customer, 10> = [Customer::default(), 10]. And if T does not implement the Default trait it would cause a compile error.

@g-r-a-n-t g-r-a-n-t merged commit 0c2d86b into ethereum:master Jul 5, 2022
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.

3 participants