2020-01-17 15:59:02 +00:00
|
|
|
//! Helper functions for binary operator type inference.
|
2021-03-01 11:35:11 +00:00
|
|
|
use chalk_ir::TyVariableKind;
|
2020-01-17 15:59:02 +00:00
|
|
|
use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2021-04-03 18:22:59 +00:00
|
|
|
use crate::{Interner, Scalar, Ty, TyBuilder, TyKind};
|
2019-02-23 14:24:07 +00:00
|
|
|
|
2020-01-17 15:59:02 +00:00
|
|
|
pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
|
2019-02-23 14:24:07 +00:00
|
|
|
match op {
|
2021-03-13 13:44:51 +00:00
|
|
|
BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
|
2021-04-03 18:22:59 +00:00
|
|
|
BinaryOp::Assignment { .. } => TyBuilder::unit(),
|
2021-03-13 13:44:51 +00:00
|
|
|
BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => {
|
2021-04-10 14:56:32 +00:00
|
|
|
// all integer combinations are valid here
|
|
|
|
if matches!(
|
|
|
|
lhs_ty.kind(&Interner),
|
2021-03-13 13:44:51 +00:00
|
|
|
TyKind::Scalar(Scalar::Int(_))
|
2021-04-10 14:56:32 +00:00
|
|
|
| TyKind::Scalar(Scalar::Uint(_))
|
|
|
|
| TyKind::InferenceVar(_, TyVariableKind::Integer)
|
|
|
|
) && matches!(
|
|
|
|
rhs_ty.kind(&Interner),
|
|
|
|
TyKind::Scalar(Scalar::Int(_))
|
|
|
|
| TyKind::Scalar(Scalar::Uint(_))
|
|
|
|
| TyKind::InferenceVar(_, TyVariableKind::Integer)
|
|
|
|
) {
|
|
|
|
lhs_ty
|
|
|
|
} else {
|
|
|
|
TyKind::Error.intern(&Interner)
|
2021-03-13 13:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 14:56:32 +00:00
|
|
|
BinaryOp::ArithOp(_) => match (lhs_ty.kind(&Interner), rhs_ty.kind(&Interner)) {
|
|
|
|
// (int, int) | (uint, uint) | (float, float)
|
|
|
|
(TyKind::Scalar(Scalar::Int(_)), TyKind::Scalar(Scalar::Int(_)))
|
|
|
|
| (TyKind::Scalar(Scalar::Uint(_)), TyKind::Scalar(Scalar::Uint(_)))
|
|
|
|
| (TyKind::Scalar(Scalar::Float(_)), TyKind::Scalar(Scalar::Float(_))) => rhs_ty,
|
|
|
|
// ({int}, int) | ({int}, uint)
|
|
|
|
(TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Int(_)))
|
|
|
|
| (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Uint(_))) => {
|
|
|
|
rhs_ty
|
|
|
|
}
|
|
|
|
// (int, {int}) | (uint, {int})
|
|
|
|
(TyKind::Scalar(Scalar::Int(_)), TyKind::InferenceVar(_, TyVariableKind::Integer))
|
|
|
|
| (TyKind::Scalar(Scalar::Uint(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) => {
|
|
|
|
lhs_ty
|
|
|
|
}
|
|
|
|
// ({float} | float)
|
|
|
|
(TyKind::InferenceVar(_, TyVariableKind::Float), TyKind::Scalar(Scalar::Float(_))) => {
|
|
|
|
rhs_ty
|
|
|
|
}
|
|
|
|
// (float, {float})
|
|
|
|
(TyKind::Scalar(Scalar::Float(_)), TyKind::InferenceVar(_, TyVariableKind::Float)) => {
|
|
|
|
lhs_ty
|
|
|
|
}
|
|
|
|
// ({int}, {int}) | ({float}, {float})
|
|
|
|
(
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Integer),
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Integer),
|
|
|
|
)
|
|
|
|
| (
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Float),
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Float),
|
|
|
|
) => rhs_ty,
|
2021-04-05 12:37:11 +00:00
|
|
|
_ => TyKind::Error.intern(&Interner),
|
2019-02-23 14:24:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
|
|
|
|
match op {
|
2021-03-13 13:44:51 +00:00
|
|
|
BinaryOp::LogicOp(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
|
2020-05-10 14:20:13 +00:00
|
|
|
BinaryOp::Assignment { op: None } => lhs_ty,
|
2021-04-03 11:08:29 +00:00
|
|
|
BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty.kind(&Interner) {
|
2021-03-13 13:44:51 +00:00
|
|
|
TyKind::Scalar(_) | TyKind::Str => lhs_ty,
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Integer)
|
|
|
|
| TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
|
2021-04-05 12:37:11 +00:00
|
|
|
_ => TyKind::Error.intern(&Interner),
|
2020-02-18 13:32:19 +00:00
|
|
|
},
|
2021-03-13 13:44:51 +00:00
|
|
|
BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => {
|
2021-04-05 12:37:11 +00:00
|
|
|
TyKind::Error.intern(&Interner)
|
2021-03-13 13:44:51 +00:00
|
|
|
}
|
2019-08-17 14:51:01 +00:00
|
|
|
BinaryOp::CmpOp(CmpOp::Ord { .. })
|
|
|
|
| BinaryOp::Assignment { op: Some(_) }
|
2021-04-03 11:08:29 +00:00
|
|
|
| BinaryOp::ArithOp(_) => match lhs_ty.kind(&Interner) {
|
2021-03-13 13:44:51 +00:00
|
|
|
TyKind::Scalar(Scalar::Int(_))
|
|
|
|
| TyKind::Scalar(Scalar::Uint(_))
|
|
|
|
| TyKind::Scalar(Scalar::Float(_)) => lhs_ty,
|
|
|
|
TyKind::InferenceVar(_, TyVariableKind::Integer)
|
|
|
|
| TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
|
2021-04-05 12:37:11 +00:00
|
|
|
_ => TyKind::Error.intern(&Interner),
|
2019-08-17 14:51:01 +00:00
|
|
|
},
|
2019-02-23 14:24:07 +00:00
|
|
|
}
|
|
|
|
}
|