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: instead of folding expr for constant values, we keep the expr as… #252

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading