leo_passes/static_analysis/await_checker.rs
1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::ConditionalTreeNode;
18use indexmap::IndexSet;
19use leo_errors::StaticAnalyzerWarning;
20use leo_span::{Span, Symbol};
21
22// TODO: Could optimize by removing duplicate paths (if set of futures is the same).
23pub struct AwaitChecker {
24 /// All possible subsets of futures that must be awaited.
25 pub(crate) to_await: Vec<ConditionalTreeNode>,
26 /// Statically updated set of futures to await.
27 pub(crate) static_to_await: IndexSet<Symbol>,
28}
29
30impl AwaitChecker {
31 /// Initializes a new `AwaitChecker`.
32 pub fn new() -> Self {
33 Self { to_await: Vec::new(), static_to_await: IndexSet::new() }
34 }
35
36 /// Remove from list.
37 /// Returns `true` if there was a path where the future was not awaited in the order of the input list.
38 pub fn remove(&mut self, name: &Symbol) -> bool {
39 // Can assume in finalize block.
40 // Remove from dynamic list.
41 let is_not_first = self.to_await.iter_mut().any(|node| node.remove_element(name));
42
43 // Remove from static list.
44 self.static_to_await.shift_remove(name);
45
46 is_not_first
47 }
48
49 /// Initialize futures.
50 pub fn set_futures(&mut self, futures: IndexSet<Symbol>) {
51 if futures.is_empty() {
52 self.to_await = Vec::new();
53 } else {
54 self.to_await = vec![ConditionalTreeNode::new(futures.clone())];
55 }
56 self.static_to_await = futures;
57 }
58
59 /// Enter scope for `then` branch of conditional.
60 pub fn create_then_scope(
61 &mut self,
62 is_finalize: bool,
63 _input: Span,
64 ) -> Result<Vec<ConditionalTreeNode>, StaticAnalyzerWarning> {
65 if is_finalize {
66 let mut current_nodes = Vec::new();
67 // Extend all paths by one node to represent the upcoming `then` branch.
68 for node in self.to_await.iter() {
69 // Extend current path.
70 current_nodes.push(node.clone().create_child());
71 }
72 // Update the set of nodes to be current set.
73 self.to_await = current_nodes.clone();
74 Ok(current_nodes)
75 } else {
76 Ok(Vec::new())
77 }
78 }
79
80 /// Exit scope for `then` branch of conditional.
81 pub fn exit_then_scope(
82 &mut self,
83 is_finalize: bool,
84 parent_nodes: Vec<ConditionalTreeNode>,
85 ) -> Vec<ConditionalTreeNode> {
86 // Check if a nested conditional statement signaled their existence.
87 if is_finalize { core::mem::replace(&mut self.to_await, parent_nodes) } else { Vec::new() }
88 }
89
90 /// Exit scope for conditional statement at current depth.
91 pub fn exit_statement_scope(&mut self, is_finalize: bool, then_nodes: Vec<ConditionalTreeNode>) {
92 if is_finalize {
93 // Merge together the current set of nodes (from `otherwise` branch) with `then` nodes.
94 self.to_await.extend(then_nodes);
95 }
96 }
97}