leo_passes/static_analysis/
statement.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 super::StaticAnalyzingVisitor;
18use crate::ConditionalTreeNode;
19
20use leo_ast::*;
21
22impl StatementVisitor for StaticAnalyzingVisitor<'_> {
23    fn visit_conditional(&mut self, input: &ConditionalStatement) {
24        self.visit_expression(&input.condition, &Default::default());
25
26        // Create scope for checking awaits in `then` branch of conditional.
27        let current_bst_nodes: Vec<ConditionalTreeNode> =
28            match self.await_checker.create_then_scope(self.variant == Some(Variant::AsyncFunction), input.span) {
29                Ok(nodes) => nodes,
30                Err(warn) => return self.emit_warning(warn),
31            };
32
33        // Visit block.
34        self.visit_block(&input.then);
35
36        // Exit scope for checking awaits in `then` branch of conditional.
37        let saved_paths =
38            self.await_checker.exit_then_scope(self.variant == Some(Variant::AsyncFunction), current_bst_nodes);
39
40        if let Some(otherwise) = &input.otherwise {
41            match &**otherwise {
42                Statement::Block(stmt) => {
43                    // Visit the otherwise-block.
44                    self.visit_block(stmt);
45                }
46                Statement::Conditional(stmt) => self.visit_conditional(stmt),
47                _ => unreachable!("Else-case can only be a block or conditional statement."),
48            }
49        }
50
51        // Update the set of all possible BST paths.
52        self.await_checker.exit_statement_scope(self.variant == Some(Variant::AsyncFunction), saved_paths);
53    }
54}