-
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.
Fix side effects in
if-let
expressions (#5215)
## Description This PR fixes the critical issue in `if-let`s: #5173 `if-let`s were desugared to `match` expressions but used their own implementation for desugaring which wasn't storing the result of the matched value into a compiler generated variable but instead re-evaluated it on every access, causing side effects. The new implementation generates exactly the same desugaring as the equivalent `match` expression would have, simply by forwarding the `if-let` desugaring to `match` desugaring. Since the generated IR is different, the IR gen tests needed to be adapted as well. Also, a single assert is commented out in the `go_to_definition_for_matches` LSP test until #5221 is solved. Closes #5173. ## 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
10 changed files
with
163 additions
and
70 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
2 changes: 2 additions & 0 deletions
2
test/src/e2e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/.gitignore
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 @@ | ||
out | ||
target |
16 changes: 16 additions & 0 deletions
16
test/src/e2e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/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,16 @@ | ||
[[package]] | ||
name = "core" | ||
source = "path+from-root-AF27191E41C715AD" | ||
|
||
[[package]] | ||
name = "if_let_no_side_effects" | ||
source = "member" | ||
dependencies = [ | ||
"core", | ||
"std", | ||
] | ||
|
||
[[package]] | ||
name = "std" | ||
source = "path+from-root-AF27191E41C715AD" | ||
dependencies = ["core"] |
9 changes: 9 additions & 0 deletions
9
test/src/e2e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/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 <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "if_let_no_side_effects" | ||
|
||
[dependencies] | ||
core = { path = "../../../../../../../sway-lib-core" } | ||
std = { path = "../../../../../../../sway-lib-std" } |
25 changes: 25 additions & 0 deletions
25
...e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/json_abi_oracle.json
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,25 @@ | ||
{ | ||
"configurables": [], | ||
"functions": [ | ||
{ | ||
"attributes": null, | ||
"inputs": [], | ||
"name": "main", | ||
"output": { | ||
"name": "", | ||
"type": 0, | ||
"typeArguments": null | ||
} | ||
} | ||
], | ||
"loggedTypes": [], | ||
"messagesTypes": [], | ||
"types": [ | ||
{ | ||
"components": null, | ||
"type": "u64", | ||
"typeId": 0, | ||
"typeParameters": null | ||
} | ||
] | ||
} |
26 changes: 26 additions & 0 deletions
26
test/src/e2e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/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,26 @@ | ||
// Test that proves that this issue is fixed: https://github.com/FuelLabs/sway/issues/5173 | ||
script; | ||
|
||
struct Struct { | ||
x: u64, | ||
y: u64, | ||
z: u64 | ||
} | ||
|
||
fn test_inc(ref mut i: u64) -> Struct { | ||
i = i + 11; | ||
|
||
Struct { x: 1111, y: 2222, z: 333 } | ||
} | ||
|
||
fn main() -> u64 { | ||
let mut i = 0; | ||
|
||
if let Struct { x, y, z: 0 } = test_inc(i) { | ||
let a = x + y; | ||
assert(a == 3333); | ||
}; | ||
assert(i == 11); | ||
|
||
i | ||
} |
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_pass/language/if_let_no_side_effects/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,3 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 11 } | ||
validate_abi = true |
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