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:
Ian Manske
2025-02-13 04:03:40 +00:00
committed by GitHub
parent 2e1b6acc0e
commit 62e56d3581
32 changed files with 1684 additions and 1772 deletions

View File

@ -32,45 +32,48 @@ def command-not-found-error [span: record] {
throw-error "std::help::command_not_found" "command not found" $span
}
def get-all-operators [] { return [
[type, operator, name, description, precedence];
[Assignment, =, Assign, "Assigns a value to a variable.", 10]
[Assignment, +=, PlusAssign, "Adds a value to a variable.", 10]
[Assignment, ++=, ConcatAssign, "Concatenate two lists, two strings, or two binary values.", 10]
[Assignment, -=, MinusAssign, "Subtracts a value from a variable.", 10]
[Assignment, *=, MultiplyAssign, "Multiplies a variable by a value.", 10]
[Assignment, /=, DivideAssign, "Divides a variable by a value.", 10]
[Comparison, ==, Equal, "Checks if two values are equal.", 80]
[Comparison, !=, NotEqual, "Checks if two values are not equal.", 80]
[Comparison, <, LessThan, "Checks if a value is less than another.", 80]
[Comparison, <=, LessThanOrEqual, "Checks if a value is less than or equal to another.", 80]
[Comparison, >, GreaterThan, "Checks if a value is greater than another.", 80]
[Comparison, >=, GreaterThanOrEqual, "Checks if a value is greater than or equal to another.", 80]
[Comparison, '=~ or like', RegexMatch, "Checks if a value matches a regular expression.", 80]
[Comparison, '!~ or not-like', NotRegexMatch, "Checks if a value does not match a regular expression.", 80]
[Comparison, in, In, "Checks if a value is in a list or string.", 80]
[Comparison, not-in, NotIn, "Checks if a value is not in a list or string.", 80]
[Comparison, starts-with, StartsWith, "Checks if a string starts with another.", 80]
[Comparison, ends-with, EndsWith, "Checks if a string ends with another.", 80]
[Comparison, not, UnaryNot, "Negates a value or expression.", 0]
[Math, +, Plus, "Adds two values.", 90]
[Math, ++, Concat, "Concatenate two lists, two strings, or two binary values.", 80]
[Math, -, Minus, "Subtracts two values.", 90]
[Math, *, Multiply, "Multiplies two values.", 95]
[Math, /, Divide, "Divides two values.", 95]
[Math, //, FloorDivision, "Divides two values and floors the result.", 95]
[Math, mod, Modulo, "Divides two values and returns the remainder.", 95]
[Math, **, "Pow ", "Raises one value to the power of another.", 100]
[Bitwise, bit-or, BitOr, "Performs a bitwise OR on two values.", 60]
[Bitwise, bit-xor, BitXor, "Performs a bitwise XOR on two values.", 70]
[Bitwise, bit-and, BitAnd, "Performs a bitwise AND on two values.", 75]
[Bitwise, bit-shl, ShiftLeft, "Shifts a value left by another.", 85]
[Bitwise, bit-shr, ShiftRight, "Shifts a value right by another.", 85]
[Boolean, and, And, "Checks if two values are true.", 50]
[Boolean, or, Or, "Checks if either value is true.", 40]
[Boolean, xor, Xor, "Checks if one value is true and the other is false.", 45]
]}
def get-all-operators [] {
[
[type, operator, name, description, precedence];
[Assignment, =, Assign, 'Assigns a value to a variable.', 10]
[Assignment, +=, AddAssign, 'Adds a value to a variable.', 10]
[Assignment, -=, SubtractAssign, 'Subtracts a value from a variable.', 10]
[Assignment, *=, MultiplyAssign, 'Multiplies a variable by a value.', 10]
[Assignment, /=, DivideAssign, 'Divides a variable by a value.', 10]
[Assignment, ++=, ConcatenateAssign, 'Concatenates a list, a string, or a binary value to a variable of the same type.', 10]
[Comparison, ==, Equal, 'Checks if two values are equal.', 80]
[Comparison, !=, NotEqual, 'Checks if two values are not equal.', 80]
[Comparison, <, LessThan, 'Checks if a value is less than another.', 80]
[Comparison, >, GreaterThan, 'Checks if a value is greater than another.', 80]
[Comparison, <=, LessThanOrEqual, 'Checks if a value is less than or equal to another.', 80]
[Comparison, >=, GreaterThanOrEqual, 'Checks if a value is greater than or equal to another.', 80]
[Comparison, '=~ or like', RegexMatch, 'Checks if a value matches a regular expression.', 80]
[Comparison, '!~ or not-like', NotRegexMatch, 'Checks if a value does not match a regular expression.', 80]
[Comparison, in, In, 'Checks if a value is in a list, is part of a string, or is a key in a record.', 80]
[Comparison, not-in, NotIn, 'Checks if a value is not in a list, is not part of a string, or is not a key in a record.', 80]
[Comparison, has, Has, 'Checks if a list contains a value, a string contains another, or if a record has a key.', 80]
[Comparison, not-has, NotHas, 'Checks if a list does not contains a value, a string does not contains another, or if a record does not have a key.', 80]
[Comparison, starts-with, StartsWith, 'Checks if a string starts with another.', 80]
[Comparison, ends-with, EndsWith, 'Checks if a string ends with another.', 80]
[Math, +, Add, 'Adds two values.', 90]
[Math, -, Subtract, 'Subtracts two values.', 90]
[Math, *, Multiply, 'Multiplies two values.', 95]
[Math, /, Divide, 'Divides two values.', 95]
[Math, //, FloorDivide, 'Divides two values and floors the result.', 95]
[Math, mod, Modulo, 'Divides two values and returns the remainder.', 95]
[Math, **, Pow, 'Raises one value to the power of another.', 100]
[Math, ++, Concatenate, 'Concatenates two lists, two strings, or two binary values.', 80]
[Bitwise, bit-or, BitOr, 'Performs a bitwise OR on two values.', 60]
[Bitwise, bit-xor, BitXor, 'Performs a bitwise XOR on two values.', 70]
[Bitwise, bit-and, BitAnd, 'Performs a bitwise AND on two values.', 75]
[Bitwise, bit-shl, ShiftLeft, 'Bitwise shifts a value left by another.', 85]
[Bitwise, bit-shr, ShiftRight, 'Bitwise shifts a value right by another.', 85]
[Boolean, or, Or, 'Checks if either value is true.', 40]
[Boolean, xor, Xor, 'Checks if one value is true and the other is false.', 45]
[Boolean, and, And, 'Checks if both values are true.', 50]
[Boolean, not, Not, 'Negates a value or expression.', 55]
]
}
def "nu-complete list-aliases" [] {
scope aliases | select name description | rename value description