Skip to content

Commit

Permalink
fixed usage of for iterator variable
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerStarkware committed Jul 29, 2024
1 parent 95c49a8 commit 8373446
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,14 @@ fn test_array_snap_into_span() {
fn test_span_into_array_snap() {
assert_eq!(@array![1, 2, 3], array![1, 2, 3].span().into());
}
#[test]
fn nested_for_loop() {
let mat = array![array![1, 2], array![3, 4], array![5, 6]];
let mut result = 0;
for arr in mat {
for i in arr {
result += i;
};
};
assert_eq!(result, 21);
}
45 changes: 45 additions & 0 deletions crates/cairo-lang-lowering/src/lower/test_data/usage
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,48 @@ Block 6:27:
Usage: ParamId(test::a)::b::c,
Changes: ParamId(test::a)::b::c,
Introductions: LocalVarId(test::c), LocalVarId(test::only_used_in_condition),

//! > ==========================================================================

//! > Test for usage

//! > test_runner_name
test_function_usage

//! > function
fn foo(a: u32) {
let arr = array![1, 2, 3];
for b in arr {
a + b;
};
}

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > usage
Block 0:0:
Usage:
Changes:
Snapshot_Usage:
Introductions: LocalVarId(test::__array_builder_macro_result__),
Block 2:17:
Usage: ParamId(test::a), LocalVarId(test::b),
Changes:
Snapshot_Usage:
Introductions:
For 2:4:
Usage: LocalVarId(test::in), ParamId(test::a),
Changes: LocalVarId(test::in),
Snapshot_Usage:
Introductions: LocalVarId(test::b),
Block 0:15:
Usage: ParamId(test::a),
Changes:
Snapshot_Usage:
Introductions: LocalVarId(test::arr), LocalVarId(test::in),

4 changes: 4 additions & 0 deletions crates/cairo-lang-lowering/src/lower/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cairo_lang_semantic::{
self as semantic, Expr, ExprFunctionCallArg, ExprId, ExprVarMemberPath, FunctionBody, Pattern,
Statement, VarId,
};
use cairo_lang_utils::extract_matches;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
use id_arena::Arena;
Expand Down Expand Up @@ -209,6 +210,9 @@ impl BlockUsages {
self.block_usages.insert(expr_id, usage);
}
Expr::For(expr) => {
current.introductions.insert(
extract_matches!(&expr.into_iter_member_path, ExprVarMemberPath::Var).var,
);
let mut usage: Usage = Default::default();
usage.usage.insert(
(&expr.into_iter_member_path).into(),
Expand Down

0 comments on commit 8373446

Please sign in to comment.