mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 05:24:56 +02:00
Improve comparison errors (#4541)
This commit is contained in:
@ -1537,6 +1537,13 @@ impl Value {
|
||||
return lhs.operation(*span, Operator::LessThan, op, rhs);
|
||||
}
|
||||
|
||||
if !type_compatible(self.get_type(), rhs.get_type())
|
||||
&& (self.get_type() != Type::Unknown)
|
||||
&& (rhs.get_type() != Type::Unknown)
|
||||
{
|
||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
||||
}
|
||||
|
||||
match self.partial_cmp(rhs) {
|
||||
Some(ordering) => Ok(Value::Bool {
|
||||
val: matches!(ordering, Ordering::Less),
|
||||
@ -1558,6 +1565,13 @@ impl Value {
|
||||
return lhs.operation(*span, Operator::LessThanOrEqual, op, rhs);
|
||||
}
|
||||
|
||||
if !type_compatible(self.get_type(), rhs.get_type())
|
||||
&& (self.get_type() != Type::Unknown)
|
||||
&& (rhs.get_type() != Type::Unknown)
|
||||
{
|
||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
||||
}
|
||||
|
||||
match self.partial_cmp(rhs) {
|
||||
Some(ordering) => Ok(Value::Bool {
|
||||
val: matches!(ordering, Ordering::Less | Ordering::Equal),
|
||||
@ -1579,6 +1593,13 @@ impl Value {
|
||||
return lhs.operation(*span, Operator::GreaterThan, op, rhs);
|
||||
}
|
||||
|
||||
if !type_compatible(self.get_type(), rhs.get_type())
|
||||
&& (self.get_type() != Type::Unknown)
|
||||
&& (rhs.get_type() != Type::Unknown)
|
||||
{
|
||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
||||
}
|
||||
|
||||
match self.partial_cmp(rhs) {
|
||||
Some(ordering) => Ok(Value::Bool {
|
||||
val: matches!(ordering, Ordering::Greater),
|
||||
@ -1600,6 +1621,13 @@ impl Value {
|
||||
return lhs.operation(*span, Operator::GreaterThanOrEqual, op, rhs);
|
||||
}
|
||||
|
||||
if !type_compatible(self.get_type(), rhs.get_type())
|
||||
&& (self.get_type() != Type::Unknown)
|
||||
&& (rhs.get_type() != Type::Unknown)
|
||||
{
|
||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
||||
}
|
||||
|
||||
match self.partial_cmp(rhs) {
|
||||
Some(ordering) => Ok(Value::Bool {
|
||||
val: matches!(ordering, Ordering::Greater | Ordering::Equal),
|
||||
@ -1985,6 +2013,14 @@ impl From<Spanned<HashMap<String, Value>>> for Value {
|
||||
}
|
||||
}
|
||||
|
||||
fn type_compatible(a: Type, b: Type) -> bool {
|
||||
if a == b {
|
||||
return true;
|
||||
}
|
||||
|
||||
matches!((a, b), (Type::Int, Type::Float) | (Type::Float, Type::Int))
|
||||
}
|
||||
|
||||
/// Create a Value::Record from a spanned indexmap
|
||||
impl From<Spanned<IndexMap<String, Value>>> for Value {
|
||||
fn from(input: Spanned<IndexMap<String, Value>>) -> Self {
|
||||
|
Reference in New Issue
Block a user