Skip to content

Commit

Permalink
fix: Transform hir before type checks (#2994)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Oct 5, 2023
1 parent 0e266eb commit a29b568
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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"),
}
Expand Down Expand Up @@ -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())))],
))]);

Expand Down Expand Up @@ -831,14 +818,14 @@ fn create_loop_over(var: Expression, loop_body: Vec<Statement>) -> 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 {
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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));
Expand All @@ -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::<Vec<_>>(),
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit a29b568

Please sign in to comment.