Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssa): codegen missing check for unary minus #2413

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unary_operators"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let x = -1;
assert(x == 1 - 2);

let y: i32 = -1;
assert(x == 1 - 2);
}
10 changes: 7 additions & 3 deletions crates/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,26 @@ impl<'a> FunctionContext<'a> {
mut lhs: ValueId,
operator: noirc_frontend::BinaryOpKind,
mut rhs: ValueId,
location: Location,
location: Option<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)
return self.insert_array_equality(lhs, operator, rhs, location.unwrap())
}
_ => {
let op = convert_operator(operator);
if operator_requires_swapped_operands(operator) {
std::mem::swap(&mut lhs, &mut rhs);
}
self.builder.set_location(location).insert_binary(lhs, op, rhs)

if let Some(location) = location {
self.builder.set_location(location);
}
self.builder.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.builder.insert_binary(zero, BinaryOp::Sub, rhs).into()
self.insert_binary(zero, noirc_frontend::BinaryOpKind::Subtract, rhs, None)
}
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, binary.location)
self.insert_binary(lhs, binary.operator, rhs, Some(binary.location))
}

fn codegen_index(&mut self, index: &ast::Index) -> Values {
Expand Down