aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-17 15:42:41 +0100
committerAleksey Kladov <[email protected]>2019-08-17 15:42:41 +0100
commit7e5a186c1fe585aac95019addc963bf74cb112ae (patch)
tree3ece58abec006c824f38e8470f67229bc7c6acdd /crates/ra_hir/src/ty
parent8919aa8065c31d55050a6bfe10b574fc71bcec09 (diff)
Introduce separate hir::BinaryOp
Unlike ast::BinOp, it has significantly more structure to it, so it's easier to, say, handle all assignment-like operations in the same way.
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/infer.rs4
-rw-r--r--crates/ra_hir/src/ty/op.rs84
2 files changed, 21 insertions, 67 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 675df4a22..33bfd0952 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -1265,9 +1265,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1265 Expr::BinaryOp { lhs, rhs, op } => match op { 1265 Expr::BinaryOp { lhs, rhs, op } => match op {
1266 Some(op) => { 1266 Some(op) => {
1267 let lhs_expectation = match op { 1267 let lhs_expectation = match op {
1268 BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { 1268 BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)),
1269 Expectation::has_type(Ty::simple(TypeCtor::Bool))
1270 }
1271 _ => Expectation::none(), 1269 _ => Expectation::none(),
1272 }; 1270 };
1273 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); 1271 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
diff --git a/crates/ra_hir/src/ty/op.rs b/crates/ra_hir/src/ty/op.rs
index 9ba868298..1d089f1b0 100644
--- a/crates/ra_hir/src/ty/op.rs
+++ b/crates/ra_hir/src/ty/op.rs
@@ -1,37 +1,14 @@
1use super::{InferTy, Ty, TypeCtor}; 1use super::{InferTy, Ty, TypeCtor};
2use crate::{expr::BinaryOp, ty::ApplicationTy}; 2use crate::{
3 expr::{BinaryOp, CmpOp},
4 ty::ApplicationTy,
5};
3 6
4pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { 7pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
5 match op { 8 match op {
6 BinaryOp::BooleanOr 9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool),
7 | BinaryOp::BooleanAnd 10 BinaryOp::Assignment { .. } => Ty::unit(),
8 | BinaryOp::EqualityTest 11 BinaryOp::ArithOp(_) => match rhs_ty {
9 | BinaryOp::NegatedEqualityTest
10 | BinaryOp::LesserEqualTest
11 | BinaryOp::GreaterEqualTest
12 | BinaryOp::LesserTest
13 | BinaryOp::GreaterTest => Ty::simple(TypeCtor::Bool),
14 BinaryOp::Assignment
15 | BinaryOp::AddAssign
16 | BinaryOp::SubAssign
17 | BinaryOp::DivAssign
18 | BinaryOp::MulAssign
19 | BinaryOp::RemAssign
20 | BinaryOp::ShrAssign
21 | BinaryOp::ShlAssign
22 | BinaryOp::BitAndAssign
23 | BinaryOp::BitOrAssign
24 | BinaryOp::BitXorAssign => Ty::unit(),
25 BinaryOp::Addition
26 | BinaryOp::Subtraction
27 | BinaryOp::Multiplication
28 | BinaryOp::Division
29 | BinaryOp::Remainder
30 | BinaryOp::LeftShift
31 | BinaryOp::RightShift
32 | BinaryOp::BitwiseAnd
33 | BinaryOp::BitwiseOr
34 | BinaryOp::BitwiseXor => match rhs_ty {
35 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 12 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
36 TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, 13 TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty,
37 _ => Ty::Unknown, 14 _ => Ty::Unknown,
@@ -39,14 +16,15 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
39 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, 16 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
40 _ => Ty::Unknown, 17 _ => Ty::Unknown,
41 }, 18 },
42 BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown,
43 } 19 }
44} 20}
45 21
46pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { 22pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
47 match op { 23 match op {
48 BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeCtor::Bool), 24 BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool),
49 BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty { 25 BinaryOp::Assignment { op: None }
26 | BinaryOp::CmpOp(CmpOp::Equal)
27 | BinaryOp::CmpOp(CmpOp::NotEqual) => match lhs_ty {
50 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 28 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
51 TypeCtor::Int(..) 29 TypeCtor::Int(..)
52 | TypeCtor::Float(..) 30 | TypeCtor::Float(..)
@@ -58,37 +36,15 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
58 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 36 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
59 _ => Ty::Unknown, 37 _ => Ty::Unknown,
60 }, 38 },
61 BinaryOp::LesserEqualTest 39 BinaryOp::CmpOp(_) | BinaryOp::Assignment { op: Some(_) } | BinaryOp::ArithOp(_) => {
62 | BinaryOp::GreaterEqualTest 40 match lhs_ty {
63 | BinaryOp::LesserTest 41 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
64 | BinaryOp::GreaterTest 42 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
65 | BinaryOp::AddAssign 43 _ => Ty::Unknown,
66 | BinaryOp::SubAssign 44 },
67 | BinaryOp::DivAssign 45 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
68 | BinaryOp::MulAssign
69 | BinaryOp::RemAssign
70 | BinaryOp::ShrAssign
71 | BinaryOp::ShlAssign
72 | BinaryOp::BitAndAssign
73 | BinaryOp::BitOrAssign
74 | BinaryOp::BitXorAssign
75 | BinaryOp::Addition
76 | BinaryOp::Subtraction
77 | BinaryOp::Multiplication
78 | BinaryOp::Division
79 | BinaryOp::Remainder
80 | BinaryOp::LeftShift
81 | BinaryOp::RightShift
82 | BinaryOp::BitwiseAnd
83 | BinaryOp::BitwiseOr
84 | BinaryOp::BitwiseXor => match lhs_ty {
85 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
86 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
87 _ => Ty::Unknown, 46 _ => Ty::Unknown,
88 }, 47 }
89 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 48 }
90 _ => Ty::Unknown,
91 },
92 _ => Ty::Unknown,
93 } 49 }
94} 50}