diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-05 20:28:30 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-05 20:28:30 +0000 |
commit | 4fc233a02e8dc07619a969400c445ec47c2b1a9d (patch) | |
tree | dae33907a38b7c4ed9d1a63ff38803e99f1ae6df /crates/ra_hir | |
parent | 3e42a158787955ff9f2e81be43479dbe8f2b1bb6 (diff) |
Implement type inference for boolean operators
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt | 10 |
3 files changed, 38 insertions, 2 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index e33762e0d..718e193f7 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 | ||
27 | use ra_db::{LocalSyntaxPtr, Cancelable}; | 27 | use ra_db::{LocalSyntaxPtr, Cancelable}; |
28 | use ra_syntax::{ | 28 | use 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 | ||
@@ -906,7 +906,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
906 | } | 906 | } |
907 | } | 907 | } |
908 | ast::Expr::RangeExpr(_e) => Ty::Unknown, | 908 | ast::Expr::RangeExpr(_e) => Ty::Unknown, |
909 | ast::Expr::BinExpr(_e) => Ty::Unknown, | 909 | ast::Expr::BinExpr(e) => match e.op() { |
910 | Some(BinOp::BooleanOr) | ||
911 | | Some(BinOp::BooleanAnd) | ||
912 | | Some(BinOp::EqualityTest) | ||
913 | | Some(BinOp::LesserEqualTest) | ||
914 | | Some(BinOp::GreaterEqualTest) | ||
915 | | Some(BinOp::LesserTest) | ||
916 | | Some(BinOp::GreaterTest) => Ty::Bool, | ||
917 | _ => Ty::Unknown, | ||
918 | }, | ||
910 | ast::Expr::Literal(_e) => Ty::Unknown, | 919 | ast::Expr::Literal(_e) => Ty::Unknown, |
911 | }; | 920 | }; |
912 | // use a new type variable if we got Ty::Unknown here | 921 | // 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 fb53fcf0b..97c466890 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -153,6 +153,23 @@ impl S { | |||
153 | ); | 153 | ); |
154 | } | 154 | } |
155 | 155 | ||
156 | #[test] | ||
157 | fn infer_boolean_op() { | ||
158 | check_inference( | ||
159 | r#" | ||
160 | fn test() { | ||
161 | let x = a && b; | ||
162 | let y = true || false; | ||
163 | let z = x == y; | ||
164 | let h = CONST_1 <= CONST_2; | ||
165 | |||
166 | 10 < 3 | ||
167 | } | ||
168 | "#, | ||
169 | "0008_boolean_op.txt", | ||
170 | ); | ||
171 | } | ||
172 | |||
156 | fn infer(content: &str) -> String { | 173 | fn infer(content: &str) -> String { |
157 | let (db, _, file_id) = MockDatabase::with_single_file(content); | 174 | let (db, _, file_id) = MockDatabase::with_single_file(content); |
158 | let source_file = db.source_file(file_id); | 175 | 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..cc07cdccb --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt | |||
@@ -0,0 +1,10 @@ | |||
1 | [21; 22) 'x': bool | ||
2 | [68; 69) 'z': bool | ||
3 | [72; 78) 'x == y': bool | ||
4 | [45; 58) 'true || false': bool | ||
5 | [11; 125) '{ ... < 3 }': bool | ||
6 | [117; 123) '10 < 3': bool | ||
7 | [88; 89) 'h': bool | ||
8 | [41; 42) 'y': bool | ||
9 | [92; 110) 'CONST_...ONST_2': bool | ||
10 | [25; 31) 'a && b': bool | ||