Skip to content

Commit

Permalink
Bind fixed-size arrays to a different built-in type without push/pop
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 23, 2024
1 parent 1756ae0 commit c743c01
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 11 deletions.
12 changes: 10 additions & 2 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -842,14 +842,22 @@ inherit .lexical_scope
node @array.output
}

@array [ArrayTypeName [TypeName] index: [Expression]] {
let @array.type = "%arrayFixed"
}

@array [ArrayTypeName [OpenBracket] . [CloseBracket]] {
let @array.type = "%array"
}

@array [ArrayTypeName @type_name [TypeName]] {
; First define the normal, reference route:

; We first push the array type `%array`, which should connect to two distinct paths:
; 1. the typed path, which will use a jump scope entry to resolve the element type
; 2. the hard-coded path to connect to any `using` directive
node array
attr (array) push_symbol = "%array"
attr (array) push_symbol = @array.type
edge @array.output -> array

; For the first path, we need to define a scope jump entry for resolving the element type of the array
Expand All @@ -875,7 +883,7 @@ inherit .lexical_scope
; Now we define the "definition" route (aka. the pop route), to use in `using` directives only
; This is essentially the reverse of the second path above
node pop_array
attr (pop_array) pop_symbol = "%array"
attr (pop_array) pop_symbol = @array.type

let @array.pop_begin = @type_name.pop_begin
edge @type_name.pop_end -> pop_array
Expand Down
9 changes: 9 additions & 0 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6849,6 +6849,15 @@ codegen_language_macros::compile!(Language(
BuiltInFunction(name = "pop", parameters = [])
]
),
BuiltInType(
name = "$arrayFixed",
fields = [BuiltInField(definition = "uint length")],
functions = [BuiltInFunction(
name = "$index",
parameters = ["uint"],
return_type = "$element"
)]
),
BuiltInType(
name = "$blockType",
fields = [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract Test {
function test() public {
uint[5] memory values;
values.pop();
// ^ref:! -- fixed size arrays should not bind pop/push
values.push(1);
// ^ref:! -- fixed size arrays should not bind pop/push
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ References and definitions:
3 │ function testArray() public {
│ ────┬────
│ ╰────── def: 3
4 │ uint[] memory b = new uint[](5);
│ ┬
│ ╰── def: 4
4 │ uint[] storage b = new uint[](5);
╰── def: 4
5 │ assert(b.length == 5);
│ ───┬── ┬ ───┬──
│ ╰───────────── ref: built-in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ References and definitions:
3 │ function testArray() public {
│ ────┬────
│ ╰────── def: 3
4 │ uint[] memory b = new uint[](5);
│ ┬
│ ╰── def: 4
4 │ uint[] storage b = new uint[](5);
╰── def: 4
5 │ assert(b.length == 5);
│ ───┬── ┬ ───┬──
│ ╰───────────── ref: built-in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract Test {
uint[] a;
function testArray() public {
uint[] memory b = new uint[](5);
uint[] storage b = new uint[](5);
assert(b.length == 5);

a.push();
Expand Down

0 comments on commit c743c01

Please sign in to comment.