Skip to content

Commit

Permalink
fix: properly propagate constant value to assigned var in mast phase (#…
Browse files Browse the repository at this point in the history
…183)

- also update the tests to cover the related scenarios
  • Loading branch information
katat authored Sep 23, 2024
1 parent 34e0d16 commit 09451d3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/fixture/asm/kimchi/generic_nested_func.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@ noname.0.7.0
@ public inputs: 4

DoubleGeneric<1>
DoubleGeneric<1>
Expand Down
1 change: 1 addition & 0 deletions examples/fixture/asm/kimchi/generic_nested_method.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@ noname.0.7.0
@ public inputs: 4

DoubleGeneric<1>
DoubleGeneric<1>
Expand Down
1 change: 1 addition & 0 deletions examples/fixture/asm/r1cs/generic_nested_func.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@ noname.0.7.0
@ public inputs: 4

v_4 == (v_1) * (1)
v_4 == (v_2) * (1)
Expand Down
1 change: 1 addition & 0 deletions examples/fixture/asm/r1cs/generic_nested_method.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@ noname.0.7.0
@ public inputs: 4

v_4 == (v_1) * (1)
v_4 == (v_2) * (1)
Expand Down
14 changes: 14 additions & 0 deletions examples/generic_for_loop.no
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ fn join(const LEN: Field, arr1: [Field; LLEN], arr2: [Field; RLEN]) -> [Field; L
return arr;
}

fn accumulate_mut(const INIT: Field) -> Field {
// it shouldn't fold these variables, even they hold constant values
// it should only fold the generic vars
let mut zz = INIT;
for ii in 0..3 {
zz = zz + zz;
}
return zz;
}

fn main(pub xx: Field) {
let arr1 = [xx + 1, xx + 2];
let arr2 = [xx + 3, xx + 4];
Expand All @@ -21,4 +31,8 @@ fn main(pub xx: Field) {
assert_eq(arr[1], arr1[1]);
assert_eq(arr[2], arr2[0]);
assert_eq(arr[3], arr2[1]);

let init_val = 1;
let res = accumulate_mut(init_val);
assert_eq(res, 8);
}
2 changes: 1 addition & 1 deletion src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ pub fn monomorphize_stmt<B: Backend>(
StmtKind::Assign { mutable, lhs, rhs } => {
let rhs_mono = monomorphize_expr(ctx, rhs, mono_fn_env)?;
let typ = rhs_mono.typ.as_ref().expect("expected a type");
let type_info = MTypeInfo::new(typ, lhs.span, None);
let type_info = MTypeInfo::new(typ, lhs.span, rhs_mono.constant);

// store the type of lhs in the env
mono_fn_env.store_type(&lhs.value, &type_info)?;
Expand Down

0 comments on commit 09451d3

Please sign in to comment.