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_ast::Identifier;
20use leo_errors::StaticAnalyzerWarning;
21use leo_span::{Span, Symbol};
22
23// TODO: Could optimize by removing duplicate paths (if set of futures is the same).
24pub struct AwaitChecker {
25 /// All possible subsets of futures that must be awaited.
26 pub(crate) to_await: Vec<ConditionalTreeNode>,
27 /// Statically updated set of futures to await.
28 pub(crate) static_to_await: IndexSet<Symbol>,
29}
30
31impl AwaitChecker {
32 /// Initializes a new `AwaitChecker`.
33 pub fn new() -> Self {
34 Self { to_await: Vec::new(), static_to_await: IndexSet::new() }
35 }
36
37 /// Remove from list.
38 /// Returns `true` if there was a path where the future was not awaited in the order of the input list.
39 pub fn remove(&mut self, id: &Identifier) -> bool {
40 // Can assume in finalize block.
41 // Remove from dynamic list.
42 let is_not_first = self.to_await.iter_mut().any(|node| node.remove_element(&id.name));
43
44 // Remove from static list.
45 self.static_to_await.shift_remove(&id.name);
46
47 is_not_first
48 }
49
50 /// Initialize futures.
51 pub fn set_futures(&mut self, futures: IndexSet<Symbol>) {
52 if futures.is_empty() {
53 self.to_await = Vec::new();
54 } else {
55 self.to_await = vec![ConditionalTreeNode::new(futures.clone())];
56 }
57 self.static_to_await = futures;
58 }
59
60 /// Enter scope for `then` branch of conditional.
61 pub fn create_then_scope(
62 &mut self,
63 is_finalize: bool,
64 _input: Span,
65 ) -> Result<Vec<ConditionalTreeNode>, StaticAnalyzerWarning> {
66 if is_finalize {
67 let mut current_nodes = Vec::new();
68 // Extend all paths by one node to represent the upcoming `then` branch.
69 for node in self.to_await.iter() {
70 // Extend current path.
71 current_nodes.push(node.clone().create_child());
72 }
73 // Update the set of nodes to be current set.
74 self.to_await = current_nodes.clone();
75 Ok(current_nodes)
76 } else {
77 Ok(Vec::new())
78 }
79 }
80
81 /// Exit scope for `then` branch of conditional.
82 pub fn exit_then_scope(
83 &mut self,
84 is_finalize: bool,
85 parent_nodes: Vec<ConditionalTreeNode>,
86 ) -> Vec<ConditionalTreeNode> {
87 // Check if a nested conditional statement signaled their existence.
88 if is_finalize { core::mem::replace(&mut self.to_await, parent_nodes) } else { Vec::new() }
89 }
90
91 /// Exit scope for conditional statement at current depth.
92 pub fn exit_statement_scope(&mut self, is_finalize: bool, then_nodes: Vec<ConditionalTreeNode>) {
93 if is_finalize {
94 // Merge together the current set of nodes (from `otherwise` branch) with `then` nodes.
95 self.to_await.extend(then_nodes);
96 }
97 }
98}