From 9a954fdc486d252b8062457754fe25267e40cd1e Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 30 Jul 2024 10:54:56 -0300 Subject: [PATCH] fix: parse if statement independently of if expression in statement --- compiler/noirc_frontend/src/parser/parser.rs | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 9dc46a8b0aa..9cb698b9d07 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -466,6 +466,7 @@ where assertion::assertion_eq(expr_parser.clone()), declaration(expr_parser.clone()), assignment(expr_parser.clone()), + if_statement(expr_no_constructors.clone(), statement.clone()), block_statement(statement.clone()), for_loop(expr_no_constructors.clone(), statement.clone()), break_statement(), @@ -933,6 +934,19 @@ where }) } +fn if_statement<'a, P, S>( + expr_no_constructors: P, + statement: S, +) -> impl NoirParser + 'a +where + P: ExprParser + 'a, + S: NoirParser + 'a, +{ + if_expr(expr_no_constructors, statement).map_with_span(|expression_kind, span| { + StatementKind::Expression(Expression::new(expression_kind, span)) + }) +} + fn block_statement<'a, S>(statement: S) -> impl NoirParser + 'a where S: NoirParser + 'a, @@ -1651,4 +1665,16 @@ mod test { let block_expr = block_expr.expect("Failed to parse module"); assert_eq!(block_expr.statements.len(), 2); } + + #[test] + fn test_parses_if_statement_not_infix_expression() { + let src = r#" + { + if 1 { 2 } else { 3 } + -1 + }"#; + let (block_expr, _) = parse_recover(block(fresh_statement()), src); + let block_expr = block_expr.expect("Failed to parse module"); + assert_eq!(block_expr.statements.len(), 2); + } }