mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
raise ParseError if assign to a non-variable or non-mutable-variable (#14405)
# Description While reviewing #14388, I think we can make some improvement on parser. For the following code: ```nushell let a = 3 a = 10 # should be error $a = 10 # another error ``` I think they can raise `ParseError`, so nushell doesn't need to move forward compiling IR block. # User-Facing Changes ```nushell let a = 3 a = 10 ``` Will raise parse error instead of compile error. # Tests + Formatting Added 1 test.
This commit is contained in:
@ -4988,6 +4988,20 @@ pub fn parse_assignment_expression(
|
||||
|
||||
// Parse the lhs and operator as usual for a math expression
|
||||
let mut lhs = parse_expression(working_set, lhs_spans);
|
||||
// make sure that lhs is a mutable variable.
|
||||
match &lhs.expr {
|
||||
Expr::FullCellPath(p) => {
|
||||
if let Expr::Var(var_id) = p.head.expr {
|
||||
if var_id != nu_protocol::ENV_VARIABLE_ID
|
||||
&& !working_set.get_variable(var_id).mutable
|
||||
{
|
||||
working_set.error(ParseError::AssignmentRequiresMutableVar(lhs.span))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => working_set.error(ParseError::AssignmentRequiresVar(lhs.span)),
|
||||
}
|
||||
|
||||
let mut operator = parse_assignment_operator(working_set, op_span);
|
||||
|
||||
// Re-parse the right-hand side as a subexpression
|
||||
|
Reference in New Issue
Block a user