diff options
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 |