nushell/crates/nu-command/src/math
Ian Manske 62e56d3581
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`.
```
2025-02-12 20:03:40 -08:00
..
abs.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
avg.rs Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
ceil.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
floor.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
log.rs remove duplicate code in math/log.rs (#15022) 2025-02-06 07:00:25 -06:00
math_.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
max.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
median.rs Rework operator type errors (#14429) 2025-02-12 20:03:40 -08:00
min.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
mod.rs cratification: part III of the math commands to nu-cmd-extra (#9674) 2023-07-13 09:11:26 -07:00
mode.rs Rework operator type errors (#14429) 2025-02-12 20:03:40 -08:00
product.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
reducers.rs Rework operator type errors (#14429) 2025-02-12 20:03:40 -08:00
round.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
sqrt.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
stddev.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
sum.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
utils.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
variance.rs better error message for "sum", "product", and "sum_of_squares" (#14711) 2024-12-31 16:04:23 -06:00