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

chore: box Closure in comptime::Value enum #7400

Merged
merged 2 commits into from
Feb 14, 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
25 changes: 16 additions & 9 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
};

use super::errors::{IResult, InterpreterError};
use super::value::{unwrap_rc, Value};
use super::value::{unwrap_rc, Closure, Value};

mod builtin;
mod foreign;
Expand Down Expand Up @@ -258,13 +258,13 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 261 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

fn call_closure(
&mut self,
closure: HirLambda,
lambda: HirLambda,
environment: Vec<Value>,
arguments: Vec<(Value, Location)>,
function_scope: Option<FuncId>,
Expand All @@ -275,7 +275,7 @@
let old_module = self.elaborator.replace_module(module_scope);
let old_function = std::mem::replace(&mut self.current_function, function_scope);

let result = self.call_closure_inner(closure, environment, arguments, call_location);
let result = self.call_closure_inner(lambda, environment, arguments, call_location);

self.current_function = old_function;
self.elaborator.replace_module(old_module);
Expand Down Expand Up @@ -1056,7 +1056,7 @@
}

/// Generate matches for bit shifting, which in Noir only accepts `u8` for RHS.
macro_rules! match_bitshift {

Check warning on line 1059 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(($lhs_value:ident as $lhs:ident $op:literal $rhs_value:ident as $rhs:ident) => $expr:expr) => {
match_values! {
($lhs_value as $lhs $op $rhs_value as $rhs) {
Expand Down Expand Up @@ -1126,10 +1126,10 @@
BinaryOpKind::Xor => match_bitwise! {
(lhs_value as lhs "^" rhs_value as rhs) => lhs ^ rhs
},
BinaryOpKind::ShiftRight => match_bitshift! {

Check warning on line 1129 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(lhs_value as lhs ">>" rhs_value as rhs) => lhs.checked_shr(rhs.into())
},
BinaryOpKind::ShiftLeft => match_bitshift! {

Check warning on line 1132 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitshift)
(lhs_value as lhs "<<" rhs_value as rhs) => lhs.checked_shl(rhs.into())
},
BinaryOpKind::Modulo => match_integer! {
Expand Down Expand Up @@ -1359,9 +1359,14 @@
}
Ok(result)
}
Value::Closure(closure, env, _, function_scope, module_scope) => {
self.call_closure(closure, env, arguments, function_scope, module_scope, location)
}
Value::Closure(closure) => self.call_closure(
closure.lambda,
closure.env,
arguments,
closure.function_scope,
closure.module_scope,
location,
),
value => {
let typ = value.get_type().into_owned();
Err(InterpreterError::NonFunctionCalled { typ, location })
Expand Down Expand Up @@ -1544,12 +1549,14 @@

fn evaluate_lambda(&mut self, lambda: HirLambda, id: ExprId) -> IResult<Value> {
let location = self.elaborator.interner.expr_location(&id);
let environment =
let env =
try_vecmap(&lambda.captures, |capture| self.lookup_id(capture.ident.id, location))?;

let typ = self.elaborator.interner.id_type(id).follow_bindings();
let module = self.elaborator.module_id();
Ok(Value::Closure(lambda, environment, typ, self.current_function, module))
let module_scope = self.elaborator.module_id();
let closure =
Closure { lambda, env, typ, function_scope: self.current_function, module_scope };
Ok(Value::Closure(Box::new(closure)))
}

fn evaluate_quote(&mut self, mut tokens: Tokens, expr_id: ExprId) -> IResult<Value> {
Expand Down
17 changes: 12 additions & 5 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

// Closures also store their original scope (function & module)
// in case they use functions such as `Quoted::as_type` which require them.
Closure(HirLambda, Vec<Value>, Type, Option<FuncId>, ModuleId),
Closure(Box<Closure>),

Tuple(Vec<Value>),
Struct(HashMap<Rc<String>, Value>, Type),
Expand All @@ -76,6 +76,15 @@
UnresolvedType(UnresolvedTypeData),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Closure {
pub lambda: HirLambda,
pub env: Vec<Value>,
pub typ: Type,
pub function_scope: Option<FuncId>,
pub module_scope: ModuleId,
}

#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum ExprValue {
Expression(ExpressionKind),
Expand All @@ -99,8 +108,8 @@
Value::Expr(Box::new(ExprValue::Statement(statement)))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 111 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(Box::new(ExprValue::LValue(lvaue)))

Check warning on line 112 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn pattern(pattern: Pattern) -> Self {
Expand All @@ -127,7 +136,7 @@
}
Value::FormatString(_, typ) => return Cow::Borrowed(typ),
Value::Function(_, typ, _) => return Cow::Borrowed(typ),
Value::Closure(_, _, typ, ..) => return Cow::Borrowed(typ),
Value::Closure(closure) => return Cow::Borrowed(&closure.typ),
Value::Tuple(fields) => {
Type::Tuple(vecmap(fields, |field| field.get_type().into_owned()))
}
Expand Down Expand Up @@ -456,9 +465,7 @@
Value::Pointer(element, true) => {
return element.unwrap_or_clone().into_hir_expression(interner, location);
}
Value::Closure(hir_lambda, _args, _typ, _opt_func_id, _module_id) => {
HirExpression::Lambda(hir_lambda)
}
Value::Closure(closure) => HirExpression::Lambda(closure.lambda.clone()),
Value::TypedExpr(TypedExpr::StmtId(..))
| Value::Expr(..)
| Value::Pointer(..)
Expand Down
Loading