diff --git a/compiler/noirc_frontend/src/ast/expression.rs b/compiler/noirc_frontend/src/ast/expression.rs index cd1151f9b62..6bdadac85f5 100644 --- a/compiler/noirc_frontend/src/ast/expression.rs +++ b/compiler/noirc_frontend/src/ast/expression.rs @@ -798,7 +798,7 @@ impl Display for MatchExpression { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "match {} {{", self.expression)?; for (pattern, branch) in &self.rules { - writeln!(f, " {pattern} -> {branch},")?; + writeln!(f, " {pattern} => {branch},")?; } write!(f, "}}") } diff --git a/compiler/noirc_frontend/src/parser/parser/expression.rs b/compiler/noirc_frontend/src/parser/parser/expression.rs index 27a07d6812c..fbe288de7c0 100644 --- a/compiler/noirc_frontend/src/parser/parser/expression.rs +++ b/compiler/noirc_frontend/src/parser/parser/expression.rs @@ -596,7 +596,10 @@ impl Parser<'_> { let expression = self.parse_expression_except_constructor_or_error(); - self.eat_left_brace(); + if !self.eat_left_brace() { + self.expected_token(Token::LeftBrace); + return Some(ExpressionKind::Error); + } let rules = self.parse_many( "match cases", @@ -607,7 +610,7 @@ impl Parser<'_> { Some(ExpressionKind::Match(Box::new(MatchExpression { expression, rules }))) } - /// MatchRule = Expression '->' (Block ','?) | (Expression ',') + /// MatchRule = Expression '=>' (Block ','?) | (Expression ',') fn parse_match_rule(&mut self) -> Option<(Expression, Expression)> { let pattern = self.parse_expression()?; self.eat_or_error(Token::FatArrow); @@ -1042,6 +1045,7 @@ mod tests { ArrayLiteral, BinaryOpKind, ConstrainKind, Expression, ExpressionKind, Literal, StatementKind, UnaryOp, UnresolvedTypeData, }, + parse_program_with_dummy_file, parser::{ Parser, ParserErrorReason, parser::tests::{ @@ -2221,4 +2225,19 @@ mod tests { let reason = get_single_error_reason(&parser.errors, span); assert!(matches!(reason, ParserErrorReason::ConstrainDeprecated)); } + + #[test] + fn errors_on_match_without_left_brace_after_expression() { + let src = " + fn main() { + if true { + match c _ => { + match d _ => 0, + } + } + } } } + "; + let (_, errors) = parse_program_with_dummy_file(src); + assert!(!errors.is_empty()); + } }