Skip to content

Commit 0d62e33

Browse files
committed
fix zkDSL compiler edge case
1 parent 9f9d7ee commit 0d62e33

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,31 @@ fn handle_array_assignment(
15881588
) {
15891589
let simplified_index = simplify_expr(index, res, counters, array_manager, const_malloc, const_arrays);
15901590

1591+
if let (ArrayAccessType::VarIsAssigned(var), SimpleExpr::Var(array_var)) = (&access_type, &array)
1592+
&& let Some(const_array) = const_arrays.get(array_var)
1593+
{
1594+
let index = simplified_index
1595+
.as_constant()
1596+
.expect("Const array access index should be constant")
1597+
.naive_eval()
1598+
.unwrap()
1599+
.to_usize();
1600+
assert!(
1601+
index < const_array.len(),
1602+
"Const array '{}' index {} out of bounds (length {})",
1603+
array_var,
1604+
index,
1605+
const_array.len()
1606+
);
1607+
res.push(SimpleLine::Assignment {
1608+
var: var.clone().into(),
1609+
operation: HighLevelOperation::Add,
1610+
arg0: SimpleExpr::Constant(ConstExpression::from(const_array[index])),
1611+
arg1: SimpleExpr::zero(),
1612+
});
1613+
return;
1614+
}
1615+
15911616
if let SimpleExpr::Constant(offset) = simplified_index.clone()
15921617
&& let SimpleExpr::Var(array_var) = &array
15931618
&& let Some(label) = const_malloc.map.get(array_var)

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,24 @@ fn intertwined_unrolled_loops_and_const_function_arguments() {
10571057
);
10581058
}
10591059

1060+
#[test]
1061+
fn test_direct_const_arr_access() {
1062+
let program = r#"
1063+
const ARR = [10, 100];
1064+
fn main() {
1065+
a = ARR[0];
1066+
assert a == 10;
1067+
return;
1068+
}
1069+
"#;
1070+
compile_and_run(
1071+
&ProgramSource::Raw(program.to_string()),
1072+
(&[], &[]),
1073+
DEFAULT_NO_VEC_RUNTIME_MEMORY,
1074+
false,
1075+
);
1076+
}
1077+
10601078
#[test]
10611079
fn test_const_fibonacci() {
10621080
let program = r#"

0 commit comments

Comments
 (0)