Improve comparison errors (#4541)

This commit is contained in:
JT
2022-02-18 17:11:27 -05:00
committed by GitHub
parent f085bd97f6
commit d53eaac7a1
3 changed files with 67 additions and 23 deletions

View File

@ -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 {