From 09451d350d15d614995338839433d9d361617391 Mon Sep 17 00:00:00 2001 From: Kata Choi Date: Mon, 23 Sep 2024 10:46:45 +0800 Subject: [PATCH] fix: properly propagate constant value to assigned var in mast phase (#183) - also update the tests to cover the related scenarios --- .../fixture/asm/kimchi/generic_nested_func.asm | 1 + .../fixture/asm/kimchi/generic_nested_method.asm | 1 + examples/fixture/asm/r1cs/generic_nested_func.asm | 1 + .../fixture/asm/r1cs/generic_nested_method.asm | 1 + examples/generic_for_loop.no | 14 ++++++++++++++ src/mast/mod.rs | 2 +- 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/examples/fixture/asm/kimchi/generic_nested_func.asm b/examples/fixture/asm/kimchi/generic_nested_func.asm index ea83d5c55..c2c1cbf62 100644 --- a/examples/fixture/asm/kimchi/generic_nested_func.asm +++ b/examples/fixture/asm/kimchi/generic_nested_func.asm @@ -1,4 +1,5 @@ @ noname.0.7.0 +@ public inputs: 4 DoubleGeneric<1> DoubleGeneric<1> diff --git a/examples/fixture/asm/kimchi/generic_nested_method.asm b/examples/fixture/asm/kimchi/generic_nested_method.asm index ea83d5c55..c2c1cbf62 100644 --- a/examples/fixture/asm/kimchi/generic_nested_method.asm +++ b/examples/fixture/asm/kimchi/generic_nested_method.asm @@ -1,4 +1,5 @@ @ noname.0.7.0 +@ public inputs: 4 DoubleGeneric<1> DoubleGeneric<1> diff --git a/examples/fixture/asm/r1cs/generic_nested_func.asm b/examples/fixture/asm/r1cs/generic_nested_func.asm index 4d0e4621a..b7ff82ca4 100644 --- a/examples/fixture/asm/r1cs/generic_nested_func.asm +++ b/examples/fixture/asm/r1cs/generic_nested_func.asm @@ -1,4 +1,5 @@ @ noname.0.7.0 +@ public inputs: 4 v_4 == (v_1) * (1) v_4 == (v_2) * (1) diff --git a/examples/fixture/asm/r1cs/generic_nested_method.asm b/examples/fixture/asm/r1cs/generic_nested_method.asm index 4d0e4621a..b7ff82ca4 100644 --- a/examples/fixture/asm/r1cs/generic_nested_method.asm +++ b/examples/fixture/asm/r1cs/generic_nested_method.asm @@ -1,4 +1,5 @@ @ noname.0.7.0 +@ public inputs: 4 v_4 == (v_1) * (1) v_4 == (v_2) * (1) diff --git a/examples/generic_for_loop.no b/examples/generic_for_loop.no index 4c564fdba..c0caddc27 100644 --- a/examples/generic_for_loop.no +++ b/examples/generic_for_loop.no @@ -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]; @@ -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); } \ No newline at end of file diff --git a/src/mast/mod.rs b/src/mast/mod.rs index f13103639..60039c45d 100644 --- a/src/mast/mod.rs +++ b/src/mast/mod.rs @@ -892,7 +892,7 @@ pub fn monomorphize_stmt( 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)?;