From ce3186a4ce17fecf3900616b36c0d7e7d75e6d5f Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 5 Sep 2024 16:45:15 -0300 Subject: [PATCH 1/6] WIP: `Module::add_item` --- .../noirc_frontend/src/elaborator/comptime.rs | 2 +- compiler/noirc_frontend/src/elaborator/mod.rs | 2 +- .../src/hir/comptime/interpreter/builtin.rs | 40 +++++++++++++++++++ compiler/noirc_frontend/src/parser/mod.rs | 4 +- compiler/noirc_frontend/src/parser/parser.rs | 4 +- .../docs/noir/standard_library/meta/module.md | 6 +++ noir_stdlib/src/meta/module.nr | 5 +++ .../comptime_module/src/main.nr | 9 +++++ 8 files changed, 66 insertions(+), 6 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index e9650a625e8..20a3d4e09ba 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -352,7 +352,7 @@ impl<'context> Elaborator<'context> { } } - fn add_item( + pub(crate) fn add_item( &mut self, item: TopLevelStatement, generated_items: &mut CollectedItems, diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 6b23336b5ea..18c334503ff 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -274,7 +274,7 @@ impl<'context> Elaborator<'context> { this } - fn elaborate_items(&mut self, mut items: CollectedItems) { + pub(crate) fn elaborate_items(&mut self, mut items: CollectedItems) { // We must first resolve and intern the globals before we can resolve any stmts inside each function. // Each function uses its own resolver with a newly created ScopeForest, and must be resolved again to be within a function's scope // diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 072ac86b974..a8a40de0944 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -25,10 +25,21 @@ use crate::{ FunctionReturnType, IntegerBitSize, LValue, Literal, Statement, StatementKind, UnaryOp, UnresolvedType, UnresolvedTypeData, Visibility, }, +<<<<<<< Updated upstream hir::comptime::{ errors::IResult, value::{ExprValue, TypedExpr}, InterpreterError, Value, +======= + elaborator::{self, Elaborator}, + hir::{ + comptime::{ + errors::IResult, + value::{ExprValue, TypedExpr}, + InterpreterError, Value, + }, + def_collector::dc_crate::CollectedItems, +>>>>>>> Stashed changes }, hir_def::function::FunctionBody, lexer::Lexer, @@ -112,6 +123,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "function_def_set_return_public" => { function_def_set_return_public(self, arguments, location) } + "module_add_item" => module_add_item(self, arguments, location), "module_functions" => module_functions(self, arguments, location), "module_has_named_attribute" => module_has_named_attribute(self, arguments, location), "module_is_contract" => module_is_contract(self, arguments, location), @@ -2007,6 +2019,34 @@ fn function_def_set_return_public( Ok(Value::Unit) } +// fn add_item(self, item: Quoted) +fn module_add_item( + interpreter: &mut Interpreter, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let (self_argument, item) = check_two_arguments(arguments, location)?; + let module_id = get_module(self_argument)?; + + let parser = parser::top_level_statement(parser::module()); + let top_level_statement = parse(item, parser, "a top-level item")?; + + // TODO: we need to type-check the top-level statement relative to the current function + // (interpreter.current_function) but we need to add it to the given module (module_id). + // How to do this? + + let mut generated_items = CollectedItems::default(); + interpreter.elaborator.add_item(top_level_statement, &mut generated_items, location); + + if !generated_items.is_empty() { + interpreter.elaborate_item(interpreter.current_function, |elaborator| { + elaborator.elaborate_items(generated_items); + }); + } + + Ok(Value::Unit) +} + // fn functions(self) -> [FunctionDefinition] fn module_functions( interpreter: &Interpreter, diff --git a/compiler/noirc_frontend/src/parser/mod.rs b/compiler/noirc_frontend/src/parser/mod.rs index 2995e90ab01..66be0fdced5 100644 --- a/compiler/noirc_frontend/src/parser/mod.rs +++ b/compiler/noirc_frontend/src/parser/mod.rs @@ -26,8 +26,8 @@ use noirc_errors::Span; pub use parser::path::path_no_turbofish; pub use parser::traits::trait_bound; pub use parser::{ - block, expression, fresh_statement, lvalue, parse_program, parse_type, pattern, - top_level_items, visibility, + block, expression, fresh_statement, lvalue, module, parse_program, parse_type, pattern, + top_level_items, top_level_statement, visibility, }; #[derive(Debug, Clone)] diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 1aee697aa88..48d25e7a1d8 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -175,7 +175,7 @@ fn program() -> impl NoirParser { /// module: top_level_statement module /// | %empty -fn module() -> impl NoirParser { +pub fn module() -> impl NoirParser { recursive(|module_parser| { empty() .to(ParsedModule::default()) @@ -202,7 +202,7 @@ pub fn top_level_items() -> impl NoirParser> { /// | module_declaration /// | use_statement /// | global_declaration -fn top_level_statement<'a>( +pub fn top_level_statement<'a>( module_parser: impl NoirParser + 'a, ) -> impl NoirParser + 'a { choice(( diff --git a/docs/docs/noir/standard_library/meta/module.md b/docs/docs/noir/standard_library/meta/module.md index 870e366461c..eddaec0b02b 100644 --- a/docs/docs/noir/standard_library/meta/module.md +++ b/docs/docs/noir/standard_library/meta/module.md @@ -8,6 +8,12 @@ declarations in the source program. ## Methods +### add_item + +#include_code add_item noir_stdlib/src/meta/module.nr rust + +Adds a top-level item to the module. + ### name #include_code name noir_stdlib/src/meta/module.nr rust diff --git a/noir_stdlib/src/meta/module.nr b/noir_stdlib/src/meta/module.nr index b3f76812b8a..b766a0b630c 100644 --- a/noir_stdlib/src/meta/module.nr +++ b/noir_stdlib/src/meta/module.nr @@ -1,4 +1,9 @@ impl Module { + #[builtin(module_add_item)] + // docs:start:add_item + fn add_item(self, item: Quoted) -> bool {} + // docs:end:add_item + #[builtin(module_has_named_attribute)] // docs:start:has_named_attribute fn has_named_attribute(self, name: Quoted) -> bool {} diff --git a/test_programs/compile_success_empty/comptime_module/src/main.nr b/test_programs/compile_success_empty/comptime_module/src/main.nr index 1d1690c4017..8fe35479266 100644 --- a/test_programs/compile_success_empty/comptime_module/src/main.nr +++ b/test_programs/compile_success_empty/comptime_module/src/main.nr @@ -42,6 +42,13 @@ fn outer_attribute_separate_module(m: Module) { increment_counter(); } +#[add_function] +mod add_to_me {} + +fn add_function(m: Module) { + m.add_item(quote { pub fn added_function() {} }); +} + fn main() { comptime { @@ -73,6 +80,8 @@ fn main() { yet_another_module::generated_outer_function(); yet_another_module::generated_inner_function(); + + add_to_me::added_function(); } // docs:start:as_module_example From 4f223b364079168e2c85aa1a894d6b46b90bc5b3 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 5 Sep 2024 17:02:09 -0300 Subject: [PATCH 2/6] Try something... it still doesn't work --- .../noirc_frontend/src/elaborator/comptime.rs | 33 +++++++++++++++++++ .../src/hir/comptime/interpreter.rs | 12 +++++++ .../src/hir/comptime/interpreter/builtin.rs | 16 +++------ noir_stdlib/src/meta/module.nr | 2 +- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index 20a3d4e09ba..dd5958e6422 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -89,6 +89,39 @@ impl<'context> Elaborator<'context> { result } + pub fn elaborate_item_in_module<'a, T>( + &'a mut self, + module: ModuleId, + f: impl FnOnce(&mut Elaborator<'a>) -> T, + ) -> T { + // Create a fresh elaborator to ensure no state is changed from + // this elaborator + let mut elaborator = Elaborator::new( + self.interner, + self.def_maps, + module.krate, + self.debug_comptime_in_file, + self.enable_arithmetic_generics, + self.interpreter_call_stack.clone(), + ); + + elaborator.function_context.push(FunctionContext::default()); + elaborator.scopes.start_function(); + + elaborator.current_item = None; + elaborator.crate_id = module.krate; + elaborator.local_module = module.local_id; + // TODO: elaborator.file = meta.source_file; + + elaborator.populate_scope_from_comptime_scopes(); + + let result = f(&mut elaborator); + elaborator.check_and_pop_function_context(); + + self.errors.append(&mut elaborator.errors); + result + } + fn populate_scope_from_comptime_scopes(&mut self) { // Take the comptime scope to be our runtime scope. // Iterate from global scope to the most local scope so that the diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 9f559b7c5e6..72d88ab1155 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -10,6 +10,7 @@ use rustc_hash::FxHashMap as HashMap; use crate::ast::{BinaryOpKind, FunctionKind, IntegerBitSize, Signedness}; use crate::elaborator::Elaborator; use crate::graph::CrateId; +use crate::hir::def_map::ModuleId; use crate::hir_def::expr::ImplKind; use crate::hir_def::function::FunctionBody; use crate::macros_api::UnaryOp; @@ -194,6 +195,17 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { result } + fn elaborate_in_module( + &mut self, + module: ModuleId, + f: impl FnOnce(&mut Elaborator) -> T, + ) -> T { + self.unbind_generics_from_previous_function(); + let result = self.elaborator.elaborate_item_in_module(module, f); + self.rebind_generics_from_previous_function(); + result + } + fn call_special( &mut self, function: FuncId, diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index a8a40de0944..802754bbb8c 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -25,22 +25,12 @@ use crate::{ FunctionReturnType, IntegerBitSize, LValue, Literal, Statement, StatementKind, UnaryOp, UnresolvedType, UnresolvedTypeData, Visibility, }, -<<<<<<< Updated upstream hir::comptime::{ errors::IResult, value::{ExprValue, TypedExpr}, InterpreterError, Value, -======= - elaborator::{self, Elaborator}, - hir::{ - comptime::{ - errors::IResult, - value::{ExprValue, TypedExpr}, - InterpreterError, Value, - }, - def_collector::dc_crate::CollectedItems, ->>>>>>> Stashed changes }, + hir::def_collector::dc_crate::CollectedItems, hir_def::function::FunctionBody, lexer::Lexer, macros_api::{HirExpression, HirLiteral, Ident, ModuleDefId, NodeInterner, Signedness}, @@ -2036,7 +2026,9 @@ fn module_add_item( // How to do this? let mut generated_items = CollectedItems::default(); - interpreter.elaborator.add_item(top_level_statement, &mut generated_items, location); + interpreter.elaborate_in_module(module_id, |elaborator| { + elaborator.add_item(top_level_statement, &mut generated_items, location); + }); if !generated_items.is_empty() { interpreter.elaborate_item(interpreter.current_function, |elaborator| { diff --git a/noir_stdlib/src/meta/module.nr b/noir_stdlib/src/meta/module.nr index b766a0b630c..bee6612e1bf 100644 --- a/noir_stdlib/src/meta/module.nr +++ b/noir_stdlib/src/meta/module.nr @@ -1,7 +1,7 @@ impl Module { #[builtin(module_add_item)] // docs:start:add_item - fn add_item(self, item: Quoted) -> bool {} + fn add_item(self, item: Quoted) {} // docs:end:add_item #[builtin(module_has_named_attribute)] From bac846bdb00bc43be3f43a8bffb9afd1279756de Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 08:10:02 -0300 Subject: [PATCH 3/6] Type-check added item as if it's inside the module we are adding to --- .../noirc_frontend/src/elaborator/comptime.rs | 5 ++-- .../src/hir/comptime/interpreter.rs | 4 +++- .../src/hir/comptime/interpreter/builtin.rs | 24 +++++++++---------- .../docs/noir/standard_library/meta/module.md | 4 +++- .../comptime_module/src/main.nr | 15 +++++++++--- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index dd5958e6422..7f93aa62b2f 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -92,6 +92,7 @@ impl<'context> Elaborator<'context> { pub fn elaborate_item_in_module<'a, T>( &'a mut self, module: ModuleId, + file: FileId, f: impl FnOnce(&mut Elaborator<'a>) -> T, ) -> T { // Create a fresh elaborator to ensure no state is changed from @@ -99,7 +100,7 @@ impl<'context> Elaborator<'context> { let mut elaborator = Elaborator::new( self.interner, self.def_maps, - module.krate, + self.crate_id, self.debug_comptime_in_file, self.enable_arithmetic_generics, self.interpreter_call_stack.clone(), @@ -111,7 +112,7 @@ impl<'context> Elaborator<'context> { elaborator.current_item = None; elaborator.crate_id = module.krate; elaborator.local_module = module.local_id; - // TODO: elaborator.file = meta.source_file; + elaborator.file = file; elaborator.populate_scope_from_comptime_scopes(); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 72d88ab1155..64069e89edc 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use std::{collections::hash_map::Entry, rc::Rc}; use acvm::{acir::AcirField, FieldElement}; +use fm::FileId; use im::Vector; use iter_extended::try_vecmap; use noirc_errors::Location; @@ -198,10 +199,11 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { fn elaborate_in_module( &mut self, module: ModuleId, + file: FileId, f: impl FnOnce(&mut Elaborator) -> T, ) -> T { self.unbind_generics_from_previous_function(); - let result = self.elaborator.elaborate_item_in_module(module, f); + let result = self.elaborator.elaborate_item_in_module(module, file, f); self.rebind_generics_from_previous_function(); result } diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 802754bbb8c..5e78cd68caa 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -2017,24 +2017,22 @@ fn module_add_item( ) -> IResult { let (self_argument, item) = check_two_arguments(arguments, location)?; let module_id = get_module(self_argument)?; + let module_data = interpreter.elaborator.get_module(module_id); - let parser = parser::top_level_statement(parser::module()); - let top_level_statement = parse(item, parser, "a top-level item")?; + let parser = parser::top_level_items(); + let top_level_statements = parse(item, parser, "a top-level item")?; - // TODO: we need to type-check the top-level statement relative to the current function - // (interpreter.current_function) but we need to add it to the given module (module_id). - // How to do this? + interpreter.elaborate_in_module(module_id, module_data.location.file, |elaborator| { + let mut generated_items = CollectedItems::default(); - let mut generated_items = CollectedItems::default(); - interpreter.elaborate_in_module(module_id, |elaborator| { - elaborator.add_item(top_level_statement, &mut generated_items, location); - }); + for top_level_statement in top_level_statements { + elaborator.add_item(top_level_statement, &mut generated_items, location); + } - if !generated_items.is_empty() { - interpreter.elaborate_item(interpreter.current_function, |elaborator| { + if !generated_items.is_empty() { elaborator.elaborate_items(generated_items); - }); - } + } + }); Ok(Value::Unit) } diff --git a/docs/docs/noir/standard_library/meta/module.md b/docs/docs/noir/standard_library/meta/module.md index eddaec0b02b..380760c42f7 100644 --- a/docs/docs/noir/standard_library/meta/module.md +++ b/docs/docs/noir/standard_library/meta/module.md @@ -12,7 +12,9 @@ declarations in the source program. #include_code add_item noir_stdlib/src/meta/module.nr rust -Adds a top-level item to the module. +Adds a top-level item to the module. Adding multiple items in one go is also +valid if the `Quoted` valuee has multiple items in it. Note that the items +are type-checked as if they are inside the module they are being added to. ### name diff --git a/test_programs/compile_success_empty/comptime_module/src/main.nr b/test_programs/compile_success_empty/comptime_module/src/main.nr index 8fe35479266..baf45c517ed 100644 --- a/test_programs/compile_success_empty/comptime_module/src/main.nr +++ b/test_programs/compile_success_empty/comptime_module/src/main.nr @@ -42,11 +42,20 @@ fn outer_attribute_separate_module(m: Module) { increment_counter(); } +struct Foo {} + #[add_function] -mod add_to_me {} +mod add_to_me { + fn add_to_me_function() {} +} fn add_function(m: Module) { - m.add_item(quote { pub fn added_function() {} }); + m.add_item( + quote { pub fn added_function() -> super::Foo { + add_to_me_function(); + super::Foo {} + } } + ); } fn main() { @@ -81,7 +90,7 @@ fn main() { yet_another_module::generated_outer_function(); yet_another_module::generated_inner_function(); - add_to_me::added_function(); + let _ = add_to_me::added_function(); } // docs:start:as_module_example From 1ca299a50653ef482e755b3f237edc6109faeec3 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 08:11:40 -0300 Subject: [PATCH 4/6] Explain what an item is #Please enter the commit message for your changes. Lines starting --- docs/docs/noir/standard_library/meta/module.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/noir/standard_library/meta/module.md b/docs/docs/noir/standard_library/meta/module.md index 380760c42f7..de042760d51 100644 --- a/docs/docs/noir/standard_library/meta/module.md +++ b/docs/docs/noir/standard_library/meta/module.md @@ -12,9 +12,9 @@ declarations in the source program. #include_code add_item noir_stdlib/src/meta/module.nr rust -Adds a top-level item to the module. Adding multiple items in one go is also -valid if the `Quoted` valuee has multiple items in it. Note that the items -are type-checked as if they are inside the module they are being added to. +Adds a top-level item (a function, a struct, a global, etc.) to the module. +Adding multiple items in one go is also valid if the `Quoted` value has multiple items in it. +Note that the items are type-checked as if they are inside the module they are being added to. ### name From e619df605aec5b7951d7f218f5f0872fc4389b27 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 08:16:45 -0300 Subject: [PATCH 5/6] Remove duplicate code --- .../noirc_frontend/src/elaborator/comptime.rs | 62 ++++++++----------- .../src/hir/comptime/interpreter.rs | 10 +-- .../src/hir/comptime/interpreter/builtin.rs | 51 ++++++++------- 3 files changed, 58 insertions(+), 65 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index 7f93aa62b2f..f94834711d7 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -52,48 +52,41 @@ impl<'context> Elaborator<'context> { /// Elaborate an expression from the middle of a comptime scope. /// When this happens we require additional information to know /// what variables should be in scope. - pub fn elaborate_item_from_comptime<'a, T>( + pub fn elaborate_item_from_comptime_in_function<'a, T>( &'a mut self, current_function: Option, f: impl FnOnce(&mut Elaborator<'a>) -> T, ) -> T { - // Create a fresh elaborator to ensure no state is changed from - // this elaborator - let mut elaborator = Elaborator::new( - self.interner, - self.def_maps, - self.crate_id, - self.debug_comptime_in_file, - self.enable_arithmetic_generics, - self.interpreter_call_stack.clone(), - ); - - elaborator.function_context.push(FunctionContext::default()); - elaborator.scopes.start_function(); - - if let Some(function) = current_function { - let meta = elaborator.interner.function_meta(&function); - elaborator.current_item = Some(DependencyId::Function(function)); - elaborator.crate_id = meta.source_crate; - elaborator.local_module = meta.source_module; - elaborator.file = meta.source_file; - elaborator.introduce_generics_into_scope(meta.all_generics.clone()); - } - - elaborator.populate_scope_from_comptime_scopes(); - - let result = f(&mut elaborator); - elaborator.check_and_pop_function_context(); - - self.errors.append(&mut elaborator.errors); - result + self.elaborate_item_from_comptime(f, |elaborator| { + if let Some(function) = current_function { + let meta = elaborator.interner.function_meta(&function); + elaborator.current_item = Some(DependencyId::Function(function)); + elaborator.crate_id = meta.source_crate; + elaborator.local_module = meta.source_module; + elaborator.file = meta.source_file; + elaborator.introduce_generics_into_scope(meta.all_generics.clone()); + } + }) } - pub fn elaborate_item_in_module<'a, T>( + pub fn elaborate_item_from_comptime_in_module<'a, T>( &'a mut self, module: ModuleId, file: FileId, f: impl FnOnce(&mut Elaborator<'a>) -> T, + ) -> T { + self.elaborate_item_from_comptime(f, |elaborator| { + elaborator.current_item = None; + elaborator.crate_id = module.krate; + elaborator.local_module = module.local_id; + elaborator.file = file; + }) + } + + fn elaborate_item_from_comptime<'a, T>( + &'a mut self, + f: impl FnOnce(&mut Elaborator<'a>) -> T, + setup: impl FnOnce(&mut Elaborator<'a>), ) -> T { // Create a fresh elaborator to ensure no state is changed from // this elaborator @@ -109,10 +102,7 @@ impl<'context> Elaborator<'context> { elaborator.function_context.push(FunctionContext::default()); elaborator.scopes.start_function(); - elaborator.current_item = None; - elaborator.crate_id = module.krate; - elaborator.local_module = module.local_id; - elaborator.file = file; + setup(&mut elaborator); elaborator.populate_scope_from_comptime_scopes(); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 64069e89edc..5f58c18d66e 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -172,7 +172,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { Some(body) => Ok(body), None => { if matches!(&meta.function_body, FunctionBody::Unresolved(..)) { - self.elaborate_item(None, |elaborator| { + self.elaborate_in_function(None, |elaborator| { elaborator.elaborate_function(function); }); @@ -185,13 +185,13 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { } } - fn elaborate_item( + fn elaborate_in_function( &mut self, function: Option, f: impl FnOnce(&mut Elaborator) -> T, ) -> T { self.unbind_generics_from_previous_function(); - let result = self.elaborator.elaborate_item_from_comptime(function, f); + let result = self.elaborator.elaborate_item_from_comptime_in_function(function, f); self.rebind_generics_from_previous_function(); result } @@ -203,7 +203,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { f: impl FnOnce(&mut Elaborator) -> T, ) -> T { self.unbind_generics_from_previous_function(); - let result = self.elaborator.elaborate_item_in_module(module, file, f); + let result = self.elaborator.elaborate_item_from_comptime_in_module(module, file, f); self.rebind_generics_from_previous_function(); result } @@ -1258,7 +1258,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { let mut result = self.call_function(function_id, arguments, bindings, location)?; if call.is_macro_call { let expr = result.into_expression(self.elaborator.interner, location)?; - let expr = self.elaborate_item(self.current_function, |elaborator| { + let expr = self.elaborate_in_function(self.current_function, |elaborator| { elaborator.elaborate_expression(expr).0 }); result = self.evaluate(expr)?; diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 5e78cd68caa..3fcdeae3f64 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -569,9 +569,10 @@ fn quoted_as_module( let path = parse(argument, parser::path_no_turbofish(), "a path").ok(); let option_value = path.and_then(|path| { - let module = interpreter.elaborate_item(interpreter.current_function, |elaborator| { - elaborator.resolve_module_by_path(path) - }); + let module = interpreter + .elaborate_in_function(interpreter.current_function, |elaborator| { + elaborator.resolve_module_by_path(path) + }); module.map(Value::ModuleDefinition) }); @@ -587,7 +588,7 @@ fn quoted_as_trait_constraint( let argument = check_one_argument(arguments, location)?; let trait_bound = parse(argument, parser::trait_bound(), "a trait constraint")?; let bound = interpreter - .elaborate_item(interpreter.current_function, |elaborator| { + .elaborate_in_function(interpreter.current_function, |elaborator| { elaborator.resolve_trait_bound(&trait_bound, Type::Unit) }) .ok_or(InterpreterError::FailedToResolveTraitBound { trait_bound, location })?; @@ -603,8 +604,8 @@ fn quoted_as_type( ) -> IResult { let argument = check_one_argument(arguments, location)?; let typ = parse(argument, parser::parse_type(), "a type")?; - let typ = - interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ)); + let typ = interpreter + .elaborate_in_function(interpreter.current_function, |elab| elab.resolve_type(typ)); Ok(Value::Type(typ)) } @@ -1675,23 +1676,25 @@ fn expr_resolve( interpreter.current_function }; - let value = interpreter.elaborate_item(function_to_resolve_in, |elaborator| match expr_value { - ExprValue::Expression(expression_kind) => { - let expr = Expression { kind: expression_kind, span: self_argument_location.span }; - let (expr_id, _) = elaborator.elaborate_expression(expr); - Value::TypedExpr(TypedExpr::ExprId(expr_id)) - } - ExprValue::Statement(statement_kind) => { - let statement = Statement { kind: statement_kind, span: self_argument_location.span }; - let (stmt_id, _) = elaborator.elaborate_statement(statement); - Value::TypedExpr(TypedExpr::StmtId(stmt_id)) - } - ExprValue::LValue(lvalue) => { - let expr = lvalue.as_expression(); - let (expr_id, _) = elaborator.elaborate_expression(expr); - Value::TypedExpr(TypedExpr::ExprId(expr_id)) - } - }); + let value = + interpreter.elaborate_in_function(function_to_resolve_in, |elaborator| match expr_value { + ExprValue::Expression(expression_kind) => { + let expr = Expression { kind: expression_kind, span: self_argument_location.span }; + let (expr_id, _) = elaborator.elaborate_expression(expr); + Value::TypedExpr(TypedExpr::ExprId(expr_id)) + } + ExprValue::Statement(statement_kind) => { + let statement = + Statement { kind: statement_kind, span: self_argument_location.span }; + let (stmt_id, _) = elaborator.elaborate_statement(statement); + Value::TypedExpr(TypedExpr::StmtId(stmt_id)) + } + ExprValue::LValue(lvalue) => { + let expr = lvalue.as_expression(); + let (expr_id, _) = elaborator.elaborate_expression(expr); + Value::TypedExpr(TypedExpr::ExprId(expr_id)) + } + }); Ok(value) } @@ -1942,7 +1945,7 @@ fn function_def_set_parameters( "a pattern", )?; - let hir_pattern = interpreter.elaborate_item(Some(func_id), |elaborator| { + let hir_pattern = interpreter.elaborate_in_function(Some(func_id), |elaborator| { elaborator.elaborate_pattern_and_store_ids( parameter_pattern, parameter_type.clone(), From 3986a3f7ed281a7942a47d2f9f1f14684b541758 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 6 Sep 2024 14:09:59 -0300 Subject: [PATCH 6/6] cargo fmt --- compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 2593d1f5964..5943e352510 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -25,6 +25,7 @@ use crate::{ FunctionReturnType, IntegerBitSize, LValue, Literal, Statement, StatementKind, UnaryOp, UnresolvedType, UnresolvedTypeData, Visibility, }, + hir::def_collector::dc_crate::CollectedItems, hir::{ comptime::{ errors::IResult, @@ -33,7 +34,6 @@ use crate::{ }, def_map::ModuleId, }, - hir::def_collector::dc_crate::CollectedItems, hir_def::function::FunctionBody, lexer::Lexer, macros_api::{HirExpression, HirLiteral, Ident, ModuleDefId, NodeInterner, Signedness},