mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 07:16:05 +02:00
Rework operator type errors (#14429)
# Description This PR adds two new `ParseError` and `ShellError` cases for type errors relating to operators. - `OperatorUnsupportedType` is used when a type is not supported by an operator in any way, shape, or form. E.g., `+` does not support `bool`. - `OperatorIncompatibleTypes` is used when a operator is used with types it supports, but the combination of types provided cannot be used together. E.g., `filesize + duration` is not a valid combination. The other preexisting error cases related to operators have been removed and replaced with the new ones above. Namely: - `ShellError::OperatorMismatch` - `ShellError::UnsupportedOperator` - `ParseError::UnsupportedOperationLHS` - `ParseError::UnsupportedOperationRHS` - `ParseError::UnsupportedOperationTernary` # User-Facing Changes - `help operators` now lists the precedence of `not` as 55 instead of 0 (above the other boolean operators). Fixes #13675. - `math median` and `math mode` now ignore NaN values so that `[NaN NaN] | math median` and `[NaN NaN] | math mode` no longer trigger a type error. Instead, it's now an empty input error. Fixing this in earnest can be left for a future PR. - Comparisons with `nan` now return false instead of causing an error. E.g., `1 == nan` is now `false`. - All the operator type errors have been standardized and reworked. In particular, they can now have a help message, which is currently used for types errors relating to `++`. ```nu [1] ++ 2 ``` ``` Error: nu::parser::operator_unsupported_type × The '++' operator does not work on values of type 'int'. ╭─[entry #1:1:5] 1 │ [1] ++ 2 · ─┬ ┬ · │ ╰── int · ╰── does not support 'int' ╰──── help: if you meant to append a value to a list or a record to a table, use the `append` command or wrap the value in a list. For example: `$list ++ $value` should be `$list ++ [$value]` or `$list | append $value`. ```
This commit is contained in:
@ -164,13 +164,13 @@ pub(crate) fn compile_expression(
|
||||
Ok(())
|
||||
}
|
||||
Expr::BinaryOp(lhs, op, rhs) => {
|
||||
if let Expr::Operator(ref operator) = op.expr {
|
||||
if let Expr::Operator(operator) = op.expr {
|
||||
drop_input(builder)?;
|
||||
compile_binary_op(
|
||||
working_set,
|
||||
builder,
|
||||
lhs,
|
||||
operator.clone().into_spanned(op.span),
|
||||
operator.into_spanned(op.span),
|
||||
rhs,
|
||||
expr.span,
|
||||
out_reg,
|
||||
|
@ -150,11 +150,11 @@ pub(crate) fn compile_binary_op(
|
||||
pub(crate) fn decompose_assignment(assignment: Assignment) -> Option<Operator> {
|
||||
match assignment {
|
||||
Assignment::Assign => None,
|
||||
Assignment::PlusAssign => Some(Operator::Math(Math::Plus)),
|
||||
Assignment::ConcatAssign => Some(Operator::Math(Math::Concat)),
|
||||
Assignment::MinusAssign => Some(Operator::Math(Math::Minus)),
|
||||
Assignment::AddAssign => Some(Operator::Math(Math::Add)),
|
||||
Assignment::SubtractAssign => Some(Operator::Math(Math::Subtract)),
|
||||
Assignment::MultiplyAssign => Some(Operator::Math(Math::Multiply)),
|
||||
Assignment::DivideAssign => Some(Operator::Math(Math::Divide)),
|
||||
Assignment::ConcatenateAssign => Some(Operator::Math(Math::Concatenate)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -520,11 +520,11 @@ impl Eval for EvalRuntime {
|
||||
|
||||
let rhs = match assignment {
|
||||
Assignment::Assign => rhs,
|
||||
Assignment::PlusAssign => {
|
||||
Assignment::AddAssign => {
|
||||
let lhs = eval_expression::<D>(engine_state, stack, lhs)?;
|
||||
lhs.add(op_span, &rhs, op_span)?
|
||||
}
|
||||
Assignment::MinusAssign => {
|
||||
Assignment::SubtractAssign => {
|
||||
let lhs = eval_expression::<D>(engine_state, stack, lhs)?;
|
||||
lhs.sub(op_span, &rhs, op_span)?
|
||||
}
|
||||
@ -536,7 +536,7 @@ impl Eval for EvalRuntime {
|
||||
let lhs = eval_expression::<D>(engine_state, stack, lhs)?;
|
||||
lhs.div(op_span, &rhs, op_span)?
|
||||
}
|
||||
Assignment::ConcatAssign => {
|
||||
Assignment::ConcatenateAssign => {
|
||||
let lhs = eval_expression::<D>(engine_state, stack, lhs)?;
|
||||
lhs.concat(op_span, &rhs, op_span)?
|
||||
}
|
||||
|
@ -970,19 +970,19 @@ fn binary_op(
|
||||
Comparison::EndsWith => lhs_val.ends_with(op_span, &rhs_val, span)?,
|
||||
},
|
||||
Operator::Math(mat) => match mat {
|
||||
Math::Plus => lhs_val.add(op_span, &rhs_val, span)?,
|
||||
Math::Concat => lhs_val.concat(op_span, &rhs_val, span)?,
|
||||
Math::Minus => lhs_val.sub(op_span, &rhs_val, span)?,
|
||||
Math::Add => lhs_val.add(op_span, &rhs_val, span)?,
|
||||
Math::Subtract => lhs_val.sub(op_span, &rhs_val, span)?,
|
||||
Math::Multiply => lhs_val.mul(op_span, &rhs_val, span)?,
|
||||
Math::Divide => lhs_val.div(op_span, &rhs_val, span)?,
|
||||
Math::FloorDivide => lhs_val.floor_div(op_span, &rhs_val, span)?,
|
||||
Math::Modulo => lhs_val.modulo(op_span, &rhs_val, span)?,
|
||||
Math::FloorDivision => lhs_val.floor_div(op_span, &rhs_val, span)?,
|
||||
Math::Pow => lhs_val.pow(op_span, &rhs_val, span)?,
|
||||
Math::Concatenate => lhs_val.concat(op_span, &rhs_val, span)?,
|
||||
},
|
||||
Operator::Boolean(bl) => match bl {
|
||||
Boolean::And => lhs_val.and(op_span, &rhs_val, span)?,
|
||||
Boolean::Or => lhs_val.or(op_span, &rhs_val, span)?,
|
||||
Boolean::Xor => lhs_val.xor(op_span, &rhs_val, span)?,
|
||||
Boolean::And => lhs_val.and(op_span, &rhs_val, span)?,
|
||||
},
|
||||
Operator::Bits(bit) => match bit {
|
||||
Bits::BitOr => lhs_val.bit_or(op_span, &rhs_val, span)?,
|
||||
|
Reference in New Issue
Block a user