mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 15:25:06 +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:
@ -1,4 +1,7 @@
|
||||
use nu_protocol::{ast, CustomValue, ShellError, Span, Value};
|
||||
use nu_protocol::{
|
||||
ast::{self, Math, Operator},
|
||||
CustomValue, ShellError, Span, Type, Value,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
@ -112,7 +115,7 @@ impl CustomValue for CoolCustomValue {
|
||||
) -> Result<Value, ShellError> {
|
||||
match operator {
|
||||
// Append the string inside `cool`
|
||||
ast::Operator::Math(ast::Math::Concat) => {
|
||||
Operator::Math(Math::Concatenate) => {
|
||||
if let Some(right) = right
|
||||
.as_custom_value()
|
||||
.ok()
|
||||
@ -125,18 +128,21 @@ impl CustomValue for CoolCustomValue {
|
||||
op_span,
|
||||
))
|
||||
} else {
|
||||
Err(ShellError::OperatorMismatch {
|
||||
Err(ShellError::OperatorUnsupportedType {
|
||||
op: Operator::Math(Math::Concatenate),
|
||||
unsupported: right.get_type(),
|
||||
op_span,
|
||||
lhs_ty: self.typetag_name().into(),
|
||||
lhs_span,
|
||||
rhs_ty: right.get_type().to_string(),
|
||||
rhs_span: right.span(),
|
||||
unsupported_span: right.span(),
|
||||
help: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedOperator {
|
||||
operator,
|
||||
span: op_span,
|
||||
_ => Err(ShellError::OperatorUnsupportedType {
|
||||
op: Operator::Math(Math::Concatenate),
|
||||
unsupported: Type::Custom(self.type_name().into()),
|
||||
op_span,
|
||||
unsupported_span: lhs_span,
|
||||
help: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user