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: allow method call after block, if and match #7655

Merged
merged 1 commit into from
Mar 11, 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
19 changes: 19 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@
self.parse_index(atom, start_location)
}

pub(super) fn parse_member_accesses_or_method_calls_after_expression(
&mut self,
mut atom: Expression,
start_location: Location,
) -> Expression {
let mut parsed;

loop {
(atom, parsed) = self.parse_member_access_or_method_call(atom, start_location);
if parsed {
continue;
} else {
break;
}
}

atom
}

/// CallExpression = Atom CallArguments
fn parse_call(&mut self, atom: Expression, start_location: Location) -> (Expression, bool) {
if let Some(call_arguments) = self.parse_call_arguments() {
Expand Down Expand Up @@ -633,7 +652,7 @@
/// = bool
/// | int
/// | str
/// | rawstr

Check warning on line 655 in compiler/noirc_frontend/src/parser/parser/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (rawstr)
/// | fmtstr
/// | QuoteExpression
/// | ArrayExpression
Expand Down
42 changes: 40 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ fn empty_body() -> BlockExpression {
mod tests {
use crate::{
ast::{
IntegerBitSize, ItemVisibility, NoirFunction, Signedness, UnresolvedTypeData,
Visibility,
ExpressionKind, IntegerBitSize, ItemVisibility, NoirFunction, Signedness,
StatementKind, UnresolvedTypeData, Visibility,
},
parse_program_with_dummy_file,
parser::{
Expand Down Expand Up @@ -570,4 +570,42 @@ mod tests {
UnresolvedTypeData::Integer(Signedness::Signed, IntegerBitSize::SixtyFour)
);
}

#[test]
fn parses_block_followed_by_call() {
let src = "fn foo() { { 1 }.bar() }";
let noir_function = parse_function_no_error(src);
let statements = &noir_function.def.body.statements;
assert_eq!(statements.len(), 1);

let StatementKind::Expression(expr) = &statements[0].kind else {
panic!("Expected expression statement");
};

let ExpressionKind::MethodCall(call) = &expr.kind else {
panic!("Expected method call expression");
};

assert!(matches!(call.object.kind, ExpressionKind::Block(_)));
assert_eq!(call.method_name.to_string(), "bar");
}

#[test]
fn parses_if_followed_by_call() {
let src = "fn foo() { if 1 { 2 } else { 3 }.bar() }";
let noir_function = parse_function_no_error(src);
let statements = &noir_function.def.body.statements;
assert_eq!(statements.len(), 1);

let StatementKind::Expression(expr) = &statements[0].kind else {
panic!("Expected expression statement");
};

let ExpressionKind::MethodCall(call) = &expr.kind else {
panic!("Expected method call expression");
};

assert!(matches!(call.object.kind, ExpressionKind::If(_)));
assert_eq!(call.method_name.to_string(), "bar");
}
}
37 changes: 23 additions & 14 deletions compiler/noirc_frontend/src/parser/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,12 @@ impl Parser<'_> {
return Some(StatementKind::While(while_));
}

if let Some(kind) = self.parse_if_expr() {
let location = self.location_since(start_location);
return Some(StatementKind::Expression(Expression { kind, location }));
}

if let Some(kind) = self.parse_match_expr() {
if let Some(kind) = self.parse_block_like() {
let location = self.location_since(start_location);
return Some(StatementKind::Expression(Expression { kind, location }));
}

if let Some(block) = self.parse_block() {
return Some(StatementKind::Expression(Expression {
kind: ExpressionKind::Block(block),
location: self.location_since(start_location),
}));
let expression = Expression { kind, location };
let expression = self
.parse_member_accesses_or_method_calls_after_expression(expression, start_location);
return Some(StatementKind::Expression(expression));
}

if let Some(token) = self.eat_kind(TokenKind::InternedLValue) {
Expand Down Expand Up @@ -214,6 +205,24 @@ impl Parser<'_> {
Some(StatementKind::Expression(expression))
}

/// Parses an expression that looks like a block (ends with '}'):
/// `{ ... }`, `if { ... }` and `match { ... }`.
fn parse_block_like(&mut self) -> Option<ExpressionKind> {
if let Some(kind) = self.parse_if_expr() {
return Some(kind);
}

if let Some(kind) = self.parse_match_expr() {
return Some(kind);
}

if let Some(block) = self.parse_block() {
return Some(ExpressionKind::Block(block));
}

None
}

fn next_is_op_assign(&mut self) -> Option<BinaryOp> {
let start_location = self.current_token_location;
let operator = if self.next_is(Token::Assign) {
Expand Down
Loading