aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-17 16:05:20 +0100
committerAleksey Kladov <[email protected]>2019-08-17 16:05:20 +0100
commit189d879659f4e44c3343023d6455bed7cdf0e7c9 (patch)
treeeba980071d5c8941fdd3adc11fc5525d243ba2b3 /crates
parentb082cd679ad1ae7646d03261bcccda435443365c (diff)
implement initial type inference for index expressions
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/expr.rs14
-rw-r--r--crates/ra_hir/src/ty/infer.rs6
-rw-r--r--crates/ra_hir/src/ty/tests.rs14
3 files changed, 33 insertions, 1 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 5430a0c9f..a16561d11 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -245,6 +245,10 @@ pub enum Expr {
245 rhs: ExprId, 245 rhs: ExprId,
246 op: Option<BinaryOp>, 246 op: Option<BinaryOp>,
247 }, 247 },
248 Index {
249 base: ExprId,
250 index: ExprId,
251 },
248 Lambda { 252 Lambda {
249 args: Vec<PatId>, 253 args: Vec<PatId>,
250 arg_types: Vec<Option<TypeRef>>, 254 arg_types: Vec<Option<TypeRef>>,
@@ -399,6 +403,10 @@ impl Expr {
399 f(*lhs); 403 f(*lhs);
400 f(*rhs); 404 f(*rhs);
401 } 405 }
406 Expr::Index { base, index } => {
407 f(*base);
408 f(*index);
409 }
402 Expr::Field { expr, .. } 410 Expr::Field { expr, .. }
403 | Expr::Await { expr } 411 | Expr::Await { expr }
404 | Expr::Try { expr } 412 | Expr::Try { expr }
@@ -887,10 +895,14 @@ where
887 }; 895 };
888 self.alloc_expr(Expr::Literal(lit), syntax_ptr) 896 self.alloc_expr(Expr::Literal(lit), syntax_ptr)
889 } 897 }
898 ast::ExprKind::IndexExpr(e) => {
899 let base = self.collect_expr_opt(e.base());
900 let index = self.collect_expr_opt(e.index());
901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
902 }
890 903
891 // FIXME implement HIR for these: 904 // FIXME implement HIR for these:
892 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 905 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
893 ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
894 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 906 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
895 ast::ExprKind::MacroCall(e) => { 907 ast::ExprKind::MacroCall(e) => {
896 let ast_id = self 908 let ast_id = self
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 33bfd0952..cca59538a 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -1279,6 +1279,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1279 } 1279 }
1280 _ => Ty::Unknown, 1280 _ => Ty::Unknown,
1281 }, 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 }
1282 Expr::Tuple { exprs } => { 1288 Expr::Tuple { exprs } => {
1283 let mut ty_vec = Vec::with_capacity(exprs.len()); 1289 let mut ty_vec = Vec::with_capacity(exprs.len());
1284 for arg in exprs.iter() { 1290 for arg in exprs.iter() {
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]
2659fn 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]
2659fn infer_macros_expanded() { 2673fn infer_macros_expanded() {
2660 assert_snapshot_matches!( 2674 assert_snapshot_matches!(
2661 infer(r#" 2675 infer(r#"