From d665acbbec39ffd26eb56b1194653459a0c495e7 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 13 Jan 2019 22:46:36 +0900 Subject: Implement array inference --- crates/ra_hir/src/ty.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (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 85d4dc05c..676ed3ac9 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -227,6 +227,9 @@ pub enum Ty { /// A tuple type. For example, `(i32, bool)`. Tuple(Arc<[Ty]>), + /// A array type. For example, `[i32]`. + Array(Arc<[Ty]>), + // The projection of an associated type. For example, // `>::N`.pub // Projection(ProjectionTy), @@ -414,6 +417,16 @@ impl fmt::Display for Ty { .to_fmt(f) } } + Ty::Array(ts) => { + if ts.len() == 1 { + write!(f, "[{},]", ts[0]) + } else { + join(ts.iter()) + .surround_with("[", "]") + .separator(", ") + .to_fmt(f) + } + } Ty::FnPtr(sig) => { join(sig.input.iter()) .surround_with("fn(", ")") @@ -1101,7 +1114,15 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Ty::Tuple(Arc::from(ty_vec)) - } + }, + Expr::Array { exprs } => { + let mut ty_vec = Vec::with_capacity(exprs.len()); + for arg in exprs.iter() { + ty_vec.push(self.infer_expr(*arg, &Expectation::none())); + } + + Ty::Array(Arc::from(ty_vec)) + }, Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Bool, Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), -- cgit v1.2.3