Skip to content

Commit

Permalink
fix: add location to monomorphizer's ast::Unary
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvitkov committed Aug 25, 2023
1 parent 274b06b commit 1b47c85
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
9 changes: 3 additions & 6 deletions crates/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,26 +291,23 @@ impl<'a> FunctionContext<'a> {
mut lhs: ValueId,
operator: noirc_frontend::BinaryOpKind,
mut rhs: ValueId,
location: Option<Location>,
location: Location,
) -> Values {
let mut result = match operator {
BinaryOpKind::ShiftLeft => self.insert_shift_left(lhs, rhs),
BinaryOpKind::ShiftRight => self.insert_shift_right(lhs, rhs),
BinaryOpKind::Equal | BinaryOpKind::NotEqual
if matches!(self.builder.type_of_value(lhs), Type::Array(..)) =>
{
return self.insert_array_equality(lhs, operator, rhs, location.unwrap())
return self.insert_array_equality(lhs, operator, rhs, location)
}
_ => {
let op = convert_operator(operator);
if operator_requires_swapped_operands(operator) {
std::mem::swap(&mut lhs, &mut rhs);
}

if let Some(location) = location {
self.builder.set_location(location);
}
self.builder.insert_binary(lhs, op, rhs)
self.builder.set_location(location).insert_binary(lhs, op, rhs)
}
};

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a> FunctionContext<'a> {
let rhs = rhs.into_leaf().eval(self);
let typ = self.builder.type_of_value(rhs);
let zero = self.builder.numeric_constant(0u128, typ);
self.insert_binary(zero, noirc_frontend::BinaryOpKind::Subtract, rhs, None)
self.insert_binary(zero, noirc_frontend::BinaryOpKind::Subtract, rhs, unary.location)
}
noirc_frontend::UnaryOp::MutableReference => {
self.codegen_reference(&unary.rhs).map(|rhs| {
Expand Down Expand Up @@ -252,7 +252,7 @@ impl<'a> FunctionContext<'a> {
fn codegen_binary(&mut self, binary: &ast::Binary) -> Values {
let lhs = self.codegen_non_tuple_expression(&binary.lhs);
let rhs = self.codegen_non_tuple_expression(&binary.rhs);
self.insert_binary(lhs, binary.operator, rhs, Some(binary.location))
self.insert_binary(lhs, binary.operator, rhs, binary.location)
}

fn codegen_index(&mut self, index: &ast::Index) -> Values {
Expand Down
6 changes: 6 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,15 @@ impl<'interner> TypeChecker<'interner> {
// inserted by a field access expression `foo.bar` on a mutable reference `foo`.
if self.try_remove_implicit_dereference(method_call.object).is_none() {
// If that didn't work, then wrap the whole expression in an `&mut`
let location = self.interner.id_location(method_call.object);

method_call.object =
self.interner.push_expr(HirExpression::Prefix(HirPrefixExpression {
operator: UnaryOp::MutableReference,
rhs: method_call.object,
}));
self.interner.push_expr_type(&method_call.object, new_type);
self.interner.push_expr_location(method_call.object, location.span, location.file);
}
}
}
Expand Down Expand Up @@ -567,6 +570,9 @@ impl<'interner> TypeChecker<'interner> {
}));
this.interner.push_expr_type(&old_lhs, lhs_type);
this.interner.push_expr_type(access_lhs, element);

let old_location = this.interner.id_location(old_lhs);
this.interner.push_expr_location(*access_lhs, span, old_location.file);
};

match self.check_field_access(&lhs_type, &access.rhs.0.contents, span, dereference_lhs) {
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct Unary {
pub operator: crate::UnaryOp,
pub rhs: Box<Expression>,
pub result_type: Type,
pub location: Location,
}

pub type BinaryOp = BinaryOpKind;
Expand Down
39 changes: 24 additions & 15 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,15 @@ impl<'interner> Monomorphizer<'interner> {
HirExpression::Literal(HirLiteral::Unit) => ast::Expression::Block(vec![]),
HirExpression::Block(block) => self.block(block.0),

HirExpression::Prefix(prefix) => ast::Expression::Unary(ast::Unary {
operator: prefix.operator,
rhs: Box::new(self.expr(prefix.rhs)),
result_type: Self::convert_type(&self.interner.id_type(expr)),
}),
HirExpression::Prefix(prefix) => {
let location = self.interner.expr_location(&expr);
ast::Expression::Unary(ast::Unary {
operator: prefix.operator,
rhs: Box::new(self.expr(prefix.rhs)),
result_type: Self::convert_type(&self.interner.id_type(expr)),
location,
})
}

HirExpression::Infix(infix) => {
let lhs = Box::new(self.expr(infix.lhs));
Expand Down Expand Up @@ -816,7 +820,7 @@ impl<'interner> Monomorphizer<'interner> {
};

let call = self
.try_evaluate_call(&func, &return_type)
.try_evaluate_call(&func, &id, &return_type)
.unwrap_or(ast::Expression::Call(ast::Call { func, arguments, return_type, location }));

if !block_expressions.is_empty() {
Expand Down Expand Up @@ -897,6 +901,7 @@ impl<'interner> Monomorphizer<'interner> {
fn try_evaluate_call(
&mut self,
func: &ast::Expression,
expr_id: &node_interner::ExprId,
result_type: &ast::Type,
) -> Option<ast::Expression> {
if let ast::Expression::Ident(ident) = func {
Expand All @@ -907,7 +912,10 @@ impl<'interner> Monomorphizer<'interner> {
(FieldElement::max_num_bits() as u128).into(),
ast::Type::Field,
))),
"zeroed" => Some(self.zeroed_value_of_type(result_type)),
"zeroed" => {
let location = self.interner.expr_location(expr_id);
Some(self.zeroed_value_of_type(result_type, location))
}
"modulus_le_bits" => {
let bits = FieldElement::modulus().to_radix_le(2);
Some(self.modulus_array_literal(bits, 1))
Expand Down Expand Up @@ -1179,7 +1187,7 @@ impl<'interner> Monomorphizer<'interner> {
/// Implements std::unsafe::zeroed by returning an appropriate zeroed
/// ast literal or collection node for the given type. Note that for functions
/// there is no obvious zeroed value so this should be considered unsafe to use.
fn zeroed_value_of_type(&mut self, typ: &ast::Type) -> ast::Expression {
fn zeroed_value_of_type(&mut self, typ: &ast::Type, location: noirc_errors::Location) -> ast::Expression {
match typ {
ast::Type::Field | ast::Type::Integer(..) => {
ast::Expression::Literal(ast::Literal::Integer(0_u128.into(), typ.clone()))
Expand All @@ -1189,7 +1197,7 @@ impl<'interner> Monomorphizer<'interner> {
// anyway.
ast::Type::Unit => ast::Expression::Literal(ast::Literal::Bool(false)),
ast::Type::Array(length, element_type) => {
let element = self.zeroed_value_of_type(element_type.as_ref());
let element = self.zeroed_value_of_type(element_type.as_ref(), location);
ast::Expression::Literal(ast::Literal::Array(ast::ArrayLiteral {
contents: vec![element; *length as usize],
typ: ast::Type::Array(*length, element_type.clone()),
Expand All @@ -1199,7 +1207,7 @@ impl<'interner> Monomorphizer<'interner> {
ast::Expression::Literal(ast::Literal::Str("\0".repeat(*length as usize)))
}
ast::Type::FmtString(length, fields) => {
let zeroed_tuple = self.zeroed_value_of_type(fields);
let zeroed_tuple = self.zeroed_value_of_type(fields, location);
let fields_len = match &zeroed_tuple {
ast::Expression::Tuple(fields) => fields.len() as u64,
_ => unreachable!("ICE: format string fields should be structured in a tuple, but got a {zeroed_tuple}"),
Expand All @@ -1211,10 +1219,10 @@ impl<'interner> Monomorphizer<'interner> {
))
}
ast::Type::Tuple(fields) => {
ast::Expression::Tuple(vecmap(fields, |field| self.zeroed_value_of_type(field)))
ast::Expression::Tuple(vecmap(fields, |field| self.zeroed_value_of_type(field, location)))
}
ast::Type::Function(parameter_types, ret_type, env) => {
self.create_zeroed_function(parameter_types, ret_type, env)
self.create_zeroed_function(parameter_types, ret_type, env, location)
}
ast::Type::Slice(element_type) => {
ast::Expression::Literal(ast::Literal::Array(ast::ArrayLiteral {
Expand All @@ -1224,9 +1232,9 @@ impl<'interner> Monomorphizer<'interner> {
}
ast::Type::MutableReference(element) => {
use crate::UnaryOp::MutableReference;
let rhs = Box::new(self.zeroed_value_of_type(element));
let rhs = Box::new(self.zeroed_value_of_type(element, location));
let result_type = typ.clone();
ast::Expression::Unary(ast::Unary { rhs, result_type, operator: MutableReference })
ast::Expression::Unary(ast::Unary { rhs, result_type, operator: MutableReference, location })
}
}
}
Expand All @@ -1242,14 +1250,15 @@ impl<'interner> Monomorphizer<'interner> {
parameter_types: &[ast::Type],
ret_type: &ast::Type,
env_type: &ast::Type,
location: noirc_errors::Location,
) -> ast::Expression {
let lambda_name = "zeroed_lambda";

let parameters = vecmap(parameter_types, |parameter_type| {
(self.next_local_id(), false, "_".into(), parameter_type.clone())
});

let body = self.zeroed_value_of_type(ret_type);
let body = self.zeroed_value_of_type(ret_type, location);

let id = self.next_function_id();
let return_type = ret_type.clone();
Expand Down

0 comments on commit 1b47c85

Please sign in to comment.