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.into());
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 let Some(core_function) = CoreFunction::from_symbols(input.variant.name, input.name.name) else {
120 panic!("Typechecking guarantees that this function exists.");
121 };
122
123 if core_function == CoreFunction::FutureAwait {
125 self.assert_future_await(&input.arguments.first(), input.span());
126 }
127 }
128
129 fn visit_call(&mut self, input: &CallExpression, _: &Self::AdditionalInput) -> Self::Output {
130 let caller_program = self.current_program;
131 let callee_program = input.program.unwrap_or(caller_program);
132
133 if self.non_async_external_call_seen
136 && self.variant == Some(Variant::AsyncTransition)
137 && callee_program != caller_program
138 {
139 self.assert_simple_async_transition_call(callee_program, &input.function, input.span());
140 }
141
142 let function_program = input.program.unwrap_or(self.current_program);
144
145 let func_symbol = self
146 .state
147 .symbol_table
148 .lookup_function(&Location::new(function_program, input.function.absolute_path().to_vec()))
149 .expect("Type checking guarantees functions exist.");
150
151 if func_symbol.function.variant == Variant::Transition {
152 self.non_async_external_call_seen = true;
153 }
154 }
155
156 fn visit_conditional(&mut self, input: &ConditionalStatement) {
158 self.visit_expression(&input.condition, &Default::default());
159
160 let current_bst_nodes: Vec<ConditionalTreeNode> =
162 match self.await_checker.create_then_scope(self.variant == Some(Variant::AsyncFunction), input.span) {
163 Ok(nodes) => nodes,
164 Err(warn) => return self.emit_warning(warn),
165 };
166
167 self.visit_block(&input.then);
169
170 let saved_paths =
172 self.await_checker.exit_then_scope(self.variant == Some(Variant::AsyncFunction), current_bst_nodes);
173
174 if let Some(otherwise) = &input.otherwise {
175 match &**otherwise {
176 Statement::Block(stmt) => {
177 self.visit_block(stmt);
179 }
180 Statement::Conditional(stmt) => self.visit_conditional(stmt),
181 _ => unreachable!("Else-case can only be a block or conditional statement."),
182 }
183 }
184
185 self.await_checker.exit_statement_scope(self.variant == Some(Variant::AsyncFunction), saved_paths);
187 }
188}