core/magic_methods: Allow unknown return types

These types can be later inferred by the type inferencer.
This commit is contained in:
David Mak 2024-03-25 16:44:06 +08:00
parent 0636398f3c
commit 2f73c96e98
1 changed files with 18 additions and 16 deletions

View File

@ -90,7 +90,7 @@ pub fn impl_binop(
_store: &PrimitiveStore, _store: &PrimitiveStore,
ty: Type, ty: Type,
other_ty: &[Type], other_ty: &[Type],
ret_ty: Type, ret_ty: Option<Type>,
ops: &[Operator], ops: &[Operator],
) { ) {
with_fields(unifier, ty, |unifier, fields| { with_fields(unifier, ty, |unifier, fields| {
@ -107,6 +107,8 @@ pub fn impl_binop(
VarMap::new() VarMap::new()
}; };
let ret_ty = ret_ty.unwrap_or_else(|| unifier.get_fresh_var(None, None).0);
for op in ops { for op in ops {
fields.insert(binop_name(op).into(), { fields.insert(binop_name(op).into(), {
( (
@ -193,7 +195,7 @@ pub fn impl_basic_arithmetic(
store: &PrimitiveStore, store: &PrimitiveStore,
ty: Type, ty: Type,
other_ty: &[Type], other_ty: &[Type],
ret_ty: Type, ret_ty: Option<Type>,
) { ) {
impl_binop( impl_binop(
unifier, unifier,
@ -211,7 +213,7 @@ pub fn impl_pow(
store: &PrimitiveStore, store: &PrimitiveStore,
ty: Type, ty: Type,
other_ty: &[Type], other_ty: &[Type],
ret_ty: Type, ret_ty: Option<Type>,
) { ) {
impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::Pow]); impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::Pow]);
} }
@ -223,19 +225,19 @@ pub fn impl_bitwise_arithmetic(unifier: &mut Unifier, store: &PrimitiveStore, ty
store, store,
ty, ty,
&[ty], &[ty],
ty, Some(ty),
&[Operator::BitAnd, Operator::BitOr, Operator::BitXor], &[Operator::BitAnd, Operator::BitOr, Operator::BitXor],
); );
} }
/// `LShift`, `RShift` /// `LShift`, `RShift`
pub fn impl_bitwise_shift(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type) { pub fn impl_bitwise_shift(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type) {
impl_binop(unifier, store, ty, &[store.int32, store.uint32], ty, &[Operator::LShift, Operator::RShift]); impl_binop(unifier, store, ty, &[store.int32, store.uint32], Some(ty), &[Operator::LShift, Operator::RShift]);
} }
/// `Div` /// `Div`
pub fn impl_div(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type, other_ty: &[Type]) { pub fn impl_div(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type, other_ty: &[Type]) {
impl_binop(unifier, store, ty, other_ty, store.float, &[Operator::Div]); impl_binop(unifier, store, ty, other_ty, Some(store.float), &[Operator::Div]);
} }
/// `FloorDiv` /// `FloorDiv`
@ -244,7 +246,7 @@ pub fn impl_floordiv(
store: &PrimitiveStore, store: &PrimitiveStore,
ty: Type, ty: Type,
other_ty: &[Type], other_ty: &[Type],
ret_ty: Type, ret_ty: Option<Type>,
) { ) {
impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::FloorDiv]); impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::FloorDiv]);
} }
@ -255,7 +257,7 @@ pub fn impl_mod(
store: &PrimitiveStore, store: &PrimitiveStore,
ty: Type, ty: Type,
other_ty: &[Type], other_ty: &[Type],
ret_ty: Type, ret_ty: Option<Type>,
) { ) {
impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::Mod]); impl_binop(unifier, store, ty, other_ty, ret_ty, &[Operator::Mod]);
} }
@ -304,13 +306,13 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
/* int ======== */ /* int ======== */
for t in [int32_t, int64_t, uint32_t, uint64_t] { for t in [int32_t, int64_t, uint32_t, uint64_t] {
impl_basic_arithmetic(unifier, store, t, &[t], t); impl_basic_arithmetic(unifier, store, t, &[t], Some(t));
impl_pow(unifier, store, t, &[t], t); impl_pow(unifier, store, t, &[t], Some(t));
impl_bitwise_arithmetic(unifier, store, t); impl_bitwise_arithmetic(unifier, store, t);
impl_bitwise_shift(unifier, store, t); impl_bitwise_shift(unifier, store, t);
impl_div(unifier, store, t, &[t]); impl_div(unifier, store, t, &[t]);
impl_floordiv(unifier, store, t, &[t], t); impl_floordiv(unifier, store, t, &[t], Some(t));
impl_mod(unifier, store, t, &[t], t); impl_mod(unifier, store, t, &[t], Some(t));
impl_invert(unifier, store, t); impl_invert(unifier, store, t);
impl_not(unifier, store, t); impl_not(unifier, store, t);
impl_comparison(unifier, store, t, t); impl_comparison(unifier, store, t, t);
@ -321,11 +323,11 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
} }
/* float ======== */ /* float ======== */
impl_basic_arithmetic(unifier, store, float_t, &[float_t], float_t); impl_basic_arithmetic(unifier, store, float_t, &[float_t], Some(float_t));
impl_pow(unifier, store, float_t, &[int32_t, float_t], float_t); impl_pow(unifier, store, float_t, &[int32_t, float_t], Some(float_t));
impl_div(unifier, store, float_t, &[float_t]); impl_div(unifier, store, float_t, &[float_t]);
impl_floordiv(unifier, store, float_t, &[float_t], float_t); impl_floordiv(unifier, store, float_t, &[float_t], Some(float_t));
impl_mod(unifier, store, float_t, &[float_t], float_t); impl_mod(unifier, store, float_t, &[float_t], Some(float_t));
impl_sign(unifier, store, float_t); impl_sign(unifier, store, float_t);
impl_not(unifier, store, float_t); impl_not(unifier, store, float_t);
impl_comparison(unifier, store, float_t, float_t); impl_comparison(unifier, store, float_t, float_t);