Compare commits

...

2 Commits

Author SHA1 Message Date
lyken 2acf9b88e9 core: fix crash on iterating over non-iterables 2024-06-27 16:59:35 +08:00
lyken d06c13f936 core: fix crash on invalid subscripting 2024-06-27 16:58:48 +08:00
1 changed files with 27 additions and 3 deletions

View File

@ -248,7 +248,17 @@ impl<'a> Fold<()> for Inferencer<'a> {
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
todo!()
}
_ => unreachable!(),
_ => {
// User is attempting to use a for loop to iterate
// over a value of an unsupported type.
let iter_ty = iter.custom.unwrap();
let iter_ty_str = self.unifier.stringify(iter_ty);
return report_error(
format!("'{iter_ty_str}' object is not iterable").as_str(),
iter.location,
)?;
}
};
self.unify(list_like_ty, iter.custom.unwrap(), &iter.location)?;
}
@ -1856,6 +1866,18 @@ impl<'a> Inferencer<'a> {
slice: &ast::Expr<Option<Type>>,
ctx: ExprContext,
) -> InferenceResult {
let report_unscriptable_error = |unifier: &mut Unifier| {
// User is attempting to index into a value of an unsupported type.
let value_ty = value.custom.unwrap();
let value_ty_str = unifier.stringify(value_ty);
return report_error(
format!("'{value_ty_str}' object is not subscriptable").as_str(),
slice.location, // using the slice's location (rather than value's) because it is more clear
);
};
let ty = self.unifier.get_dummy_var().ty;
match &slice.node {
ExprKind::Slice { lower, upper, step } => {
@ -1871,7 +1893,9 @@ impl<'a> Inferencer<'a> {
make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims))
}
_ => unreachable!(),
_ => {
return report_unscriptable_error(self.unifier);
}
};
self.constrain(value.custom.unwrap(), list_like_ty, &value.location)?;
Ok(list_like_ty)
@ -1961,7 +1985,7 @@ impl<'a> Inferencer<'a> {
self.constrain(slice.custom.unwrap(), valid_index_ty, &slice.location)?;
self.infer_subscript_ndarray(value, slice, ty, ndims)
}
_ => unreachable!(),
_ => report_unscriptable_error(self.unifier),
}
}
}