Skip to content

Commit a9a83b8

Browse files
authored
fix: instead of folding expr for constant values, we keep the expr as… (#252)
* fix: instead of folding expr for constant values, we keep the expr as it is * fix: avoid re-monomorphized a function name
1 parent 1f4844c commit a9a83b8

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

examples/functions.no

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn main(pub one: Field) {
1010
let four = add(one, 3);
1111
assert_eq(four, 4);
1212

13+
// double() should not be folded to return 8
14+
// the asm test will catch the missing constraint if it is folded
1315
let eight = double(4);
1416
assert_eq(eight, double(four));
1517
}

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ pub enum ErrorKind {
372372
#[error("division by zero")]
373373
DivisionByZero,
374374

375+
#[error("lhs `{0}` is less than rhs `{1}`")]
376+
NegativeLhsLessThanRhs(String, String),
377+
375378
#[error("Not enough variables provided to fill placeholders in the formatted string")]
376379
InsufficientVariables,
377380
}

src/mast/mod.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -827,20 +827,42 @@ fn monomorphize_expr<B: Backend>(
827827
let ExprMonoInfo { expr: rhs_expr, .. } = rhs_mono;
828828

829829
// fold constants
830-
let cst = match (&lhs_expr.kind, &rhs_expr.kind) {
831-
(ExprKind::BigUInt(lhs), ExprKind::BigUInt(rhs)) => match op {
832-
Op2::Addition => Some(lhs + rhs),
833-
Op2::Subtraction => Some(lhs - rhs),
834-
Op2::Multiplication => Some(lhs * rhs),
835-
Op2::Division => Some(lhs / rhs),
836-
_ => None,
837-
},
830+
let cst = match (&lhs_mono.constant, &rhs_mono.constant) {
831+
(Some(PropagatedConstant::Single(lhs)), Some(PropagatedConstant::Single(rhs))) => {
832+
match op {
833+
Op2::Addition => Some(lhs + rhs),
834+
Op2::Subtraction => {
835+
if lhs < rhs {
836+
// throw error
837+
return Err(error(
838+
ErrorKind::NegativeLhsLessThanRhs(
839+
lhs.to_string(),
840+
rhs.to_string(),
841+
),
842+
expr.span,
843+
));
844+
}
845+
Some(lhs - rhs)
846+
}
847+
Op2::Multiplication => Some(lhs * rhs),
848+
Op2::Division => Some(lhs / rhs),
849+
_ => None,
850+
}
851+
}
838852
_ => None,
839853
};
840854

841855
match cst {
842856
Some(v) => {
843-
let mexpr = expr.to_mast(ctx, &ExprKind::BigUInt(v.clone()));
857+
let mexpr = expr.to_mast(
858+
ctx,
859+
&ExprKind::BinaryOp {
860+
op: op.clone(),
861+
protected: *protected,
862+
lhs: Box::new(lhs_expr),
863+
rhs: Box::new(rhs_expr),
864+
},
865+
);
844866

845867
ExprMonoInfo::new(mexpr, typ, Some(PropagatedConstant::from(v)))
846868
}

src/parser/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ impl FnSig {
716716
pub fn monomorphized_name(&self) -> Ident {
717717
let mut name = self.name.clone();
718718

719+
// check if it contains # in the name
720+
if name.value.contains('#') {
721+
// if so, then it is already monomorphized
722+
return name;
723+
}
724+
719725
if self.require_monomorphization() {
720726
let mut generics = self.generics.parameters.iter().collect::<Vec<_>>();
721727
generics.sort_by(|a, b| a.0.cmp(b.0));

0 commit comments

Comments
 (0)