diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-06 21:28:36 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-06 21:28:36 +0000 |
commit | 31c1999505ccb51584dee45fb9fa1ffe16b1608e (patch) | |
tree | 64404f418c59cbbf7dcb52a834da0d44d444a8c5 /crates/ra_hir/src/ty.rs | |
parent | 0d59422b18de8dce416d792b9e7dbe9b8d5aa30a (diff) | |
parent | 82d9a77dade454ee8d09f198fa839e7755ff7bfb (diff) |
Merge #440
440: Implement type inference for boolean operators r=flodiebold a=marcusklaas
Tried implementing the easiest part of https://github.com/rust-analyzer/rust-analyzer/issues/390. Hope this is somewhat close to what the intent of the issue was. Found it surprisingly easy to find my way around the repository - it's well organized!
Very grateful for any pointers.
Co-authored-by: Marcus Klaas de Vries <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 35 |
1 files changed, 33 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 | ||
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 | ||
@@ -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 | ||
532 | fn 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 | |||
530 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 544 | impl<'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 |