From 5d3884d5b49e06991f8f1b0d9031f8dd37edd6ab Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Mon, 14 Jan 2019 22:51:54 +0900 Subject: Fix Ty::Array --- crates/ra_hir/src/ty.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 676ed3ac9..d3373644d 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -181,11 +181,12 @@ pub enum Ty { /// The pointee of a string slice. Written as `str`. Str, - // An array with the given length. Written as `[T; n]`. - // Array(Ty, ty::Const), /// The pointee of an array slice. Written as `[T]`. Slice(Arc), + // An array with the given length. Written as `[T; n]`. + Array(Arc), + /// A raw pointer. Written as `*mut T` or `*const T` RawPtr(Arc, Mutability), @@ -227,9 +228,6 @@ 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), @@ -279,7 +277,10 @@ impl Ty { let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::RawPtr(Arc::new(inner_ty), *mutability) } - TypeRef::Array(_inner) => Ty::Unknown, // TODO + TypeRef::Array(inner) => { + let inner_ty = Ty::from_hir(db, module, impl_block, inner); + Ty::Array(Arc::new(inner_ty)) + } TypeRef::Slice(inner) => { let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::Slice(Arc::new(inner_ty)) @@ -403,7 +404,7 @@ impl fmt::Display for Ty { Ty::Int(t) => write!(f, "{}", t.ty_to_string()), Ty::Float(t) => write!(f, "{}", t.ty_to_string()), Ty::Str => write!(f, "str"), - Ty::Slice(t) => write!(f, "[{}]", t), + Ty::Slice(t) | Ty::Array(t) => write!(f, "[{}]", t), Ty::RawPtr(t, m) => write!(f, "*{}{}", m.as_keyword_for_ptr(), t), Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t), Ty::Never => write!(f, "!"), @@ -417,16 +418,6 @@ 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(", ")") @@ -1116,12 +1107,16 @@ 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())); + let mut elem_ty = match &expected.ty { + Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner), + _ => self.new_type_var(), + }; + + for expr in exprs.iter() { + elem_ty = self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); } - Ty::Array(Arc::from(ty_vec)) + Ty::Array(Arc::new(elem_ty)) }, Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Bool, -- cgit v1.2.3