aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/ty.rs35
-rw-r--r--crates/ra_hir/src/ty/tests.rs24
-rw-r--r--crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt31
-rw-r--r--crates/ra_syntax/src/ast.rs52
4 files changed, 140 insertions, 2 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 8c320a705..b685259d7 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -26,7 +26,7 @@ use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError};
26 26
27use ra_db::{LocalSyntaxPtr, Cancelable}; 27use ra_db::{LocalSyntaxPtr, Cancelable};
28use ra_syntax::{ 28use ra_syntax::{
29 ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp}, 29 ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp, BinOp},
30 SyntaxNodeRef 30 SyntaxNodeRef
31}; 31};
32 32
@@ -527,6 +527,20 @@ struct InferenceContext<'a, D: HirDatabase> {
527 return_ty: Ty, 527 return_ty: Ty,
528} 528}
529 529
530// helper function that determines whether a binary operator
531// always returns a boolean
532fn is_boolean_operator(op: BinOp) -> bool {
533 match op {
534 BinOp::BooleanOr
535 | BinOp::BooleanAnd
536 | BinOp::EqualityTest
537 | BinOp::LesserEqualTest
538 | BinOp::GreaterEqualTest
539 | BinOp::LesserTest
540 | BinOp::GreaterTest => true,
541 }
542}
543
530impl<'a, D: HirDatabase> InferenceContext<'a, D> { 544impl<'a, D: HirDatabase> InferenceContext<'a, D> {
531 fn new( 545 fn new(
532 db: &'a D, 546 db: &'a D,
@@ -899,7 +913,24 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
899 } 913 }
900 } 914 }
901 ast::Expr::RangeExpr(_e) => Ty::Unknown, 915 ast::Expr::RangeExpr(_e) => Ty::Unknown,
902 ast::Expr::BinExpr(_e) => Ty::Unknown, 916 ast::Expr::BinExpr(e) => match e.op() {
917 Some(op) => {
918 let subtype_expectation = match op {
919 BinOp::BooleanAnd | BinOp::BooleanOr => Expectation::has_type(Ty::Bool),
920 _ => Expectation::none(),
921 };
922 let (lhs, rhs) = e.sub_exprs();
923 let _lhs_ty = self.infer_expr_opt(lhs, &subtype_expectation)?;
924 let _rhs_ty = self.infer_expr_opt(rhs, &subtype_expectation)?;
925
926 if is_boolean_operator(op) {
927 Ty::Bool
928 } else {
929 Ty::Unknown
930 }
931 }
932 _ => Ty::Unknown,
933 },
903 ast::Expr::Literal(_e) => Ty::Unknown, 934 ast::Expr::Literal(_e) => Ty::Unknown,
904 }; 935 };
905 // use a new type variable if we got Ty::Unknown here 936 // use a new type variable if we got Ty::Unknown here
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 515c66e85..25a354947 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -156,6 +156,30 @@ impl S {
156 ); 156 );
157} 157}
158 158
159#[test]
160fn infer_boolean_op() {
161 check_inference(
162 r#"
163fn f(x: bool) -> i32 {
164 0i32
165}
166
167fn test() {
168 let x = a && b;
169 let y = true || false;
170 let z = x == y;
171 let h = CONST_1 <= CONST_2;
172 let c = f(z || y) + 5;
173 let d = b;
174 let e = 3i32 && "hello world";
175
176 10 < 3
177}
178"#,
179 "0008_boolean_op.txt",
180 );
181}
182
159fn infer(content: &str) -> String { 183fn infer(content: &str) -> String {
160 let (db, _, file_id) = MockDatabase::with_single_file(content); 184 let (db, _, file_id) = MockDatabase::with_single_file(content);
161 let source_file = db.source_file(file_id); 185 let source_file = db.source_file(file_id);
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
new file mode 100644
index 000000000..ca01ad159
--- /dev/null
+++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
@@ -0,0 +1,31 @@
1[28; 32) '0i32': i32
2[22; 34) '{ 0i32 }': i32
3[6; 7) 'x': [unknown]
4[127; 134) 'CONST_1': [unknown]
5[201; 205) '3i32': bool
6[76; 77) 'y': bool
7[65; 66) 'b': bool
8[60; 66) 'a && b': bool
9[127; 145) 'CONST_...ONST_2': 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 c10169d90..9df8ec663 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -489,6 +489,58 @@ impl<'a> PrefixExpr<'a> {
489} 489}
490 490
491#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] 491#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
492pub enum BinOp {
493 /// The `||` operator for boolean OR
494 BooleanOr,
495 /// The `&&` operator for boolean AND
496 BooleanAnd,
497 /// The `==` operator for equality testing
498 EqualityTest,
499 /// The `<=` operator for lesser-equal testing
500 LesserEqualTest,
501 /// The `>=` operator for greater-equal testing
502 GreaterEqualTest,
503 /// The `<` operator for comparison
504 LesserTest,
505 /// The `>` operator for comparison
506 GreaterTest,
507 // TODO: lots of others
508}
509
510impl<'a> BinExpr<'a> {
511 pub fn op(&self) -> Option<BinOp> {
512 self.syntax()
513 .children()
514 .filter_map(|c| match c.kind() {
515 PIPEPIPE => Some(BinOp::BooleanOr),
516 AMPAMP => Some(BinOp::BooleanAnd),
517 EQEQ => Some(BinOp::EqualityTest),
518 LTEQ => Some(BinOp::LesserEqualTest),
519 GTEQ => Some(BinOp::GreaterEqualTest),
520 L_ANGLE => Some(BinOp::LesserTest),
521 R_ANGLE => Some(BinOp::GreaterTest),
522 _ => None,
523 })
524 .next()
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 }
541}
542
543#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
492pub enum SelfParamFlavor { 544pub enum SelfParamFlavor {
493 /// self 545 /// self
494 Owned, 546 Owned,