From a2b6d3da30020421c97100d7c8699a3b4f8cd6fb Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 14 Jan 2019 23:15:16 +0100 Subject: Implement rudimentary type inference for unary operators --- crates/ra_hir/src/ty.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index dbbbce795..85d4dc05c 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -1051,7 +1051,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::UnaryOp { expr, op } => { let inner_ty = self.infer_expr(*expr, &Expectation::none()); match op { - Some(UnaryOp::Deref) => { + UnaryOp::Deref => { if let Some(derefed_ty) = inner_ty.builtin_deref() { derefed_ty } else { @@ -1059,7 +1059,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Ty::Unknown } } - _ => Ty::Unknown, + UnaryOp::Neg => { + match inner_ty { + Ty::Int(primitive::UncertainIntTy::Unknown) + | Ty::Int(primitive::UncertainIntTy::Signed(..)) + | Ty::Infer(InferTy::IntVar(..)) + | Ty::Infer(InferTy::FloatVar(..)) + | Ty::Float(..) => inner_ty, + // TODO: resolve ops::Neg trait + _ => Ty::Unknown, + } + } + UnaryOp::Not if inner_ty == Ty::Bool => Ty::Bool, + // TODO: resolve ops::Not trait for inner_ty + UnaryOp::Not => Ty::Unknown, } } Expr::BinaryOp { lhs, rhs, op } => match op { -- cgit v1.2.3