Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly propagate constant value to assigned var in mast phase #183

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/generic_for_loop.no
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ fn join(const LEN: Field, arr1: [Field; LLEN], arr2: [Field; RLEN]) -> [Field; L
return arr;
}

fn accumulate_mut(const LEN: Field) -> Field {
// shouldn't fold mutable variables as constants
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cryptic comment that will confuse us later when we forget what this was about :o

let mut zz = LEN;
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 +30,7 @@ fn main(pub xx: Field) {
assert_eq(arr[1], arr1[1]);
assert_eq(arr[2], arr2[0]);
assert_eq(arr[3], arr2[1]);

let res = accumulate_mut(1);
assert_eq(res, 8);
}
41 changes: 33 additions & 8 deletions src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,33 @@ fn monomorphize_expr<B: Backend>(
| Op2::Multiplication
| Op2::Division
| Op2::BoolAnd
| Op2::BoolOr => lhs_mono.typ,
| Op2::BoolOr => lhs_mono.clone().typ,
};

let cst = match (lhs_mono.constant, rhs_mono.constant) {
(Some(lhs), Some(rhs)) => match op {
Op2::Addition => Some(lhs + rhs),
Op2::Subtraction => Some(lhs - rhs),
Op2::Multiplication => Some(lhs * rhs),
Op2::Division => Some(lhs / rhs),
let ExprMonoInfo {
typ: lhs_typ,
constant: lhs_cst,
..
} = lhs_mono;
let ExprMonoInfo {
typ: rhs_typ,
constant: rhs_cst,
..
} = rhs_mono;

// check if the constant values can be folded
let cst = match (lhs_typ, rhs_typ) {
(
Some(TyKind::Field { constant: true }),
Some(TyKind::Field { constant: true }),
) => match (lhs_cst, rhs_cst) {
(Some(lhs), Some(rhs)) => match op {
Op2::Addition => Some(lhs + rhs),
Op2::Subtraction => Some(lhs - rhs),
Op2::Multiplication => Some(lhs * rhs),
Op2::Division => Some(lhs / rhs),
_ => None,
},
_ => None,
},
_ => None,
Expand Down Expand Up @@ -895,7 +913,14 @@ 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 = if *mutable && matches!(typ, TyKind::Field { constant: true }) {
// mutable variable shouldn't be constant
let new_typ = &TyKind::Field { constant: false };
MTypeInfo::new(new_typ, lhs.span, None)
} else {
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
Loading