Resolver - Improved handling of 'return'

This commit is contained in:
Emmanuel BENOîT 2023-01-11 15:33:21 +01:00
parent 57a5a62300
commit ee53c2fba4

View file

@ -383,13 +383,13 @@ impl VarResolver for StmtNode {
StmtNode::Return { StmtNode::Return {
token, token,
value: Some(expr), value: Some(expr),
} => { } => match rs.current_type() {
if !matches!(rs.current_type(), ScopeType::Method | ScopeType::Function) { ScopeType::TopLevel => rs.error(token, "'return' not allowed here"),
ScopeType::Initializer => {
rs.error(token, "'return' with value is not allowed here") rs.error(token, "'return' with value is not allowed here")
} else {
expr.resolve(rs)
} }
} _ => 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),