From 5d15dd70b037b3d1623ebd83d8ef0f66ad6950af Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 7 Jan 2019 20:39:23 +0100 Subject: Tidy up binary operator type inference; add test file --- crates/ra_hir/src/ty.rs | 85 +++++++++++++-------------- crates/ra_hir/src/ty/tests/data/binary_op.txt | 46 +++++++++++++++ 2 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 crates/ra_hir/src/ty/tests/data/binary_op.txt (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index e09279a68..7827e82c4 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -527,9 +527,7 @@ struct InferenceContext<'a, D: HirDatabase> { return_ty: Ty, } -// helper function that determines whether a binary operator -// always returns a boolean -fn boolean_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { +fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { match op { BinaryOp::BooleanOr | BinaryOp::BooleanAnd @@ -566,6 +564,44 @@ fn boolean_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { } } +fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { + 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, + } +} + impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn new( db: &'a D, @@ -923,50 +959,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { 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_expectation = binary_op_rhs_expectation(*op, lhs_ty); let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation))?; // TODO: similar as above, return ty is often associated trait type - boolean_op_return_ty(*op, rhs_ty) + binary_op_return_ty(*op, rhs_ty) } _ => Ty::Unknown, }, diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt new file mode 100644 index 000000000..59c07ff43 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt @@ -0,0 +1,46 @@ +[6; 7) 'x': [unknown] +[22; 34) '{ 0i32 }': i32 +[28; 32) '0i32': i32 +[46; 342) '{ ... < 3 }': bool +[56; 57) 'x': bool +[60; 61) 'a': bool +[60; 66) 'a && b': bool +[65; 66) 'b': bool +[76; 77) 'y': bool +[80; 84) 'true': bool +[80; 93) 'true || false': bool +[88; 93) 'false': bool +[103; 104) 'z': bool +[107; 108) 'x': bool +[107; 113) 'x == y': bool +[112; 113) 'y': bool +[123; 134) 'minus_forty': isize +[144; 152) '-40isize': isize +[145; 152) '40isize': [unknown] +[162; 163) 'h': bool +[166; 177) 'minus_forty': isize +[166; 188) 'minus_...ONST_2': bool +[181; 188) 'CONST_2': isize +[198; 199) 'c': i32 +[202; 203) 'f': fn([unknown],) -> i32 +[202; 211) 'f(z || y)': i32 +[202; 215) 'f(z || y) + 5': i32 +[204; 205) 'z': bool +[204; 210) 'z || y': bool +[209; 210) 'y': bool +[214; 215) '5': i32 +[225; 226) 'd': [unknown] +[229; 230) 'b': [unknown] +[240; 241) 'g': () +[244; 255) 'minus_forty': isize +[244; 260) 'minus_...y ^= i': () +[259; 260) 'i': isize +[270; 273) 'ten': usize +[283; 285) '10': usize +[295; 308) 'ten_is_eleven': bool +[311; 314) 'ten': usize +[311; 326) 'ten == some_num': bool +[318; 326) 'some_num': usize +[333; 336) 'ten': usize +[333; 340) 'ten < 3': bool +[339; 340) '3': usize -- cgit v1.2.3