Validate let expressions

Emit an error if they're found in an invalid position.
This commit is contained in:
Chayim Refael Friedman 2022-01-30 12:23:36 +02:00
parent a1b7169b48
commit 821b791b6d
3 changed files with 261 additions and 0 deletions

View file

@ -38,6 +38,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
ast::PtrType(it) => validate_trait_object_ptr_ty(it, &mut errors),
ast::FnPtrType(it) => validate_trait_object_fn_ptr_ret_ty(it, &mut errors),
ast::MacroRules(it) => validate_macro_rules(it, &mut errors),
ast::LetExpr(it) => validate_let_expr(it, &mut errors),
_ => (),
}
}
@ -343,3 +344,33 @@ fn validate_const(const_: ast::Const, errors: &mut Vec<SyntaxError>) {
errors.push(SyntaxError::new("const globals cannot be mutable", mut_token.text_range()));
}
}
fn validate_let_expr(let_: ast::LetExpr, errors: &mut Vec<SyntaxError>) {
let mut token = let_.syntax().clone();
loop {
token = match token.parent() {
Some(it) => it,
None => break,
};
if ast::ParenExpr::can_cast(token.kind()) {
continue;
} else if let Some(it) = ast::BinExpr::cast(token.clone()) {
if it.op_kind() == Some(ast::BinaryOp::LogicOp(ast::LogicOp::And)) {
continue;
}
} else if ast::IfExpr::can_cast(token.kind())
|| ast::WhileExpr::can_cast(token.kind())
|| ast::MatchGuard::can_cast(token.kind())
{
// It must be part of the condition since the expressions are inside a block.
return;
}
break;
}
errors.push(SyntaxError::new(
"`let` expressions are not supported here",
let_.syntax().text_range(),
));
}