aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--crates/ra_syntax/src/ast.rs35
-rw-r--r--crates/ra_syntax/src/ast/generated.rs10
-rw-r--r--crates/ra_syntax/src/grammar.ron7
6 files changed, 93 insertions, 43 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
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 1bce6fa40..9df8ec663 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -511,20 +511,33 @@ impl<'a> BinExpr<'a> {
511 pub fn op(&self) -> Option<BinOp> { 511 pub fn op(&self) -> Option<BinOp> {
512 self.syntax() 512 self.syntax()
513 .children() 513 .children()
514 .filter_map(|c| { 514 .filter_map(|c| match c.kind() {
515 match c.kind() { 515 PIPEPIPE => Some(BinOp::BooleanOr),
516 PIPEPIPE => Some(BinOp::BooleanOr), 516 AMPAMP => Some(BinOp::BooleanAnd),
517 AMPAMP => Some(BinOp::BooleanAnd), 517 EQEQ => Some(BinOp::EqualityTest),
518 EQEQ => Some(BinOp::EqualityTest), 518 LTEQ => Some(BinOp::LesserEqualTest),
519 LTEQ => Some(BinOp::LesserEqualTest), 519 GTEQ => Some(BinOp::GreaterEqualTest),
520 GTEQ => Some(BinOp::GreaterEqualTest), 520 L_ANGLE => Some(BinOp::LesserTest),
521 L_ANGLE => Some(BinOp::LesserTest), 521 R_ANGLE => Some(BinOp::GreaterTest),
522 R_ANGLE => Some(BinOp::GreaterTest), 522 _ => None,
523 _ => None,
524 }
525 }) 523 })
526 .next() 524 .next()
527 } 525 }
526
527 pub fn lhs(self) -> Option<Expr<'a>> {
528 children(self).nth(0)
529 }
530
531 pub fn rhs(self) -> Option<Expr<'a>> {
532 children(self).nth(1)
533 }
534
535 pub fn sub_exprs(self) -> (Option<Expr<'a>>, Option<Expr<'a>>) {
536 let mut children = children(self);
537 let first = children.next();
538 let second = children.next();
539 (first, second)
540 }
528} 541}
529 542
530#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] 543#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index ac320606d..7df6a9c46 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -217,15 +217,7 @@ impl<R: TreeRoot<RaTypes>> BinExprNode<R> {
217} 217}
218 218
219 219
220impl<'a> BinExpr<'a> { 220impl<'a> BinExpr<'a> {}
221 pub fn lhs(self) -> Option<Expr<'a>> {
222 super::child_opt(self)
223 }
224
225 pub fn rhs(self) -> Option<Expr<'a>> {
226 super::child_opt(self)
227 }
228}
229 221
230// BindPat 222// BindPat
231#[derive(Debug, Clone, Copy,)] 223#[derive(Debug, Clone, Copy,)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index e59961da3..c55e9e07a 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -422,12 +422,7 @@ Grammar(
422 "RefExpr": (options: ["Expr"]), 422 "RefExpr": (options: ["Expr"]),
423 "PrefixExpr": (options: ["Expr"]), 423 "PrefixExpr": (options: ["Expr"]),
424 "RangeExpr": (), 424 "RangeExpr": (),
425 "BinExpr": ( 425 "BinExpr": (),
426 options: [
427 ["lhs", "Expr"],
428 ["rhs", "Expr"]
429 ]
430 ),
431 "String": (), 426 "String": (),
432 "Byte": (), 427 "Byte": (),
433 "ByteString": (), 428 "ByteString": (),