Skip to content

Commit

Permalink
fix: instead of folding expr for constant values, we keep the expr as… (
Browse files Browse the repository at this point in the history
#252)

* fix: instead of folding expr for constant values, we keep the expr as it is

* fix: avoid re-monomorphized a function name
  • Loading branch information
katat authored Jan 21, 2025
1 parent 1f4844c commit a9a83b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/functions.no
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main(pub one: Field) {
let four = add(one, 3);
assert_eq(four, 4);

// double() should not be folded to return 8
// the asm test will catch the missing constraint if it is folded
let eight = double(4);
assert_eq(eight, double(four));
}
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ pub enum ErrorKind {
#[error("division by zero")]
DivisionByZero,

#[error("lhs `{0}` is less than rhs `{1}`")]
NegativeLhsLessThanRhs(String, String),

#[error("Not enough variables provided to fill placeholders in the formatted string")]
InsufficientVariables,
}
40 changes: 31 additions & 9 deletions src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,20 +827,42 @@ fn monomorphize_expr<B: Backend>(
let ExprMonoInfo { expr: rhs_expr, .. } = rhs_mono;

// fold constants
let cst = match (&lhs_expr.kind, &rhs_expr.kind) {
(ExprKind::BigUInt(lhs), ExprKind::BigUInt(rhs)) => match op {
Op2::Addition => Some(lhs + rhs),
Op2::Subtraction => Some(lhs - rhs),
Op2::Multiplication => Some(lhs * rhs),
Op2::Division => Some(lhs / rhs),
_ => None,
},
let cst = match (&lhs_mono.constant, &rhs_mono.constant) {
(Some(PropagatedConstant::Single(lhs)), Some(PropagatedConstant::Single(rhs))) => {
match op {
Op2::Addition => Some(lhs + rhs),
Op2::Subtraction => {
if lhs < rhs {
// throw error
return Err(error(
ErrorKind::NegativeLhsLessThanRhs(
lhs.to_string(),
rhs.to_string(),
),
expr.span,
));
}
Some(lhs - rhs)
}
Op2::Multiplication => Some(lhs * rhs),
Op2::Division => Some(lhs / rhs),
_ => None,
}
}
_ => None,
};

match cst {
Some(v) => {
let mexpr = expr.to_mast(ctx, &ExprKind::BigUInt(v.clone()));
let mexpr = expr.to_mast(
ctx,
&ExprKind::BinaryOp {
op: op.clone(),
protected: *protected,
lhs: Box::new(lhs_expr),
rhs: Box::new(rhs_expr),
},
);

ExprMonoInfo::new(mexpr, typ, Some(PropagatedConstant::from(v)))
}
Expand Down
6 changes: 6 additions & 0 deletions src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,12 @@ impl FnSig {
pub fn monomorphized_name(&self) -> Ident {
let mut name = self.name.clone();

// check if it contains # in the name
if name.value.contains('#') {
// if so, then it is already monomorphized
return name;
}

if self.require_monomorphization() {
let mut generics = self.generics.parameters.iter().collect::<Vec<_>>();
generics.sort_by(|a, b| a.0.cmp(b.0));
Expand Down

0 comments on commit a9a83b8

Please sign in to comment.