diff options
Diffstat (limited to 'crates/hir_ty/src/op.rs')
-rw-r--r-- | crates/hir_ty/src/op.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs new file mode 100644 index 000000000..0870874fc --- /dev/null +++ b/crates/hir_ty/src/op.rs | |||
@@ -0,0 +1,58 @@ | |||
1 | //! Helper functions for binary operator type inference. | ||
2 | use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; | ||
3 | |||
4 | use super::{InferTy, Ty, TypeCtor}; | ||
5 | use crate::ApplicationTy; | ||
6 | |||
7 | pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { | ||
8 | match op { | ||
9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), | ||
10 | BinaryOp::Assignment { .. } => Ty::unit(), | ||
11 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { | ||
12 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
13 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | ||
14 | _ => Ty::Unknown, | ||
15 | }, | ||
16 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
17 | _ => Ty::Unknown, | ||
18 | }, | ||
19 | BinaryOp::ArithOp(_) => match rhs_ty { | ||
20 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
21 | TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, | ||
22 | _ => Ty::Unknown, | ||
23 | }, | ||
24 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, | ||
25 | _ => Ty::Unknown, | ||
26 | }, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | ||
31 | match op { | ||
32 | BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), | ||
33 | BinaryOp::Assignment { op: None } => lhs_ty, | ||
34 | BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { | ||
35 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
36 | TypeCtor::Int(..) | ||
37 | | TypeCtor::Float(..) | ||
38 | | TypeCtor::Str | ||
39 | | TypeCtor::Char | ||
40 | | TypeCtor::Bool => lhs_ty, | ||
41 | _ => Ty::Unknown, | ||
42 | }, | ||
43 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
44 | _ => Ty::Unknown, | ||
45 | }, | ||
46 | BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown, | ||
47 | BinaryOp::CmpOp(CmpOp::Ord { .. }) | ||
48 | | BinaryOp::Assignment { op: Some(_) } | ||
49 | | BinaryOp::ArithOp(_) => match lhs_ty { | ||
50 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
51 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | ||
52 | _ => Ty::Unknown, | ||
53 | }, | ||
54 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
55 | _ => Ty::Unknown, | ||
56 | }, | ||
57 | } | ||
58 | } | ||