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: parse negative integer literals #3698

Merged
merged 5 commits into from
Dec 7, 2023
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
14 changes: 13 additions & 1 deletion compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,10 +1684,22 @@ fn literal() -> impl NoirParser<ExpressionKind> {
})
}

fn literal_with_sign() -> impl NoirParser<ExpressionKind> {
choice((
literal(),
just(Token::Minus).then(literal()).map(|(_, exp)| match exp {
ExpressionKind::Literal(Literal::Integer(value, sign)) => {
ExpressionKind::Literal(Literal::Integer(value, !sign))
}
_ => unreachable!(),
}),
))
}

fn literal_or_collection<'a>(
expr_parser: impl ExprParser + 'a,
) -> impl NoirParser<ExpressionKind> + 'a {
choice((literal(), constructor(expr_parser.clone()), array_expr(expr_parser)))
choice((literal_with_sign(), constructor(expr_parser.clone()), array_expr(expr_parser)))
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_2660/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_2660"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_2660/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global foo = -1;

fn main(x: i32) {
let y = x + foo;
assert(y == 0);
}
Loading