forked from extern/nushell
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:
@ -30,11 +30,11 @@ impl Command for HelpOperators {
|
||||
let head = call.head;
|
||||
let mut operators = [
|
||||
Operator::Assignment(Assignment::Assign),
|
||||
Operator::Assignment(Assignment::PlusAssign),
|
||||
Operator::Assignment(Assignment::ConcatAssign),
|
||||
Operator::Assignment(Assignment::MinusAssign),
|
||||
Operator::Assignment(Assignment::AddAssign),
|
||||
Operator::Assignment(Assignment::SubtractAssign),
|
||||
Operator::Assignment(Assignment::MultiplyAssign),
|
||||
Operator::Assignment(Assignment::DivideAssign),
|
||||
Operator::Assignment(Assignment::ConcatenateAssign),
|
||||
Operator::Comparison(Comparison::Equal),
|
||||
Operator::Comparison(Comparison::NotEqual),
|
||||
Operator::Comparison(Comparison::LessThan),
|
||||
@ -49,22 +49,22 @@ impl Command for HelpOperators {
|
||||
Operator::Comparison(Comparison::NotHas),
|
||||
Operator::Comparison(Comparison::StartsWith),
|
||||
Operator::Comparison(Comparison::EndsWith),
|
||||
Operator::Math(Math::Plus),
|
||||
Operator::Math(Math::Concat),
|
||||
Operator::Math(Math::Minus),
|
||||
Operator::Math(Math::Add),
|
||||
Operator::Math(Math::Subtract),
|
||||
Operator::Math(Math::Multiply),
|
||||
Operator::Math(Math::Divide),
|
||||
Operator::Math(Math::FloorDivide),
|
||||
Operator::Math(Math::Modulo),
|
||||
Operator::Math(Math::FloorDivision),
|
||||
Operator::Math(Math::Pow),
|
||||
Operator::Math(Math::Concatenate),
|
||||
Operator::Bits(Bits::BitOr),
|
||||
Operator::Bits(Bits::BitXor),
|
||||
Operator::Bits(Bits::BitAnd),
|
||||
Operator::Bits(Bits::ShiftLeft),
|
||||
Operator::Bits(Bits::ShiftRight),
|
||||
Operator::Boolean(Boolean::And),
|
||||
Operator::Boolean(Boolean::Or),
|
||||
Operator::Boolean(Boolean::Xor),
|
||||
Operator::Boolean(Boolean::And),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|op| {
|
||||
@ -87,7 +87,7 @@ impl Command for HelpOperators {
|
||||
"operator" => Value::string("not", head),
|
||||
"name" => Value::string("Not", head),
|
||||
"description" => Value::string("Negates a value or expression.", head),
|
||||
"precedence" => Value::int(0, head),
|
||||
"precedence" => Value::int(55, head),
|
||||
},
|
||||
head,
|
||||
));
|
||||
@ -151,32 +151,32 @@ fn description(operator: &Operator) -> &'static str {
|
||||
}
|
||||
Operator::Comparison(Comparison::StartsWith) => "Checks if a string starts with another.",
|
||||
Operator::Comparison(Comparison::EndsWith) => "Checks if a string ends with another.",
|
||||
Operator::Math(Math::Plus) => "Adds two values.",
|
||||
Operator::Math(Math::Concat) => {
|
||||
"Concatenates two lists, two strings, or two binary values."
|
||||
}
|
||||
Operator::Math(Math::Minus) => "Subtracts two values.",
|
||||
Operator::Math(Math::Add) => "Adds two values.",
|
||||
Operator::Math(Math::Subtract) => "Subtracts two values.",
|
||||
Operator::Math(Math::Multiply) => "Multiplies two values.",
|
||||
Operator::Math(Math::Divide) => "Divides two values.",
|
||||
Operator::Math(Math::FloorDivide) => "Divides two values and floors the result.",
|
||||
Operator::Math(Math::Modulo) => "Divides two values and returns the remainder.",
|
||||
Operator::Math(Math::FloorDivision) => "Divides two values and floors the result.",
|
||||
Operator::Math(Math::Pow) => "Raises one value to the power of another.",
|
||||
Operator::Boolean(Boolean::And) => "Checks if both values are true.",
|
||||
Operator::Math(Math::Concatenate) => {
|
||||
"Concatenates two lists, two strings, or two binary values."
|
||||
}
|
||||
Operator::Boolean(Boolean::Or) => "Checks if either value is true.",
|
||||
Operator::Boolean(Boolean::Xor) => "Checks if one value is true and the other is false.",
|
||||
Operator::Boolean(Boolean::And) => "Checks if both values are true.",
|
||||
Operator::Bits(Bits::BitOr) => "Performs a bitwise OR on two values.",
|
||||
Operator::Bits(Bits::BitXor) => "Performs a bitwise XOR on two values.",
|
||||
Operator::Bits(Bits::BitAnd) => "Performs a bitwise AND on two values.",
|
||||
Operator::Bits(Bits::ShiftLeft) => "Bitwise shifts a value left by another.",
|
||||
Operator::Bits(Bits::ShiftRight) => "Bitwise shifts a value right by another.",
|
||||
Operator::Assignment(Assignment::Assign) => "Assigns a value to a variable.",
|
||||
Operator::Assignment(Assignment::PlusAssign) => "Adds a value to a variable.",
|
||||
Operator::Assignment(Assignment::ConcatAssign) => {
|
||||
"Concatenates two lists, two strings, or two binary values."
|
||||
}
|
||||
Operator::Assignment(Assignment::MinusAssign) => "Subtracts a value from a variable.",
|
||||
Operator::Assignment(Assignment::AddAssign) => "Adds a value to a variable.",
|
||||
Operator::Assignment(Assignment::SubtractAssign) => "Subtracts a value from a variable.",
|
||||
Operator::Assignment(Assignment::MultiplyAssign) => "Multiplies a variable by a value.",
|
||||
Operator::Assignment(Assignment::DivideAssign) => "Divides a variable by a value.",
|
||||
Operator::Assignment(Assignment::ConcatenateAssign) => {
|
||||
"Concatenates a list, a string, or a binary value to a variable of the same type."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,45 +91,26 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
|
||||
Pick::Median
|
||||
};
|
||||
|
||||
let mut sorted = vec![];
|
||||
|
||||
for item in values {
|
||||
sorted.push(item.clone());
|
||||
}
|
||||
|
||||
if let Some(Err(values)) = values
|
||||
.windows(2)
|
||||
.map(|elem| {
|
||||
if elem[0].partial_cmp(&elem[1]).is_none() {
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: elem[0].get_type().to_string(),
|
||||
lhs_span: elem[0].span(),
|
||||
rhs_ty: elem[1].get_type().to_string(),
|
||||
rhs_span: elem[1].span(),
|
||||
});
|
||||
}
|
||||
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
|
||||
})
|
||||
.find(|elem| elem.is_err())
|
||||
{
|
||||
return Err(values);
|
||||
}
|
||||
let mut sorted = values
|
||||
.iter()
|
||||
.filter(|x| !x.as_float().is_ok_and(f64::is_nan))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
|
||||
|
||||
match take {
|
||||
Pick::Median => {
|
||||
let idx = (values.len() as f64 / 2.0).floor() as usize;
|
||||
let out = sorted
|
||||
Ok(sorted
|
||||
.get(idx)
|
||||
.ok_or_else(|| ShellError::UnsupportedInput {
|
||||
msg: "Empty input".to_string(),
|
||||
input: "value originates from here".into(),
|
||||
msg_span: head,
|
||||
input_span: span,
|
||||
})?;
|
||||
Ok(out.clone())
|
||||
})?
|
||||
.to_owned()
|
||||
.to_owned())
|
||||
}
|
||||
Pick::MedianAverage => {
|
||||
let idx_end = values.len() / 2;
|
||||
@ -143,7 +124,8 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
|
||||
msg_span: head,
|
||||
input_span: span,
|
||||
})?
|
||||
.clone();
|
||||
.to_owned()
|
||||
.to_owned();
|
||||
|
||||
let right = sorted
|
||||
.get(idx_end)
|
||||
@ -153,7 +135,8 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
|
||||
msg_span: head,
|
||||
input_span: span,
|
||||
})?
|
||||
.clone();
|
||||
.to_owned()
|
||||
.to_owned();
|
||||
|
||||
average(&[left, right], span, head)
|
||||
}
|
||||
|
@ -111,29 +111,12 @@ impl Command for SubCommand {
|
||||
}
|
||||
|
||||
pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellError> {
|
||||
if let Some(Err(values)) = values
|
||||
.windows(2)
|
||||
.map(|elem| {
|
||||
if elem[0].partial_cmp(&elem[1]).is_none() {
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: elem[0].get_type().to_string(),
|
||||
lhs_span: elem[0].span(),
|
||||
rhs_ty: elem[1].get_type().to_string(),
|
||||
rhs_span: elem[1].span(),
|
||||
});
|
||||
}
|
||||
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
|
||||
})
|
||||
.find(|elem| elem.is_err())
|
||||
{
|
||||
return Err(values);
|
||||
}
|
||||
//In e-q, Value doesn't implement Hash or Eq, so we have to get the values inside
|
||||
// But f64 doesn't implement Hash, so we get the binary representation to use as
|
||||
// key in the HashMap
|
||||
let hashable_values = values
|
||||
.iter()
|
||||
.filter(|x| !x.as_float().is_ok_and(f64::is_nan))
|
||||
.map(|val| match val {
|
||||
Value::Int { val, .. } => Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Int)),
|
||||
Value::Duration { val, .. } => {
|
||||
|
@ -32,18 +32,8 @@ pub fn max(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
if let Some(result) = value.partial_cmp(&biggest) {
|
||||
if result == Ordering::Greater {
|
||||
biggest = value.clone();
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: biggest.get_type().to_string(),
|
||||
lhs_span: biggest.span(),
|
||||
rhs_ty: value.get_type().to_string(),
|
||||
rhs_span: value.span(),
|
||||
});
|
||||
if value.partial_cmp(&biggest) == Some(Ordering::Greater) {
|
||||
biggest = value.clone();
|
||||
}
|
||||
}
|
||||
Ok(biggest)
|
||||
@ -61,18 +51,8 @@ pub fn min(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
if let Some(result) = value.partial_cmp(&smallest) {
|
||||
if result == Ordering::Less {
|
||||
smallest = value.clone();
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: smallest.get_type().to_string(),
|
||||
lhs_span: smallest.span(),
|
||||
rhs_ty: value.get_type().to_string(),
|
||||
rhs_span: value.span(),
|
||||
});
|
||||
if value.partial_cmp(&smallest) == Some(Ordering::Less) {
|
||||
smallest = value.clone();
|
||||
}
|
||||
}
|
||||
Ok(smallest)
|
||||
|
Reference in New Issue
Block a user