aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorMarcus Klaas de Vries <[email protected]>2019-01-06 20:39:36 +0000
committerMarcus Klaas de Vries <[email protected]>2019-01-06 21:17:54 +0000
commit82d9a77dade454ee8d09f198fa839e7755ff7bfb (patch)
tree791b3c0ee6069a06896c815795ff7e86877887df /crates/ra_hir/src
parent4fc233a02e8dc07619a969400c445ec47c2b1a9d (diff)
Touch up type inference for boolean operators
Also try to infer its subexpressions and set type expectations whenever possible.
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/ty.rs36
-rw-r--r--crates/ra_hir/src/ty/tests.rs7
-rw-r--r--crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt41
3 files changed, 67 insertions, 17 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 718e193f7..8f56cdb15 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -526,6 +526,20 @@ struct InferenceContext<'a, D: HirDatabase> {
526 return_ty: Ty, 526 return_ty: Ty,
527} 527}
528 528
529// helper function that determines whether a binary operator
530// always returns a boolean
531fn is_boolean_operator(op: BinOp) -> bool {
532 match op {
533 BinOp::BooleanOr
534 | BinOp::BooleanAnd
535 | BinOp::EqualityTest
536 | BinOp::LesserEqualTest
537 | BinOp::GreaterEqualTest
538 | BinOp::LesserTest
539 | BinOp::GreaterTest => true,
540 }
541}
542
529impl<'a, D: HirDatabase> InferenceContext<'a, D> { 543impl<'a, D: HirDatabase> InferenceContext<'a, D> {
530 fn new( 544 fn new(
531 db: &'a D, 545 db: &'a D,
@@ -907,13 +921,21 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
907 } 921 }
908 ast::Expr::RangeExpr(_e) => Ty::Unknown, 922 ast::Expr::RangeExpr(_e) => Ty::Unknown,
909 ast::Expr::BinExpr(e) => match e.op() { 923 ast::Expr::BinExpr(e) => match e.op() {
910 Some(BinOp::BooleanOr) 924 Some(op) => {
911 | Some(BinOp::BooleanAnd) 925 let subtype_expectation = match op {
912 | Some(BinOp::EqualityTest) 926 BinOp::BooleanAnd | BinOp::BooleanOr => Expectation::has_type(Ty::Bool),
913 | Some(BinOp::LesserEqualTest) 927 _ => Expectation::none(),
914 | Some(BinOp::GreaterEqualTest) 928 };
915 | Some(BinOp::LesserTest) 929 let (lhs, rhs) = e.sub_exprs();
916 | Some(BinOp::GreaterTest) => Ty::Bool, 930 let _lhs_ty = self.infer_expr_opt(lhs, &subtype_expectation)?;
931 let _rhs_ty = self.infer_expr_opt(rhs, &subtype_expectation)?;
932
933 if is_boolean_operator(op) {
934 Ty::Bool
935 } else {
936 Ty::Unknown
937 }
938 }
917 _ => Ty::Unknown, 939 _ => Ty::Unknown,
918 }, 940 },
919 ast::Expr::Literal(_e) => Ty::Unknown, 941 ast::Expr::Literal(_e) => Ty::Unknown,
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 97c466890..1650606b7 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -157,11 +157,18 @@ impl S {
157fn infer_boolean_op() { 157fn infer_boolean_op() {
158 check_inference( 158 check_inference(
159 r#" 159 r#"
160fn f(x: bool) -> i32 {
161 0i32
162}
163
160fn test() { 164fn test() {
161 let x = a && b; 165 let x = a && b;
162 let y = true || false; 166 let y = true || false;
163 let z = x == y; 167 let z = x == y;
164 let h = CONST_1 <= CONST_2; 168 let h = CONST_1 <= CONST_2;
169 let c = f(z || y) + 5;
170 let d = b;
171 let e = 3i32 && "hello world";
165 172
166 10 < 3 173 10 < 3
167} 174}
diff --git a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
index cc07cdccb..ca01ad159 100644
--- a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
+++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
@@ -1,10 +1,31 @@
1[21; 22) 'x': bool 1[28; 32) '0i32': i32
2[68; 69) 'z': bool 2[22; 34) '{ 0i32 }': i32
3[72; 78) 'x == y': bool 3[6; 7) 'x': [unknown]
4[45; 58) 'true || false': bool 4[127; 134) 'CONST_1': [unknown]
5[11; 125) '{ ... < 3 }': bool 5[201; 205) '3i32': bool
6[117; 123) '10 < 3': bool 6[76; 77) 'y': bool
7[88; 89) 'h': bool 7[65; 66) 'b': bool
8[41; 42) 'y': bool 8[60; 66) 'a && b': bool
9[92; 110) 'CONST_...ONST_2': bool 9[127; 145) 'CONST_...ONST_2': bool
10[25; 31) 'a && b': bool 10[182; 183) 'd': [unknown]
11[229; 231) '10': [unknown]
12[209; 222) '"hello world"': bool
13[229; 235) '10 < 3': bool
14[186; 187) 'b': [unknown]
15[159; 172) 'f(z || y) + 5': [unknown]
16[56; 57) 'x': bool
17[112; 113) 'y': bool
18[201; 222) '3i32 &...world"': bool
19[234; 235) '3': [unknown]
20[138; 145) 'CONST_2': [unknown]
21[80; 93) 'true || false': bool
22[46; 237) '{ ... < 3 }': bool
23[197; 198) 'e': bool
24[107; 113) 'x == y': bool
25[88; 93) 'false': bool
26[80; 84) 'true': bool
27[123; 124) 'h': bool
28[155; 156) 'c': [unknown]
29[103; 104) 'z': bool
30[60; 61) 'a': bool
31[107; 108) 'x': bool