From a29b568295e40e19dd354bbe47e31f922e08d8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Rodr=C3=ADguez?= Date: Thu, 5 Oct 2023 14:50:52 +0200 Subject: [PATCH] fix: Transform hir before type checks (#2994) --- .../src/hir/{def_map => }/aztec_library.rs | 31 ++++++------------- .../src/hir/def_collector/dc_crate.rs | 5 +++ .../noirc_frontend/src/hir/def_map/mod.rs | 7 +---- compiler/noirc_frontend/src/hir/mod.rs | 3 ++ 4 files changed, 18 insertions(+), 28 deletions(-) rename compiler/noirc_frontend/src/hir/{def_map => }/aztec_library.rs (96%) diff --git a/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs b/compiler/noirc_frontend/src/hir/aztec_library.rs similarity index 96% rename from compiler/noirc_frontend/src/hir/def_map/aztec_library.rs rename to compiler/noirc_frontend/src/hir/aztec_library.rs index 384bb65deff..2b79d3c0edb 100644 --- a/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs +++ b/compiler/noirc_frontend/src/hir/aztec_library.rs @@ -10,18 +10,17 @@ use crate::node_interner::{NodeInterner, StructId}; use crate::token::SecondaryAttribute; use crate::{ hir::Context, BlockExpression, CallExpression, CastExpression, Distinctness, Expression, - ExpressionKind, ForExpression, FunctionReturnType, Ident, ImportStatement, IndexExpression, - LetStatement, Literal, MemberAccessExpression, MethodCallExpression, NoirFunction, - ParsedModule, Path, PathKind, Pattern, Statement, UnresolvedType, UnresolvedTypeData, - Visibility, + ExpressionKind, FunctionReturnType, Ident, ImportStatement, IndexExpression, LetStatement, + Literal, MemberAccessExpression, MethodCallExpression, NoirFunction, ParsedModule, Path, + PathKind, Pattern, Statement, UnresolvedType, UnresolvedTypeData, Visibility, }; use crate::{ - FunctionDefinition, NoirStruct, PrefixExpression, Shared, Signedness, StructType, Type, - TypeBinding, TypeImpl, TypeVariableKind, UnaryOp, + ForLoopStatement, FunctionDefinition, NoirStruct, PrefixExpression, Signedness, StructType, + Type, TypeImpl, UnaryOp, }; use fm::FileId; -use super::ModuleDefId; +use super::def_map::ModuleDefId; // // Helper macros for creating noir ast nodes @@ -373,7 +372,6 @@ fn transform_event(struct_id: StructId, interner: &mut NodeInterner) { if signature == SIGNATURE_PLACEHOLDER => { let selector_literal_id = first_arg_id; - let compute_selector_call_id = compute_selector_expression.func; let structure = interner.get_struct(struct_id); let signature = event_signature(&structure.borrow()); @@ -386,17 +384,6 @@ fn transform_event(struct_id: StructId, interner: &mut NodeInterner) { selector_literal_id, Type::String(Box::new(Type::Constant(signature.len() as u64))), ); - interner.push_expr_type( - &compute_selector_call_id, - Type::Function( - vec![Type::String(Box::new(Type::TypeVariable( - Shared::new(TypeBinding::Bound(Type::Constant(signature.len() as u64))), - TypeVariableKind::Normal, - )))], - Box::new(Type::FieldElement), - Box::new(Type::Unit), - ), - ); } _ => unreachable!("Signature placeholder literal does not match"), } @@ -431,7 +418,7 @@ fn generate_selector_impl(structure: &mut NoirStruct) -> TypeImpl { let struct_type = make_type(UnresolvedTypeData::Named(path(structure.name.clone()), vec![])); let selector_fun_body = BlockExpression(vec![Statement::Expression(call( - variable_path(chained_path!("aztec", "oracle", "compute_selector", "compute_selector")), + variable_path(chained_path!("aztec", "selector", "compute_selector")), vec![expression(ExpressionKind::Literal(Literal::Str(SIGNATURE_PLACEHOLDER.to_string())))], ))]); @@ -831,14 +818,14 @@ fn create_loop_over(var: Expression, loop_body: Vec) -> Statement { let for_loop_block = expression(ExpressionKind::Block(BlockExpression(loop_body))); // `for i in 0..{ident}.len()` - Statement::Expression(expression(ExpressionKind::For(Box::new(ForExpression { + Statement::For(ForLoopStatement { identifier: ident("i"), start_range: expression(ExpressionKind::Literal(Literal::Integer(FieldElement::from( i128::from(0), )))), end_range: end_range_expression, block: for_loop_block, - })))) + }) } fn add_array_to_hasher(identifier: &Ident) -> Statement { diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index eef3bbb3700..71f13339be5 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -336,6 +336,11 @@ impl DefCollector { ); errors.extend(resolved_globals.errors); + + // We run hir transformations before type checks + #[cfg(feature = "aztec")] + crate::hir::aztec_library::transform_hir(&crate_id, context); + errors.extend(type_check_globals(&mut context.def_interner, resolved_globals.globals)); // Type check all of the functions in the crate diff --git a/compiler/noirc_frontend/src/hir/def_map/mod.rs b/compiler/noirc_frontend/src/hir/def_map/mod.rs index 3ea75c57865..34485194a85 100644 --- a/compiler/noirc_frontend/src/hir/def_map/mod.rs +++ b/compiler/noirc_frontend/src/hir/def_map/mod.rs @@ -17,9 +17,6 @@ pub use module_data::*; mod namespace; pub use namespace::*; -#[cfg(feature = "aztec")] -mod aztec_library; - /// The name that is used for a non-contract program's entry-point function. pub const MAIN_FUNCTION: &str = "main"; @@ -88,7 +85,7 @@ impl CrateDefMap { let (ast, parsing_errors) = parse_file(&context.file_manager, root_file_id); #[cfg(feature = "aztec")] - let ast = match aztec_library::transform(ast, &crate_id, context) { + let ast = match super::aztec_library::transform(ast, &crate_id, context) { Ok(ast) => ast, Err((error, file_id)) => { errors.push((error.into(), file_id)); @@ -110,8 +107,6 @@ impl CrateDefMap { // Now we want to populate the CrateDefMap using the DefCollector errors.extend(DefCollector::collect(def_map, context, ast, root_file_id)); - #[cfg(feature = "aztec")] - aztec_library::transform_hir(&crate_id, context); errors.extend( parsing_errors.iter().map(|e| (e.clone().into(), root_file_id)).collect::>(), diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index 9c747bfead1..aabb0e82daa 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -4,6 +4,9 @@ pub mod resolution; pub mod scope; pub mod type_check; +#[cfg(feature = "aztec")] +pub(crate) mod aztec_library; + use crate::graph::{CrateGraph, CrateId, Dependency}; use crate::hir_def::function::FuncMeta; use crate::node_interner::{FuncId, NodeInterner, StructId};