diff --git a/compiler/parser/src/parser/expression.rs b/compiler/parser/src/parser/expression.rs index 9148834beb..b9157db130 100644 --- a/compiler/parser/src/parser/expression.rs +++ b/compiler/parser/src/parser/expression.rs @@ -268,7 +268,7 @@ impl 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(); @@ -276,20 +276,20 @@ impl ParserContext<'_, N> { 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. diff --git a/tests/tests/compiler/field/double_negation.leo b/tests/tests/compiler/field/double_negation.leo new file mode 100644 index 0000000000..3a1fd8d6ac --- /dev/null +++ b/tests/tests/compiler/field/double_negation.leo @@ -0,0 +1,11 @@ +/* +namespace = "Compile" +expectation = "Pass" +*/ + +program test.aleo { + transition main() -> field { + let a: field = -(-1field); + return 1field * -(-1field); + } +}