Resolver - Check return statements in the resolver

This commit is contained in:
Emmanuel BENOîT 2023-01-11 15:30:24 +01:00
parent 406400bf71
commit 57a5a62300

View file

@ -376,14 +376,20 @@ impl VarResolver for StmtNode {
}) })
.and_then(|_| body.resolve(rs)), .and_then(|_| body.resolve(rs)),
StmtNode::Return { token, value: None } => match rs.current_type() {
ScopeType::TopLevel => rs.error(token, "'return' not allowed here"),
_ => Ok(()),
},
StmtNode::Return { StmtNode::Return {
token: _, token,
value: None,
} => Ok(()),
StmtNode::Return {
token: _,
value: Some(expr), value: Some(expr),
} => expr.resolve(rs), } => {
if !matches!(rs.current_type(), ScopeType::Method | ScopeType::Function) {
rs.error(token, "'return' with value is not allowed here")
} else {
expr.resolve(rs)
}
}
StmtNode::Expression(expr) => expr.resolve(rs), StmtNode::Expression(expr) => expr.resolve(rs),
StmtNode::Print(expr) => expr.resolve(rs), StmtNode::Print(expr) => expr.resolve(rs),