-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dereferencing operator index
[]
(#5530)
## Description This PR implements index operator `[]` for [references](https://github.com/FuelLabs/sway-rfcs/blob/ironcev/amend-references/files/0010-references.sw). The overall effort related to references is tracked in #5063. `[]` is defined for references using this recursive definition: `<reference>[<index>] := (*<reference>)[<index>]`. This eliminates the need for the dereferencing operator `*` when working with references to arrays: ```Sway let array = [1, 2, 3]; let r = &&&array; assert((***r)[0] == r[0]); ``` ```Sway let r = &&&[ &&[1, 2, 3], &&[4, 5, 6] ]; assert(r[0][2] == 3); assert(r[1][0] == 4); ``` Additionally, the PR fixes two previously existing issues (see below screenshots): - the misleading error message on type not implementing the "index" method when indexing a non-indexable type. - the broken error span and expression text when indexing a non-indexable type in reassignments. Before: ![No method named index found for type](https://github.com/FuelLabs/sway/assets/4142833/4693510d-bcb4-45ca-8f41-7988a8d87b3e) ![Not an indexable expression - Before](https://github.com/FuelLabs/sway/assets/4142833/6b8f28f0-25db-4ba8-b6a5-3d5468c70cdc) After: ![Type is not indexable](https://github.com/FuelLabs/sway/assets/4142833/df160b3b-312e-40dc-b32f-71786ee382ea) ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
21 changed files
with
778 additions
and
96 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
3 changes: 2 additions & 1 deletion
3
test/src/e2e_vm_tests/test_programs/should_fail/diverging_never/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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
category = "fail" | ||
|
||
# check: $()Type is not indexable | ||
# check: $()}[0]; | ||
# nextln: $()No method named "index" found for type "Never". | ||
# nextln: $()This expression has type "Never", which is not an indexable type. |
8 changes: 6 additions & 2 deletions
8
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
category = "fail" | ||
|
||
# check: $()"b" is a bool, which is not an indexable expression. | ||
# check: $()Type is not indexable | ||
# check: $()b[0] = true; | ||
# nextln: $()This expression has type "bool", which is not an indexable type. | ||
|
||
# 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. | ||
# check: $()Type is not indexable | ||
# check: $()my_array_2[0][1] = false; | ||
# nextln: $()This expression has type "u64", which is not an indexable type. |
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_fail/non_indexable_types/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 = "non_indexable_types" | ||
source = "member" |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/non_indexable_types/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 = "non_indexable_types" | ||
entry = "main.sw" | ||
implicit-std = false |
36 changes: 36 additions & 0 deletions
36
test/src/e2e_vm_tests/test_programs/should_fail/non_indexable_types/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,36 @@ | ||
script; | ||
|
||
struct S { | ||
x: u8, | ||
u8_field: u8, | ||
} | ||
|
||
fn main() { | ||
let mut not_array = 0; | ||
let _ = not_array[0]; | ||
not_array[0] = 1; | ||
|
||
let mut s = S { x: 0, u8_field: 0 }; | ||
let _ = s[0]; | ||
s[0] = 1; | ||
|
||
let _ = s.x[0]; | ||
s.x[0] = 1; | ||
|
||
let mut array = [s, s]; | ||
let _ = array[0].x[0]; | ||
array[0].x[0] = 1; | ||
|
||
let _= array[0].u8_field[0]; | ||
array[0].u8_field[0] = 1; | ||
|
||
let _ = array[0][0]; | ||
array[0][0] = 1; | ||
|
||
let mut tuple = (1, 2); | ||
let _ = tuple[0]; | ||
tuple[0] = 1; | ||
|
||
let _ = tuple.1[0]; | ||
tuple.1[0] = 1; | ||
} |
Oops, something went wrong.