Skip to content

Commit e07531d

Browse files
ilyalesokhin-starkwareorizi
authored andcommitted
Fix usage for let else statements. (#8209)
1 parent b206af6 commit e07531d

File tree

2 files changed

+43
-4
lines changed
  • crates/cairo-lang-semantic/src/usage

2 files changed

+43
-4
lines changed

crates/cairo-lang-semantic/src/usage/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::expr::fmt::ExprFormatter;
1212
use crate::expr::objects::Arenas;
1313
use crate::{
1414
ConcreteStructId, Condition, Expr, ExprFunctionCallArg, ExprId, ExprVarMemberPath,
15-
FixedSizeArrayItems, FunctionBody, Parameter, Pattern, PatternId, Statement, VarId,
15+
FixedSizeArrayItems, FunctionBody, Parameter, Pattern, PatternId, Statement, StatementLet,
16+
VarId,
1617
};
1718

1819
#[cfg(test)]
@@ -233,9 +234,18 @@ impl Usages {
233234
let mut usage = Default::default();
234235
for stmt in &expr.statements {
235236
match &arenas.statements[*stmt] {
236-
Statement::Let(stmt) => {
237-
self.handle_expr(arenas, stmt.expr, &mut usage);
238-
Self::handle_pattern(&arenas.patterns, stmt.pattern, &mut usage);
237+
Statement::Let(StatementLet {
238+
pattern,
239+
expr,
240+
else_clause,
241+
stable_ptr: _,
242+
}) => {
243+
self.handle_expr(arenas, *expr, &mut usage);
244+
Self::handle_pattern(&arenas.patterns, *pattern, &mut usage);
245+
246+
if let Some(else_clause) = else_clause {
247+
self.handle_expr(arenas, *else_clause, &mut usage);
248+
}
239249
}
240250
Statement::Expr(stmt) => self.handle_expr(arenas, stmt.expr, &mut usage),
241251
Statement::Continue(_) => (),

crates/cairo-lang-semantic/src/usage/test_data/usage

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,32 @@ For 4:4:
210210
Usage: LocalVarId(test::in), LocalVarId(test::counter),
211211
Changes: LocalVarId(test::in), LocalVarId(test::counter),
212212
Snapshot_Usage: LocalVarId(test::inner),
213+
214+
//! > ==========================================================================
215+
216+
//! > closure in else block.
217+
218+
//! > test_runner_name
219+
test_function_usage
220+
221+
//! > function
222+
fn foo(mut a: Span<u32>, mut b: Span<u32>) -> Option<usize> {
223+
let Some(_) = a.pop_front() else {
224+
return b.pop_front().map(|q| *q);
225+
};
226+
227+
None
228+
}
229+
230+
//! > function_name
231+
foo
232+
233+
//! > module_code
234+
235+
//! > semantic_diagnostics
236+
237+
//! > usage
238+
Closure 2:33:
239+
Usage:
240+
Changes:
241+
Snapshot_Usage:

0 commit comments

Comments
 (0)