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

Correctly parse double negation. #28497

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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
8 changes: 8 additions & 0 deletions tests/expectations/compiler/field/double_negation.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace = "Compile"
expectation = "Pass"
outputs = [[{ compile = [{ initial_ast = "e7bcf3eb47bb60ca381acc31ce6e9241c4dc4012f15e6067ec0d112485f44250", unrolled_ast = "d1f265ef48af254cf931a5517c918ed2b3ddd2fddaff0f2e70b1bf53f60a7427", ssa_ast = "74a1e841eaf86568417fb9a66803b466b87fbcf43823813ef7f980299e46e402", flattened_ast = "1f4dac3dfe833f42289c00340b8b5473df8ffdd7a2c9eae4d7ca4115f4efcbf4", destructured_ast = "75d1b9d954aa0cbf2bad7e25fddcd011c713393fd35d41bb7c8dd319ecb8c5cf", inlined_ast = "75d1b9d954aa0cbf2bad7e25fddcd011c713393fd35d41bb7c8dd319ecb8c5cf", dce_ast = "cad4418236706c64f0310170308d1fbc4c95775c7cee1ba002451c04a3f5e2a9", bytecode = """
program test.aleo;

function main:
output 1field as field.private;
""", errors = "", warnings = "" }] }]]
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);
}
}
Loading