diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/op.rs | 92 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 14 |
3 files changed, 45 insertions, 71 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 675df4a22..cca59538a 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -1265,9 +1265,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1265 | Expr::BinaryOp { lhs, rhs, op } => match op { | 1265 | Expr::BinaryOp { lhs, rhs, op } => match op { |
1266 | Some(op) => { | 1266 | Some(op) => { |
1267 | let lhs_expectation = match op { | 1267 | let lhs_expectation = match op { |
1268 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { | 1268 | BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)), |
1269 | Expectation::has_type(Ty::simple(TypeCtor::Bool)) | ||
1270 | } | ||
1271 | _ => Expectation::none(), | 1269 | _ => Expectation::none(), |
1272 | }; | 1270 | }; |
1273 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); | 1271 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); |
@@ -1281,6 +1279,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1281 | } | 1279 | } |
1282 | _ => Ty::Unknown, | 1280 | _ => Ty::Unknown, |
1283 | }, | 1281 | }, |
1282 | Expr::Index { base, index } => { | ||
1283 | let _base_ty = self.infer_expr(*base, &Expectation::none()); | ||
1284 | let _index_ty = self.infer_expr(*index, &Expectation::none()); | ||
1285 | // FIXME: use `std::ops::Index::Output` to figure out the real return type | ||
1286 | Ty::Unknown | ||
1287 | } | ||
1284 | Expr::Tuple { exprs } => { | 1288 | Expr::Tuple { exprs } => { |
1285 | let mut ty_vec = Vec::with_capacity(exprs.len()); | 1289 | let mut ty_vec = Vec::with_capacity(exprs.len()); |
1286 | for arg in exprs.iter() { | 1290 | for arg in exprs.iter() { |
diff --git a/crates/ra_hir/src/ty/op.rs b/crates/ra_hir/src/ty/op.rs index 9ba868298..1b30a5b9b 100644 --- a/crates/ra_hir/src/ty/op.rs +++ b/crates/ra_hir/src/ty/op.rs | |||
@@ -1,37 +1,14 @@ | |||
1 | use super::{InferTy, Ty, TypeCtor}; | 1 | use super::{InferTy, Ty, TypeCtor}; |
2 | use crate::{expr::BinaryOp, ty::ApplicationTy}; | 2 | use crate::{ |
3 | expr::{BinaryOp, CmpOp}, | ||
4 | ty::ApplicationTy, | ||
5 | }; | ||
3 | 6 | ||
4 | pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { | 7 | pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { |
5 | match op { | 8 | match op { |
6 | BinaryOp::BooleanOr | 9 | BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), |
7 | | BinaryOp::BooleanAnd | 10 | BinaryOp::Assignment { .. } => Ty::unit(), |
8 | | BinaryOp::EqualityTest | 11 | BinaryOp::ArithOp(_) => match rhs_ty { |
9 | | BinaryOp::NegatedEqualityTest | ||
10 | | BinaryOp::LesserEqualTest | ||
11 | | BinaryOp::GreaterEqualTest | ||
12 | | BinaryOp::LesserTest | ||
13 | | BinaryOp::GreaterTest => Ty::simple(TypeCtor::Bool), | ||
14 | BinaryOp::Assignment | ||
15 | | BinaryOp::AddAssign | ||
16 | | BinaryOp::SubAssign | ||
17 | | BinaryOp::DivAssign | ||
18 | | BinaryOp::MulAssign | ||
19 | | BinaryOp::RemAssign | ||
20 | | BinaryOp::ShrAssign | ||
21 | | BinaryOp::ShlAssign | ||
22 | | BinaryOp::BitAndAssign | ||
23 | | BinaryOp::BitOrAssign | ||
24 | | BinaryOp::BitXorAssign => Ty::unit(), | ||
25 | BinaryOp::Addition | ||
26 | | BinaryOp::Subtraction | ||
27 | | BinaryOp::Multiplication | ||
28 | | BinaryOp::Division | ||
29 | | BinaryOp::Remainder | ||
30 | | BinaryOp::LeftShift | ||
31 | | BinaryOp::RightShift | ||
32 | | BinaryOp::BitwiseAnd | ||
33 | | BinaryOp::BitwiseOr | ||
34 | | BinaryOp::BitwiseXor => match rhs_ty { | ||
35 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 12 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { |
36 | TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, | 13 | TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, |
37 | _ => Ty::Unknown, | 14 | _ => Ty::Unknown, |
@@ -39,49 +16,29 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { | |||
39 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, | 16 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, |
40 | _ => Ty::Unknown, | 17 | _ => Ty::Unknown, |
41 | }, | 18 | }, |
42 | BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown, | ||
43 | } | 19 | } |
44 | } | 20 | } |
45 | 21 | ||
46 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | 22 | pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { |
47 | match op { | 23 | match op { |
48 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeCtor::Bool), | 24 | BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), |
49 | BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty { | 25 | BinaryOp::Assignment { op: None } | BinaryOp::CmpOp(CmpOp::Eq { negated: _ }) => { |
50 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 26 | match lhs_ty { |
51 | TypeCtor::Int(..) | 27 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { |
52 | | TypeCtor::Float(..) | 28 | TypeCtor::Int(..) |
53 | | TypeCtor::Str | 29 | | TypeCtor::Float(..) |
54 | | TypeCtor::Char | 30 | | TypeCtor::Str |
55 | | TypeCtor::Bool => lhs_ty, | 31 | | TypeCtor::Char |
32 | | TypeCtor::Bool => lhs_ty, | ||
33 | _ => Ty::Unknown, | ||
34 | }, | ||
35 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | ||
56 | _ => Ty::Unknown, | 36 | _ => Ty::Unknown, |
57 | }, | 37 | } |
58 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | 38 | } |
59 | _ => Ty::Unknown, | 39 | BinaryOp::CmpOp(CmpOp::Ord { .. }) |
60 | }, | 40 | | BinaryOp::Assignment { op: Some(_) } |
61 | BinaryOp::LesserEqualTest | 41 | | BinaryOp::ArithOp(_) => match lhs_ty { |
62 | | BinaryOp::GreaterEqualTest | ||
63 | | BinaryOp::LesserTest | ||
64 | | BinaryOp::GreaterTest | ||
65 | | BinaryOp::AddAssign | ||
66 | | BinaryOp::SubAssign | ||
67 | | BinaryOp::DivAssign | ||
68 | | BinaryOp::MulAssign | ||
69 | | BinaryOp::RemAssign | ||
70 | | BinaryOp::ShrAssign | ||
71 | | BinaryOp::ShlAssign | ||
72 | | BinaryOp::BitAndAssign | ||
73 | | BinaryOp::BitOrAssign | ||
74 | | BinaryOp::BitXorAssign | ||
75 | | BinaryOp::Addition | ||
76 | | BinaryOp::Subtraction | ||
77 | | BinaryOp::Multiplication | ||
78 | | BinaryOp::Division | ||
79 | | BinaryOp::Remainder | ||
80 | | BinaryOp::LeftShift | ||
81 | | BinaryOp::RightShift | ||
82 | | BinaryOp::BitwiseAnd | ||
83 | | BinaryOp::BitwiseOr | ||
84 | | BinaryOp::BitwiseXor => match lhs_ty { | ||
85 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { | 42 | Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { |
86 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, | 43 | TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, |
87 | _ => Ty::Unknown, | 44 | _ => Ty::Unknown, |
@@ -89,6 +46,5 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | |||
89 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, | 46 | Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, |
90 | _ => Ty::Unknown, | 47 | _ => Ty::Unknown, |
91 | }, | 48 | }, |
92 | _ => Ty::Unknown, | ||
93 | } | 49 | } |
94 | } | 50 | } |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 28727bb18..6c2d857bc 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -2656,6 +2656,20 @@ fn test() -> u64 { | |||
2656 | } | 2656 | } |
2657 | 2657 | ||
2658 | #[test] | 2658 | #[test] |
2659 | fn indexing_arrays() { | ||
2660 | assert_snapshot_matches!( | ||
2661 | infer("fn main() { &mut [9][2]; }"), | ||
2662 | @r###" | ||
2663 | [10; 26) '{ &mut...[2]; }': () | ||
2664 | [12; 23) '&mut [9][2]': &mut {unknown} | ||
2665 | [17; 20) '[9]': [i32;_] | ||
2666 | [17; 23) '[9][2]': {unknown} | ||
2667 | [18; 19) '9': i32 | ||
2668 | [21; 22) '2': i32"### | ||
2669 | ) | ||
2670 | } | ||
2671 | |||
2672 | #[test] | ||
2659 | fn infer_macros_expanded() { | 2673 | fn infer_macros_expanded() { |
2660 | assert_snapshot_matches!( | 2674 | assert_snapshot_matches!( |
2661 | infer(r#" | 2675 | infer(r#" |