Compare commits
3 Commits
a920fe0501
...
e614dd4257
Author | SHA1 | Date | |
---|---|---|---|
e614dd4257 | |||
937a8b9698 | |||
876ad6c59c |
@ -476,10 +476,24 @@ pub fn typeof_unaryop(
|
||||
return Err("The truth value of an array with more than one element is ambiguous".to_string())
|
||||
}
|
||||
|
||||
Ok(if operand.obj_id(unifier).is_some_and(|id| PRIMITIVE_DEF_IDS.iter().any(|prim_id| id == prim_id)) {
|
||||
Ok(match *op {
|
||||
Unaryop::Not => {
|
||||
match operand.obj_id(unifier) {
|
||||
Some(v) if v == PRIMITIVE_DEF_IDS.ndarray => Some(operand),
|
||||
Some(_) => Some(primitives.bool),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
Unaryop::Invert
|
||||
| Unaryop::UAdd
|
||||
| Unaryop::USub => {
|
||||
if operand.obj_id(unifier).is_some_and(|id| PRIMITIVE_DEF_IDS.iter().any(|prim_id| id == prim_id)) {
|
||||
Some(operand)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ use std::iter::once;
|
||||
use std::{cell::RefCell, sync::Arc};
|
||||
|
||||
use super::typedef::{Call, FunSignature, FuncArg, RecordField, Type, TypeEnum, Unifier, VarMap};
|
||||
use super::{magic_methods::*, typedef::CallId};
|
||||
use super::{magic_methods::*, type_error::TypeError, typedef::CallId};
|
||||
use crate::{
|
||||
symbol_resolver::{SymbolResolver, SymbolValue},
|
||||
toplevel::{
|
||||
@ -553,7 +553,7 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||
Some(self.infer_unary_ops(expr.location, op, operand)?)
|
||||
}
|
||||
ExprKind::Compare { left, ops, comparators } => {
|
||||
Some(self.infer_compare(left, ops, comparators)?)
|
||||
Some(self.infer_compare(expr.location, left, ops, comparators)?)
|
||||
}
|
||||
ExprKind::Subscript { value, slice, ctx, .. } => {
|
||||
Some(self.infer_subscript(value.as_ref(), slice.as_ref(), ctx)?)
|
||||
@ -628,7 +628,14 @@ impl<'a> Inferencer<'a> {
|
||||
loc: Some(location),
|
||||
};
|
||||
if let Some(ret) = ret {
|
||||
self.unifier.unify(sign.ret, ret).unwrap();
|
||||
self.unifier.unify(sign.ret, ret)
|
||||
.map_err(|err| {
|
||||
format!("Cannot unify {} <: {} - {:?}",
|
||||
self.unifier.stringify(sign.ret),
|
||||
self.unifier.stringify(ret),
|
||||
TypeError::new(err.kind, Some(location)))
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
let required: Vec<_> = sign
|
||||
.args
|
||||
@ -1262,11 +1269,12 @@ impl<'a> Inferencer<'a> {
|
||||
operand.custom.unwrap(),
|
||||
).map_err(|e| HashSet::from([format!("{e} (at {location})")]))?;
|
||||
|
||||
self.build_method_call(operand.location, method, operand.custom.unwrap(), vec![], ret)
|
||||
self.build_method_call(location, method, operand.custom.unwrap(), vec![], ret)
|
||||
}
|
||||
|
||||
fn infer_compare(
|
||||
&mut self,
|
||||
location: Location,
|
||||
left: &ast::Expr<Option<Type>>,
|
||||
ops: &[ast::Cmpop],
|
||||
comparators: &[ast::Expr<Option<Type>>],
|
||||
@ -1275,6 +1283,7 @@ impl<'a> Inferencer<'a> {
|
||||
return Err(HashSet::from([String::from("Comparator chaining with ndarray types not supported")]))
|
||||
}
|
||||
|
||||
let mut res = None;
|
||||
for (a, b, c) in izip!(once(left).chain(comparators), comparators, ops) {
|
||||
let method = comparison_name(c)
|
||||
.ok_or_else(|| HashSet::from([
|
||||
@ -1290,26 +1299,16 @@ impl<'a> Inferencer<'a> {
|
||||
b.custom.unwrap(),
|
||||
).map_err(|e| HashSet::from([format!("{e} (at {})", b.location)]))?;
|
||||
|
||||
self.build_method_call(
|
||||
a.location,
|
||||
res.replace(self.build_method_call(
|
||||
location,
|
||||
method,
|
||||
a.custom.unwrap(),
|
||||
vec![b.custom.unwrap()],
|
||||
ret,
|
||||
)?;
|
||||
)?);
|
||||
}
|
||||
|
||||
let res_lhs = comparators.iter().rev().nth(1).unwrap_or(left);
|
||||
let res_rhs = comparators.iter().rev().nth(0).unwrap();
|
||||
let res_op = ops.iter().rev().nth(0).unwrap();
|
||||
|
||||
Ok(typeof_cmpop(
|
||||
self.unifier,
|
||||
self.primitives,
|
||||
res_op,
|
||||
res_lhs.custom.unwrap(),
|
||||
res_rhs.custom.unwrap(),
|
||||
).unwrap().unwrap())
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Infers the type of a subscript expression on an `ndarray`.
|
||||
|
Loading…
Reference in New Issue
Block a user