From 189d879659f4e44c3343023d6455bed7cdf0e7c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 17 Aug 2019 18:05:20 +0300 Subject: implement initial type inference for index expressions --- crates/ra_hir/src/expr.rs | 14 +++++++++++++- crates/ra_hir/src/ty/infer.rs | 6 ++++++ crates/ra_hir/src/ty/tests.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) 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 { rhs: ExprId, op: Option, }, + Index { + base: ExprId, + index: ExprId, + }, Lambda { args: Vec, arg_types: Vec>, @@ -399,6 +403,10 @@ impl Expr { f(*lhs); f(*rhs); } + Expr::Index { base, index } => { + f(*base); + f(*index); + } Expr::Field { expr, .. } | Expr::Await { expr } | Expr::Try { expr } @@ -887,10 +895,14 @@ where }; self.alloc_expr(Expr::Literal(lit), syntax_ptr) } + ast::ExprKind::IndexExpr(e) => { + let base = self.collect_expr_opt(e.base()); + let index = self.collect_expr_opt(e.index()); + self.alloc_expr(Expr::Index { base, index }, syntax_ptr) + } // FIXME implement HIR for these: ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), - ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), ast::ExprKind::MacroCall(e) => { 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> { } _ => Ty::Unknown, }, + Expr::Index { base, index } => { + let _base_ty = self.infer_expr(*base, &Expectation::none()); + let _index_ty = self.infer_expr(*index, &Expectation::none()); + // FIXME: use `std::ops::Index::Output` to figure out the real return type + Ty::Unknown + } Expr::Tuple { exprs } => { let mut ty_vec = Vec::with_capacity(exprs.len()); 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 @@ -2655,6 +2655,20 @@ fn test() -> u64 { ); } +#[test] +fn indexing_arrays() { + assert_snapshot_matches!( + infer("fn main() { &mut [9][2]; }"), + @r###" +[10; 26) '{ &mut...[2]; }': () +[12; 23) '&mut [9][2]': &mut {unknown} +[17; 20) '[9]': [i32;_] +[17; 23) '[9][2]': {unknown} +[18; 19) '9': i32 +[21; 22) '2': i32"### + ) +} + #[test] fn infer_macros_expanded() { assert_snapshot_matches!( -- cgit v1.2.3