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

@ -1,11 +1,10 @@
use crate::{ast::RedirectionSource, did_you_mean, Span, Type};
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use std::{
fmt::Display,
str::{from_utf8, Utf8Error},
};
use crate::{ast::RedirectionSource, did_you_mean, Span, Type};
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Debug, Error, Diagnostic, Serialize, Deserialize)]
@ -115,38 +114,36 @@ pub enum ParseError {
span: Span,
},
#[error("{0} is not supported on values of type {3}")]
#[diagnostic(code(nu::parser::unsupported_operation))]
UnsupportedOperationLHS(
String,
#[label = "doesn't support this value"] Span,
#[label("{3}")] Span,
Type,
),
/// One or more of the values have types not supported by the operator.
#[error("The '{op}' operator does not work on values of type '{unsupported}'.")]
#[diagnostic(code(nu::parser::operator_unsupported_type))]
OperatorUnsupportedType {
op: &'static str,
unsupported: Type,
#[label = "does not support '{unsupported}'"]
op_span: Span,
#[label("{unsupported}")]
unsupported_span: Span,
#[help]
help: Option<&'static str>,
},
#[error("{0} is not supported between {3} and {5}.")]
#[diagnostic(code(nu::parser::unsupported_operation))]
UnsupportedOperationRHS(
String,
#[label = "doesn't support these values"] Span,
#[label("{3}")] Span,
Type,
#[label("{5}")] Span,
Type,
),
#[error("{0} is not supported between {3}, {5}, and {7}.")]
#[diagnostic(code(nu::parser::unsupported_operation))]
UnsupportedOperationTernary(
String,
#[label = "doesn't support these values"] Span,
#[label("{3}")] Span,
Type,
#[label("{5}")] Span,
Type,
#[label("{7}")] Span,
Type,
),
/// The operator supports the types of both values, but not the specific combination of their types.
#[error("Types '{lhs}' and '{rhs}' are not compatible for the '{op}' operator.")]
#[diagnostic(code(nu::parser::operator_incompatible_types))]
OperatorIncompatibleTypes {
op: &'static str,
lhs: Type,
rhs: Type,
#[label = "does not operate between '{lhs}' and '{rhs}'"]
op_span: Span,
#[label("{lhs}")]
lhs_span: Span,
#[label("{rhs}")]
rhs_span: Span,
#[help]
help: Option<&'static str>,
},
#[error("Capture of mutable variable.")]
#[diagnostic(code(nu::parser::expected_keyword))]
@ -564,9 +561,8 @@ impl ParseError {
ParseError::ExpectedWithStringMsg(_, s) => *s,
ParseError::ExpectedWithDidYouMean(_, _, s) => *s,
ParseError::Mismatch(_, _, s) => *s,
ParseError::UnsupportedOperationLHS(_, _, s, _) => *s,
ParseError::UnsupportedOperationRHS(_, _, _, _, s, _) => *s,
ParseError::UnsupportedOperationTernary(_, _, _, _, _, _, s, _) => *s,
ParseError::OperatorUnsupportedType { op_span, .. } => *op_span,
ParseError::OperatorIncompatibleTypes { op_span, .. } => *op_span,
ParseError::ExpectedKeyword(_, s) => *s,
ParseError::UnexpectedKeyword(_, s) => *s,
ParseError::CantAliasKeyword(_, s) => *s,

View File

@ -17,22 +17,35 @@ pub mod location;
/// and pass it into an error viewer to display to the user.
#[derive(Debug, Clone, Error, Diagnostic, PartialEq)]
pub enum ShellError {
/// An operator received two arguments of incompatible types.
///
/// ## Resolution
///
/// Check each argument's type and convert one or both as needed.
#[error("Type mismatch during operation.")]
#[diagnostic(code(nu::shell::type_mismatch))]
OperatorMismatch {
#[label = "type mismatch for operator"]
/// One or more of the values have types not supported by the operator.
#[error("The '{op}' operator does not work on values of type '{unsupported}'.")]
#[diagnostic(code(nu::shell::operator_unsupported_type))]
OperatorUnsupportedType {
op: Operator,
unsupported: Type,
#[label = "does not support '{unsupported}'"]
op_span: Span,
lhs_ty: String,
#[label("{lhs_ty}")]
#[label("{unsupported}")]
unsupported_span: Span,
#[help]
help: Option<&'static str>,
},
/// The operator supports the types of both values, but not the specific combination of their types.
#[error("Types '{lhs}' and '{rhs}' are not compatible for the '{op}' operator.")]
#[diagnostic(code(nu::shell::operator_incompatible_types))]
OperatorIncompatibleTypes {
op: Operator,
lhs: Type,
rhs: Type,
#[label = "does not operate between '{lhs}' and '{rhs}'"]
op_span: Span,
#[label("{lhs}")]
lhs_span: Span,
rhs_ty: String,
#[label("{rhs_ty}")]
#[label("{rhs}")]
rhs_span: Span,
#[help]
help: Option<&'static str>,
},
/// An arithmetic operation's resulting value overflowed its possible size.
@ -156,20 +169,6 @@ pub enum ShellError {
call_span: Span,
},
/// This value cannot be used with this operator.
///
/// ## Resolution
///
/// Not all values, for example custom values, can be used with all operators. Either
/// implement support for the operator on this type, or convert the type to a supported one.
#[error("Unsupported operator: {operator}.")]
#[diagnostic(code(nu::shell::unsupported_operator))]
UnsupportedOperator {
operator: Operator,
#[label = "unsupported operator"]
span: Span,
},
/// Invalid assignment left-hand side
///
/// ## Resolution