From 7b0eaef58072acc087d23faca5a9f9879f1765d5 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 7 Jan 2019 20:11:31 +0100 Subject: Implement type inference for more binary operators Mostly just for primitive numeric types such as u32 and f64. Not yet a general solution using trait resolution. --- crates/ra_hir/src/ty.rs | 85 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 23ee76f4e..e09279a68 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -529,7 +529,7 @@ struct InferenceContext<'a, D: HirDatabase> { // helper function that determines whether a binary operator // always returns a boolean -fn is_boolean_operator(op: BinaryOp) -> bool { +fn boolean_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { match op { BinaryOp::BooleanOr | BinaryOp::BooleanAnd @@ -537,8 +537,32 @@ fn is_boolean_operator(op: BinaryOp) -> bool { | BinaryOp::LesserEqualTest | BinaryOp::GreaterEqualTest | BinaryOp::LesserTest - | BinaryOp::GreaterTest => true, - _ => false, + | BinaryOp::GreaterTest => Ty::Bool, + BinaryOp::Assignment + | BinaryOp::AddAssign + | BinaryOp::SubAssign + | BinaryOp::DivAssign + | BinaryOp::MulAssign + | BinaryOp::RemAssign + | BinaryOp::ShrAssign + | BinaryOp::ShlAssign + | BinaryOp::BitAndAssign + | BinaryOp::BitOrAssign + | BinaryOp::BitXorAssign => Ty::unit(), + BinaryOp::Addition + | BinaryOp::Subtraction + | BinaryOp::Multiplication + | BinaryOp::Division + | BinaryOp::Remainder + | BinaryOp::LeftShift + | BinaryOp::RightShift + | BinaryOp::BitwiseAnd + | BinaryOp::BitwiseOr + | BinaryOp::BitwiseXor => match rhs_ty { + Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => rhs_ty, + _ => Ty::Unknown, + }, + BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown, } } @@ -890,20 +914,59 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::BinaryOp { lhs, rhs, op } => match op { Some(op) => { - let subtype_expectation = match op { + let lhs_expectation = match op { BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { Expectation::has_type(Ty::Bool) } _ => Expectation::none(), }; - let _lhs_ty = self.infer_expr(*lhs, &subtype_expectation)?; - let _rhs_ty = self.infer_expr(*rhs, &subtype_expectation)?; + let lhs_ty = self.infer_expr(*lhs, &lhs_expectation)?; + // TODO: find implementation of trait corresponding to operation + // symbol and resolve associated `Output` type + let rhs_expectation = match op { + BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::Bool, + BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty { + Ty::Uint(..) + | Ty::Int(..) + | Ty::Float(..) + | Ty::Str + | Ty::Char + | Ty::Bool => lhs_ty, + _ => Ty::Unknown, + }, + BinaryOp::LesserEqualTest + | BinaryOp::GreaterEqualTest + | BinaryOp::LesserTest + | BinaryOp::GreaterTest + | BinaryOp::AddAssign + | BinaryOp::SubAssign + | BinaryOp::DivAssign + | BinaryOp::MulAssign + | BinaryOp::RemAssign + | BinaryOp::ShrAssign + | BinaryOp::ShlAssign + | BinaryOp::BitAndAssign + | BinaryOp::BitOrAssign + | BinaryOp::BitXorAssign + | BinaryOp::Addition + | BinaryOp::Subtraction + | BinaryOp::Multiplication + | BinaryOp::Division + | BinaryOp::Remainder + | BinaryOp::LeftShift + | BinaryOp::RightShift + | BinaryOp::BitwiseAnd + | BinaryOp::BitwiseOr + | BinaryOp::BitwiseXor => match lhs_ty { + Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => lhs_ty, + _ => Ty::Unknown, + }, + _ => Ty::Unknown, + }; + let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation))?; - if is_boolean_operator(*op) { - Ty::Bool - } else { - Ty::Unknown - } + // TODO: similar as above, return ty is often associated trait type + boolean_op_return_ty(*op, rhs_ty) } _ => Ty::Unknown, }, -- cgit v1.2.3