forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for mutable arrays. (FuelLabs#3153)
This adds support for reassigning array elements. We now support this by extending reassignment `ProjectionKind` with a new `ArrayIndex` variant which keeps track of the typed array index reassignnment during semantic analysis. Afterwards we implement this new projection in IR generation by lowering to the `insert_element` IR instruction which updates the array element value. Closes FuelLabs#2457. Co-authored-by: Mohammad Fawaz <[email protected]>
- Loading branch information
1 parent
e5dc94e
commit 22db07e
Showing
44 changed files
with
377 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_fail/mutable_arrays/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = 'mutable_arrays' | ||
source = 'member' |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/mutable_arrays/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "mutable_arrays" | ||
entry = "main.sw" | ||
implicit-std = false |
21 changes: 21 additions & 0 deletions
21
test/src/e2e_vm_tests/test_programs/should_fail/mutable_arrays/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
script; | ||
|
||
fn main() -> bool { | ||
let mut b = false; | ||
b[0] = true; | ||
|
||
let my_array: [u64; 1] = [1]; | ||
my_array[0] = 0; | ||
|
||
takes_ref_mut_arr(my_array); | ||
|
||
let mut my_array_2: [u64; 1] = [1]; | ||
my_array_2[0] = false; | ||
my_array_2[0][1] = false; | ||
|
||
false | ||
} | ||
|
||
fn takes_ref_mut_arr(ref mut arr: [u64; 1]) { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
test/src/e2e_vm_tests/test_programs/should_fail/mutable_arrays/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
category = "fail" | ||
|
||
# check: $()"b" is a bool, which is not an indexable expression. | ||
|
||
# check: $()Assignment to immutable variable. Variable my_array is not declared as mutable. | ||
|
||
# check: $()Cannot pass immutable argument to mutable parameter. | ||
|
||
# check: $()Mismatched types. | ||
|
||
# check: $()"my_array_2" is a u64, which is not an indexable expression. |
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = 'mutable_arrays' | ||
source = 'member' |
7 changes: 7 additions & 0 deletions
7
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "mutable_arrays" | ||
entry = "main.sw" | ||
implicit-std = false | ||
|
7 changes: 7 additions & 0 deletions
7
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
script; | ||
|
||
fn main() -> u64 { | ||
let mut my_array: [u64; 1] = [1]; | ||
my_array[0] = 10; | ||
my_array[0] | ||
} |
2 changes: 2 additions & 0 deletions
2
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 10 } |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays_enum/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-B216E59DF5F0BD4C' | ||
|
||
[[package]] | ||
name = 'mutable_arrays_enum' | ||
source = 'member' | ||
dependencies = ['core'] |
9 changes: 9 additions & 0 deletions
9
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays_enum/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "mutable_arrays_enum" | ||
entry = "main.sw" | ||
|
||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } |
17 changes: 17 additions & 0 deletions
17
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays_enum/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
script; | ||
|
||
struct X { | ||
value: u64 | ||
} | ||
|
||
enum Foo { | ||
Bar: X, | ||
} | ||
|
||
fn main() -> u64 { | ||
let mut my_array: [Foo; 1] = [Foo::Bar(X{value: 10})]; | ||
my_array[0] = Foo::Bar(X{value: 20}); | ||
match my_array[0] { | ||
Foo::Bar(x) => x.value, | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
test/src/e2e_vm_tests/test_programs/should_pass/language/mutable_arrays_enum/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 20 } |
3 changes: 3 additions & 0 deletions
3
.../e2e_vm_tests/test_programs/should_pass/language/mutable_arrays_multiple_nested/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = 'mutable_arrays_multiple_nested' | ||
source = 'member' |
Oops, something went wrong.