diff options
author | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
commit | 18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch) | |
tree | 9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_hir_ty/src/op.rs | |
parent | a7394b44c870f585eacfeb3036a33471aff49ff8 (diff) | |
parent | 484acc8a61d599662ed63a4cbda091d38a982551 (diff) |
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir_ty/src/op.rs')
-rw-r--r-- | crates/ra_hir_ty/src/op.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/op.rs b/crates/ra_hir_ty/src/op.rs new file mode 100644 index 000000000..09c47a76d --- /dev/null +++ b/crates/ra_hir_ty/src/op.rs | |||
@@ -0,0 +1,50 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | use hir_def::expr::{BinaryOp, CmpOp}; | ||
3 | |||
4 | use super::{InferTy, Ty, TypeCtor}; | ||
5 | use crate::ApplicationTy; | ||
6 | |||
7 | pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { | ||
8 | match op { | ||
9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), | ||
10 | BinaryOp::Assignment { .. } => Ty::unit(), | ||
11 | BinaryOp::ArithOp(_) => match rhs_ty { | ||
12 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
13 | TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, | ||
14 | _ => Ty::Unknown, | ||
15 | }, | ||
16 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, | ||
17 | _ => Ty::Unknown, | ||
18 | }, | ||
19 | } | ||
20 | } | ||
21 | |||
22 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | ||
23 | match op { | ||
24 | BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), | ||
25 | BinaryOp::Assignment { op: None } | BinaryOp::CmpOp(CmpOp::Eq { negated: _ }) => { | ||
26 | match lhs_ty { | ||
27 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
28 | TypeCtor::Int(..) | ||
29 | | TypeCtor::Float(..) | ||
30 | | TypeCtor::Str | ||
31 | | TypeCtor::Char | ||
32 | | TypeCtor::Bool => lhs_ty, | ||
33 | _ => Ty::Unknown, | ||
34 | }, | ||
35 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
36 | _ => Ty::Unknown, | ||
37 | } | ||
38 | } | ||
39 | BinaryOp::CmpOp(CmpOp::Ord { .. }) | ||
40 | | BinaryOp::Assignment { op: Some(_) } | ||
41 | | BinaryOp::ArithOp(_) => match lhs_ty { | ||
42 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | ||
43 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | ||
44 | _ => Ty::Unknown, | ||
45 | }, | ||
46 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
47 | _ => Ty::Unknown, | ||
48 | }, | ||
49 | } | ||
50 | } | ||