Skip to content
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
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}}")
}
Expand Down
23 changes: 21 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -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::{
Expand Down Expand Up @@ -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());
}
}
Loading