leo_passes/static_analysis/
visitor.rs1use crate::{CompilerState, ConditionalTreeNode, static_analysis::await_checker::AwaitChecker};
18
19use leo_ast::*;
20use leo_errors::{StaticAnalyzerError, StaticAnalyzerWarning};
21use leo_span::{Span, Symbol};
22
23pub struct StaticAnalyzingVisitor<'a> {
24 pub state: &'a mut CompilerState,
25 pub await_checker: AwaitChecker,
27 pub current_program: Symbol,
29 pub variant: Option<Variant>,
31 pub non_async_external_call_seen: bool,
33}
34
35impl StaticAnalyzingVisitor<'_> {
36 pub fn emit_err(&self, err: StaticAnalyzerError) {
37 self.state.handler.emit_err(err);
38 }
39
40 pub fn emit_warning(&self, warning: StaticAnalyzerWarning) {
42 self.state.handler.emit_warning(warning);
43 }
44
45 pub fn assert_future_await(&mut self, future: &Option<&Expression>, span: Span) {
47 let future_variable = match future {
49 Some(Expression::Path(path)) => path,
50 _ => {
51 return self.emit_err(StaticAnalyzerError::invalid_await_call(span));
52 }
53 };
54
55 match self.state.type_table.get(&future_variable.id) {
57 Some(type_) => {
58 if !matches!(type_, Type::Future(_)) {
59 self.emit_err(StaticAnalyzerError::expected_future(type_, future_variable.span()));
60 }
61 if self.await_checker.remove(&future_variable.identifier().name) {
64 self.emit_warning(StaticAnalyzerWarning::future_not_awaited_in_order(
65 future_variable,
66 future_variable.span(),
67 ));
68 }
69 }
70 None => {
71 self.emit_err(StaticAnalyzerError::expected_future(future_variable, future_variable.span()));
72 }
73 }
74 }
75
76 pub fn assert_simple_async_transition_call(&mut self, program: Symbol, function_path: &Path, span: Span) {
79 let func_symbol = self
80 .state
81 .symbol_table
82 .lookup_function(&Location::new(program, function_path.absolute_path().to_vec()))
83 .expect("Type checking guarantees functions are present.");
84
85 if func_symbol.function.variant != Variant::AsyncTransition {
87 return;
88 }
89
90 let finalizer = func_symbol
91 .finalizer
92 .as_ref()
93 .expect("Typechecking guarantees that all async transitions have an associated `finalize` field.");
94
95 let async_function = self
96 .state
97 .symbol_table
98 .lookup_function(&finalizer.location)
99 .expect("Type checking guarantees functions are present.");
100
101 if async_function.function.input.iter().any(|input| matches!(input.type_(), Type::Future(..))) {
103 self.emit_err(StaticAnalyzerError::async_transition_call_with_future_argument(function_path, span));
104 }
105 }
106}
107
108impl AstVisitor for StaticAnalyzingVisitor<'_> {
109 type AdditionalInput = ();
111 type Output = ();
112
113 fn visit_associated_function(
114 &mut self,
115 input: &AssociatedFunctionExpression,
116 _additional: &Self::AdditionalInput,
117 ) -> Self::Output {
118 if let Ok(CoreFunction::FutureAwait) = CoreFunction::try_from(input) {
120 self.assert_future_await(&input.arguments.first(), input.span());
121 }
122 }
123
124 fn visit_call(&mut self, input: &CallExpression, _: &Self::AdditionalInput) -> Self::Output {
125 let caller_program = self.current_program;
126 let callee_program = input.program.unwrap_or(caller_program);
127
128 if self.non_async_external_call_seen
131 && self.variant == Some(Variant::AsyncTransition)
132 && callee_program != caller_program
133 {
134 self.assert_simple_async_transition_call(callee_program, &input.function, input.span());
135 }
136
137 let function_program = input.program.unwrap_or(self.current_program);
139
140 let func_symbol = self
141 .state
142 .symbol_table
143 .lookup_function(&Location::new(function_program, input.function.absolute_path().to_vec()))
144 .expect("Type checking guarantees functions exist.");
145
146 if func_symbol.function.variant == Variant::Transition {
147 self.non_async_external_call_seen = true;
148 }
149 }
150
151 fn visit_conditional(&mut self, input: &ConditionalStatement) {
153 self.visit_expression(&input.condition, &Default::default());
154
155 let current_bst_nodes: Vec<ConditionalTreeNode> =
157 match self.await_checker.create_then_scope(self.variant == Some(Variant::AsyncFunction), input.span) {
158 Ok(nodes) => nodes,
159 Err(warn) => return self.emit_warning(warn),
160 };
161
162 self.visit_block(&input.then);
164
165 let saved_paths =
167 self.await_checker.exit_then_scope(self.variant == Some(Variant::AsyncFunction), current_bst_nodes);
168
169 if let Some(otherwise) = &input.otherwise {
170 match &**otherwise {
171 Statement::Block(stmt) => {
172 self.visit_block(stmt);
174 }
175 Statement::Conditional(stmt) => self.visit_conditional(stmt),
176 _ => unreachable!("Else-case can only be a block or conditional statement."),
177 }
178 }
179
180 self.await_checker.exit_statement_scope(self.variant == Some(Variant::AsyncFunction), saved_paths);
182 }
183}