forked from M-Labs/nac3
1
0
Fork 0

core: fix crash on invalid subscripting

This commit is contained in:
lyken 2024-06-27 16:25:45 +08:00
parent 9808923258
commit d06c13f936
1 changed files with 16 additions and 2 deletions

View File

@ -1856,6 +1856,18 @@ impl<'a> Inferencer<'a> {
slice: &ast::Expr<Option<Type>>, slice: &ast::Expr<Option<Type>>,
ctx: ExprContext, ctx: ExprContext,
) -> InferenceResult { ) -> 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; let ty = self.unifier.get_dummy_var().ty;
match &slice.node { match &slice.node {
ExprKind::Slice { lower, upper, step } => { ExprKind::Slice { lower, upper, step } => {
@ -1871,7 +1883,9 @@ impl<'a> Inferencer<'a> {
make_ndarray_ty(self.unifier, self.primitives, Some(ty), Some(ndims)) 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)?; self.constrain(value.custom.unwrap(), list_like_ty, &value.location)?;
Ok(list_like_ty) Ok(list_like_ty)
@ -1961,7 +1975,7 @@ impl<'a> Inferencer<'a> {
self.constrain(slice.custom.unwrap(), valid_index_ty, &slice.location)?; self.constrain(slice.custom.unwrap(), valid_index_ty, &slice.location)?;
self.infer_subscript_ndarray(value, slice, ty, ndims) self.infer_subscript_ndarray(value, slice, ty, ndims)
} }
_ => unreachable!(), _ => report_unscriptable_error(self.unifier),
} }
} }
} }