aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/op.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/op.rs')
-rw-r--r--crates/hir_ty/src/op.rs50
1 files changed, 22 insertions, 28 deletions
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs
index 0870874fc..bb9b8bbfc 100644
--- a/crates/hir_ty/src/op.rs
+++ b/crates/hir_ty/src/op.rs
@@ -1,27 +1,27 @@
1//! Helper functions for binary operator type inference. 1//! Helper functions for binary operator type inference.
2use chalk_ir::TyVariableKind;
2use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; 3use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
3 4
4use super::{InferTy, Ty, TypeCtor}; 5use crate::{Scalar, Ty};
5use crate::ApplicationTy;
6 6
7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { 7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
8 match op { 8 match op {
9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), 9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::Scalar(Scalar::Bool),
10 BinaryOp::Assignment { .. } => Ty::unit(), 10 BinaryOp::Assignment { .. } => Ty::unit(),
11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { 11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty {
12 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 12 Ty::Scalar(Scalar::Int(_))
13 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, 13 | Ty::Scalar(Scalar::Uint(_))
14 _ => Ty::Unknown, 14 | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
15 }, 15 Ty::InferenceVar(_, TyVariableKind::Integer)
16 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 16 | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
17 _ => Ty::Unknown, 17 _ => Ty::Unknown,
18 }, 18 },
19 BinaryOp::ArithOp(_) => match rhs_ty { 19 BinaryOp::ArithOp(_) => match rhs_ty {
20 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 20 Ty::Scalar(Scalar::Int(_))
21 TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, 21 | Ty::Scalar(Scalar::Uint(_))
22 _ => Ty::Unknown, 22 | Ty::Scalar(Scalar::Float(_)) => rhs_ty,
23 }, 23 Ty::InferenceVar(_, TyVariableKind::Integer)
24 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, 24 | Ty::InferenceVar(_, TyVariableKind::Float) => rhs_ty,
25 _ => Ty::Unknown, 25 _ => Ty::Unknown,
26 }, 26 },
27 } 27 }
@@ -29,29 +29,23 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
29 29
30pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { 30pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
31 match op { 31 match op {
32 BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), 32 BinaryOp::LogicOp(..) => Ty::Scalar(Scalar::Bool),
33 BinaryOp::Assignment { op: None } => lhs_ty, 33 BinaryOp::Assignment { op: None } => lhs_ty,
34 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { 34 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
35 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 35 Ty::Scalar(_) | Ty::Str => lhs_ty,
36 TypeCtor::Int(..) 36 Ty::InferenceVar(_, TyVariableKind::Integer)
37 | TypeCtor::Float(..) 37 | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
38 | TypeCtor::Str
39 | TypeCtor::Char
40 | TypeCtor::Bool => lhs_ty,
41 _ => Ty::Unknown,
42 },
43 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
44 _ => Ty::Unknown, 38 _ => Ty::Unknown,
45 }, 39 },
46 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown, 40 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown,
47 BinaryOp::CmpOp(CmpOp::Ord { .. }) 41 BinaryOp::CmpOp(CmpOp::Ord { .. })
48 | BinaryOp::Assignment { op: Some(_) } 42 | BinaryOp::Assignment { op: Some(_) }
49 | BinaryOp::ArithOp(_) => match lhs_ty { 43 | BinaryOp::ArithOp(_) => match lhs_ty {
50 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 44 Ty::Scalar(Scalar::Int(_))
51 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, 45 | Ty::Scalar(Scalar::Uint(_))
52 _ => Ty::Unknown, 46 | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
53 }, 47 Ty::InferenceVar(_, TyVariableKind::Integer)
54 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 48 | Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
55 _ => Ty::Unknown, 49 _ => Ty::Unknown,
56 }, 50 },
57 } 51 }