-
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.
Simplify the return path analysis (#6541)
## Description The return path analysis ensures that if a method must return a value then the spine of the method returns a value. The analysis traverses a linearized version of the CFG (one which ignores branching expressions such as `if-then-else` and loops, and simply treats them as a single node) of each method from the (unique) entry point to the (unique) exit point. If there is ever a node in the graph that does not have outgoing edges, then an error is thrown. When constructing such a linearized CFG we have so far treated local `impl`s as branches, meaning that the spine was in fact not linear: ``` fn main() -> u64 { // This point has two outgoing edges // One goes to here impl core::ops::Eq for X { fn eq(self, other: Self) -> bool { asm(r1: self, r2: other, r3) { eq r3 r2 r1; r3: bool } } } // The other goes to here if X::Y(true) == X::Y(true) { a } else { a } } ``` This has been the case even though the edge into a local `impl` does not in fact represent legal control flow. This incorrect construction has made the traversal of the spine-CFG very convoluted and difficult to follow. This PR fixes the incorrect construction, and simplifies the traversal of the spine so that it is clear what is going on. I have also added an additional test to show that methods inside local `impl`s are in fact analyzed separately, even though there is no longer an edge going into the `impl` from the surrounding function. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] 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. --------- Co-authored-by: IGI-111 <[email protected]> Co-authored-by: Joshua Batty <[email protected]> Co-authored-by: João Matos <[email protected]>
- Loading branch information
1 parent
be70774
commit c374a11
Showing
3 changed files
with
110 additions
and
72 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
5 changes: 4 additions & 1 deletion
5
test/src/e2e_vm_tests/test_programs/should_fail/return_path_analysis/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,6 +1,9 @@ | ||
category = "fail" | ||
|
||
# check: $()Declaring nested functions is currently not implemented. | ||
# check: $()Could not find symbol "tester" in this scope. | ||
# check: $()This path must return a value of type "bool" from function "f", but it does not. | ||
# check: $()Aborting due to 1 error. | ||
# check: $()This path must return a value of type "bool" from function "eq", but it does not. | ||
# check: $()Aborting due to 4 errors. | ||
|
||
|