Parse empty return statement as a unit expression

This commit is contained in:
d0cd 2022-10-13 13:39:30 -05:00
parent 6789bc8aae
commit 34fa2cb2c6

View File

@ -116,8 +116,13 @@ impl ParserContext<'_> {
/// Returns a [`ReturnStatement`] AST node if the next tokens represent a return statement.
fn parse_return_statement(&mut self) -> Result<ReturnStatement> {
let start = self.expect(&Token::Return)?;
let expression = self.parse_expression()?;
self.expect(&Token::Semicolon)?;
let expression = match self.token.token {
Token::Semicolon => {
let span = self.expect(&Token::Semicolon)?;
Expression::Unit(UnitExpression { span })
}
_ => self.parse_expression()?,
};
let span = start + expression.span();
Ok(ReturnStatement { span, expression })
}