Skip to content

Commit

Permalink
No limit on the depth of the return path analysis (#6435)
Browse files Browse the repository at this point in the history
## Description

Fixes #6369.

The return path analysis performs a breadth-first traversal of the
control flow graph to determine whether every path returns a value.

So far the traversal has been limited to 50 iterations of the outer
while loop, meaning that if the graph has a depth of more than 50 there
are paths that would not be checked. The limit was introduced to ensure
that that the analysis terminated.

This PR removes this limit. Termination is ensured by keeping track of
which nodes have already been visited so that they are not visited
again.


## 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: Sophie Dankel <[email protected]>
  • Loading branch information
jjcnn and sdankel authored Aug 20, 2024
1 parent 169f91a commit ddad3aa
Show file tree
Hide file tree
Showing 6 changed files with 561 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sway-core/src/control_flow_analysis/analyze_return_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ impl<'cfg> ControlFlowGraph<'cfg> {
return_ty: &TypeInfo,
) -> Vec<CompileError> {
let mut rovers = vec![entry_point];
let mut visited = vec![];
let mut errors = vec![];
let mut max_iterations = 50;
while !rovers.is_empty() && rovers[0] != exit_point && max_iterations > 0 {
max_iterations -= 1;
while !rovers.is_empty() && rovers[0] != exit_point {
rovers.retain(|idx| *idx != exit_point);
let mut next_rovers = vec![];
let mut last_discovered_span;
for rover in rovers {
visited.push(rover);
last_discovered_span = match &self.graph[rover] {
ControlFlowGraphNode::ProgramNode { node, .. } => Some(node.span.clone()),
ControlFlowGraphNode::MethodDeclaration { span, .. } => Some(span.clone()),
Expand Down Expand Up @@ -122,6 +122,7 @@ impl<'cfg> ControlFlowGraph<'cfg> {
}
next_rovers.append(&mut neighbors);
}
next_rovers.retain(|idx| !visited.contains(idx));
rovers = next_rovers;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[package]]
name = "core"
source = "path+from-root-EA042F5351E6BA99"

[[package]]
name = "return_path_analysis"
source = "member"
dependencies = [
"core",
"std",
]

[[package]]
name = "std"
source = "path+from-root-EA042F5351E6BA99"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "return_path_analysis"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
std = { path = "../../../reduced_std_libs/sway-lib-std-assert" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit ddad3aa

Please sign in to comment.