Skip to content

Commit e619df6

Browse files
committed
Remove duplicate code
1 parent 1ca299a commit e619df6

File tree

3 files changed

+58
-65
lines changed

3 files changed

+58
-65
lines changed

compiler/noirc_frontend/src/elaborator/comptime.rs

+26-36
Original file line numberDiff line numberDiff line change
@@ -52,48 +52,41 @@ impl<'context> Elaborator<'context> {
5252
/// Elaborate an expression from the middle of a comptime scope.
5353
/// When this happens we require additional information to know
5454
/// what variables should be in scope.
55-
pub fn elaborate_item_from_comptime<'a, T>(
55+
pub fn elaborate_item_from_comptime_in_function<'a, T>(
5656
&'a mut self,
5757
current_function: Option<FuncId>,
5858
f: impl FnOnce(&mut Elaborator<'a>) -> T,
5959
) -> T {
60-
// Create a fresh elaborator to ensure no state is changed from
61-
// this elaborator
62-
let mut elaborator = Elaborator::new(
63-
self.interner,
64-
self.def_maps,
65-
self.crate_id,
66-
self.debug_comptime_in_file,
67-
self.enable_arithmetic_generics,
68-
self.interpreter_call_stack.clone(),
69-
);
70-
71-
elaborator.function_context.push(FunctionContext::default());
72-
elaborator.scopes.start_function();
73-
74-
if let Some(function) = current_function {
75-
let meta = elaborator.interner.function_meta(&function);
76-
elaborator.current_item = Some(DependencyId::Function(function));
77-
elaborator.crate_id = meta.source_crate;
78-
elaborator.local_module = meta.source_module;
79-
elaborator.file = meta.source_file;
80-
elaborator.introduce_generics_into_scope(meta.all_generics.clone());
81-
}
82-
83-
elaborator.populate_scope_from_comptime_scopes();
84-
85-
let result = f(&mut elaborator);
86-
elaborator.check_and_pop_function_context();
87-
88-
self.errors.append(&mut elaborator.errors);
89-
result
60+
self.elaborate_item_from_comptime(f, |elaborator| {
61+
if let Some(function) = current_function {
62+
let meta = elaborator.interner.function_meta(&function);
63+
elaborator.current_item = Some(DependencyId::Function(function));
64+
elaborator.crate_id = meta.source_crate;
65+
elaborator.local_module = meta.source_module;
66+
elaborator.file = meta.source_file;
67+
elaborator.introduce_generics_into_scope(meta.all_generics.clone());
68+
}
69+
})
9070
}
9171

92-
pub fn elaborate_item_in_module<'a, T>(
72+
pub fn elaborate_item_from_comptime_in_module<'a, T>(
9373
&'a mut self,
9474
module: ModuleId,
9575
file: FileId,
9676
f: impl FnOnce(&mut Elaborator<'a>) -> T,
77+
) -> T {
78+
self.elaborate_item_from_comptime(f, |elaborator| {
79+
elaborator.current_item = None;
80+
elaborator.crate_id = module.krate;
81+
elaborator.local_module = module.local_id;
82+
elaborator.file = file;
83+
})
84+
}
85+
86+
fn elaborate_item_from_comptime<'a, T>(
87+
&'a mut self,
88+
f: impl FnOnce(&mut Elaborator<'a>) -> T,
89+
setup: impl FnOnce(&mut Elaborator<'a>),
9790
) -> T {
9891
// Create a fresh elaborator to ensure no state is changed from
9992
// this elaborator
@@ -109,10 +102,7 @@ impl<'context> Elaborator<'context> {
109102
elaborator.function_context.push(FunctionContext::default());
110103
elaborator.scopes.start_function();
111104

112-
elaborator.current_item = None;
113-
elaborator.crate_id = module.krate;
114-
elaborator.local_module = module.local_id;
115-
elaborator.file = file;
105+
setup(&mut elaborator);
116106

117107
elaborator.populate_scope_from_comptime_scopes();
118108

compiler/noirc_frontend/src/hir/comptime/interpreter.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
172172
Some(body) => Ok(body),
173173
None => {
174174
if matches!(&meta.function_body, FunctionBody::Unresolved(..)) {
175-
self.elaborate_item(None, |elaborator| {
175+
self.elaborate_in_function(None, |elaborator| {
176176
elaborator.elaborate_function(function);
177177
});
178178

@@ -185,13 +185,13 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
185185
}
186186
}
187187

188-
fn elaborate_item<T>(
188+
fn elaborate_in_function<T>(
189189
&mut self,
190190
function: Option<FuncId>,
191191
f: impl FnOnce(&mut Elaborator) -> T,
192192
) -> T {
193193
self.unbind_generics_from_previous_function();
194-
let result = self.elaborator.elaborate_item_from_comptime(function, f);
194+
let result = self.elaborator.elaborate_item_from_comptime_in_function(function, f);
195195
self.rebind_generics_from_previous_function();
196196
result
197197
}
@@ -203,7 +203,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
203203
f: impl FnOnce(&mut Elaborator) -> T,
204204
) -> T {
205205
self.unbind_generics_from_previous_function();
206-
let result = self.elaborator.elaborate_item_in_module(module, file, f);
206+
let result = self.elaborator.elaborate_item_from_comptime_in_module(module, file, f);
207207
self.rebind_generics_from_previous_function();
208208
result
209209
}
@@ -1258,7 +1258,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
12581258
let mut result = self.call_function(function_id, arguments, bindings, location)?;
12591259
if call.is_macro_call {
12601260
let expr = result.into_expression(self.elaborator.interner, location)?;
1261-
let expr = self.elaborate_item(self.current_function, |elaborator| {
1261+
let expr = self.elaborate_in_function(self.current_function, |elaborator| {
12621262
elaborator.elaborate_expression(expr).0
12631263
});
12641264
result = self.evaluate(expr)?;

compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,10 @@ fn quoted_as_module(
569569

570570
let path = parse(argument, parser::path_no_turbofish(), "a path").ok();
571571
let option_value = path.and_then(|path| {
572-
let module = interpreter.elaborate_item(interpreter.current_function, |elaborator| {
573-
elaborator.resolve_module_by_path(path)
574-
});
572+
let module = interpreter
573+
.elaborate_in_function(interpreter.current_function, |elaborator| {
574+
elaborator.resolve_module_by_path(path)
575+
});
575576
module.map(Value::ModuleDefinition)
576577
});
577578

@@ -587,7 +588,7 @@ fn quoted_as_trait_constraint(
587588
let argument = check_one_argument(arguments, location)?;
588589
let trait_bound = parse(argument, parser::trait_bound(), "a trait constraint")?;
589590
let bound = interpreter
590-
.elaborate_item(interpreter.current_function, |elaborator| {
591+
.elaborate_in_function(interpreter.current_function, |elaborator| {
591592
elaborator.resolve_trait_bound(&trait_bound, Type::Unit)
592593
})
593594
.ok_or(InterpreterError::FailedToResolveTraitBound { trait_bound, location })?;
@@ -603,8 +604,8 @@ fn quoted_as_type(
603604
) -> IResult<Value> {
604605
let argument = check_one_argument(arguments, location)?;
605606
let typ = parse(argument, parser::parse_type(), "a type")?;
606-
let typ =
607-
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));
607+
let typ = interpreter
608+
.elaborate_in_function(interpreter.current_function, |elab| elab.resolve_type(typ));
608609
Ok(Value::Type(typ))
609610
}
610611

@@ -1675,23 +1676,25 @@ fn expr_resolve(
16751676
interpreter.current_function
16761677
};
16771678

1678-
let value = interpreter.elaborate_item(function_to_resolve_in, |elaborator| match expr_value {
1679-
ExprValue::Expression(expression_kind) => {
1680-
let expr = Expression { kind: expression_kind, span: self_argument_location.span };
1681-
let (expr_id, _) = elaborator.elaborate_expression(expr);
1682-
Value::TypedExpr(TypedExpr::ExprId(expr_id))
1683-
}
1684-
ExprValue::Statement(statement_kind) => {
1685-
let statement = Statement { kind: statement_kind, span: self_argument_location.span };
1686-
let (stmt_id, _) = elaborator.elaborate_statement(statement);
1687-
Value::TypedExpr(TypedExpr::StmtId(stmt_id))
1688-
}
1689-
ExprValue::LValue(lvalue) => {
1690-
let expr = lvalue.as_expression();
1691-
let (expr_id, _) = elaborator.elaborate_expression(expr);
1692-
Value::TypedExpr(TypedExpr::ExprId(expr_id))
1693-
}
1694-
});
1679+
let value =
1680+
interpreter.elaborate_in_function(function_to_resolve_in, |elaborator| match expr_value {
1681+
ExprValue::Expression(expression_kind) => {
1682+
let expr = Expression { kind: expression_kind, span: self_argument_location.span };
1683+
let (expr_id, _) = elaborator.elaborate_expression(expr);
1684+
Value::TypedExpr(TypedExpr::ExprId(expr_id))
1685+
}
1686+
ExprValue::Statement(statement_kind) => {
1687+
let statement =
1688+
Statement { kind: statement_kind, span: self_argument_location.span };
1689+
let (stmt_id, _) = elaborator.elaborate_statement(statement);
1690+
Value::TypedExpr(TypedExpr::StmtId(stmt_id))
1691+
}
1692+
ExprValue::LValue(lvalue) => {
1693+
let expr = lvalue.as_expression();
1694+
let (expr_id, _) = elaborator.elaborate_expression(expr);
1695+
Value::TypedExpr(TypedExpr::ExprId(expr_id))
1696+
}
1697+
});
16951698

16961699
Ok(value)
16971700
}
@@ -1942,7 +1945,7 @@ fn function_def_set_parameters(
19421945
"a pattern",
19431946
)?;
19441947

1945-
let hir_pattern = interpreter.elaborate_item(Some(func_id), |elaborator| {
1948+
let hir_pattern = interpreter.elaborate_in_function(Some(func_id), |elaborator| {
19461949
elaborator.elaborate_pattern_and_store_ids(
19471950
parameter_pattern,
19481951
parameter_type.clone(),

0 commit comments

Comments
 (0)