Skip to content

Commit

Permalink
Correctly parse double negation.
Browse files Browse the repository at this point in the history
It should be a unary operation followed by a negative value.

Fixes #2555
  • Loading branch information
mikebenfield committed Jan 30, 2025
1 parent ab09981 commit 7c45dfa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/parser/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,28 +268,28 @@ impl<N: Network> ParserContext<'_, N> {
// If the last operation is a negation and the inner expression is a literal, then construct a negative literal.
if let Some((UnaryOperation::Negate, _)) = ops.last() {
match inner {
Expression::Literal(Literal::Integer(integer_type, string, span, id)) => {
Expression::Literal(Literal::Integer(integer_type, string, span, id)) if !string.starts_with('-') => {
// Remove the negation from the operations.
// Note that this unwrap is safe because there is at least one operation in `ops`.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative integer literal.
inner =
Expression::Literal(Literal::Integer(integer_type, format!("-{string}"), op_span + span, id));
}
Expression::Literal(Literal::Field(string, span, id)) => {
Expression::Literal(Literal::Field(string, span, id)) if !string.starts_with('-') => {
// Remove the negation from the operations.
// Note that
let (_, op_span) = ops.pop().unwrap();
// Construct a negative field literal.
inner = Expression::Literal(Literal::Field(format!("-{string}"), op_span + span, id));
}
Expression::Literal(Literal::Group(string, span, id)) => {
Expression::Literal(Literal::Group(string, span, id)) if !string.starts_with('-') => {
// Remove the negation from the operations.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative group literal.
inner = Expression::Literal(Literal::Group(format!("-{string}"), op_span + span, id));
}
Expression::Literal(Literal::Scalar(string, span, id)) => {
Expression::Literal(Literal::Scalar(string, span, id)) if !string.starts_with('-') => {
// Remove the negation from the operations.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative scalar literal.
Expand Down
11 changes: 11 additions & 0 deletions tests/tests/compiler/field/double_negation.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
namespace = "Compile"
expectation = "Pass"
*/

program test.aleo {
transition main() -> field {
let a: field = -(-1field);
return 1field * -(-1field);
}
}

0 comments on commit 7c45dfa

Please sign in to comment.