aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorHirokazu Hata <[email protected]>2019-01-14 13:51:54 +0000
committerHirokazu Hata <[email protected]>2019-01-16 14:37:42 +0000
commit5d3884d5b49e06991f8f1b0d9031f8dd37edd6ab (patch)
treee38b29f467f7f9d412cad1a297dcd1a8b6d65df9 /crates/ra_hir/src
parent2a3262c75792d90341e21c241b2f95ef8cdccb23 (diff)
Fix Ty::Array
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/ty.rs37
1 files 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 {
181 /// The pointee of a string slice. Written as `str`. 181 /// The pointee of a string slice. Written as `str`.
182 Str, 182 Str,
183 183
184 // An array with the given length. Written as `[T; n]`.
185 // Array(Ty, ty::Const),
186 /// The pointee of an array slice. Written as `[T]`. 184 /// The pointee of an array slice. Written as `[T]`.
187 Slice(Arc<Ty>), 185 Slice(Arc<Ty>),
188 186
187 // An array with the given length. Written as `[T; n]`.
188 Array(Arc<Ty>),
189
189 /// A raw pointer. Written as `*mut T` or `*const T` 190 /// A raw pointer. Written as `*mut T` or `*const T`
190 RawPtr(Arc<Ty>, Mutability), 191 RawPtr(Arc<Ty>, Mutability),
191 192
@@ -227,9 +228,6 @@ pub enum Ty {
227 /// A tuple type. For example, `(i32, bool)`. 228 /// A tuple type. For example, `(i32, bool)`.
228 Tuple(Arc<[Ty]>), 229 Tuple(Arc<[Ty]>),
229 230
230 /// A array type. For example, `[i32]`.
231 Array(Arc<[Ty]>),
232
233 // The projection of an associated type. For example, 231 // The projection of an associated type. For example,
234 // `<T as Trait<..>>::N`.pub 232 // `<T as Trait<..>>::N`.pub
235 // Projection(ProjectionTy), 233 // Projection(ProjectionTy),
@@ -279,7 +277,10 @@ impl Ty {
279 let inner_ty = Ty::from_hir(db, module, impl_block, inner); 277 let inner_ty = Ty::from_hir(db, module, impl_block, inner);
280 Ty::RawPtr(Arc::new(inner_ty), *mutability) 278 Ty::RawPtr(Arc::new(inner_ty), *mutability)
281 } 279 }
282 TypeRef::Array(_inner) => Ty::Unknown, // TODO 280 TypeRef::Array(inner) => {
281 let inner_ty = Ty::from_hir(db, module, impl_block, inner);
282 Ty::Array(Arc::new(inner_ty))
283 }
283 TypeRef::Slice(inner) => { 284 TypeRef::Slice(inner) => {
284 let inner_ty = Ty::from_hir(db, module, impl_block, inner); 285 let inner_ty = Ty::from_hir(db, module, impl_block, inner);
285 Ty::Slice(Arc::new(inner_ty)) 286 Ty::Slice(Arc::new(inner_ty))
@@ -403,7 +404,7 @@ impl fmt::Display for Ty {
403 Ty::Int(t) => write!(f, "{}", t.ty_to_string()), 404 Ty::Int(t) => write!(f, "{}", t.ty_to_string()),
404 Ty::Float(t) => write!(f, "{}", t.ty_to_string()), 405 Ty::Float(t) => write!(f, "{}", t.ty_to_string()),
405 Ty::Str => write!(f, "str"), 406 Ty::Str => write!(f, "str"),
406 Ty::Slice(t) => write!(f, "[{}]", t), 407 Ty::Slice(t) | Ty::Array(t) => write!(f, "[{}]", t),
407 Ty::RawPtr(t, m) => write!(f, "*{}{}", m.as_keyword_for_ptr(), t), 408 Ty::RawPtr(t, m) => write!(f, "*{}{}", m.as_keyword_for_ptr(), t),
408 Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t), 409 Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t),
409 Ty::Never => write!(f, "!"), 410 Ty::Never => write!(f, "!"),
@@ -417,16 +418,6 @@ impl fmt::Display for Ty {
417 .to_fmt(f) 418 .to_fmt(f)
418 } 419 }
419 } 420 }
420 Ty::Array(ts) => {
421 if ts.len() == 1 {
422 write!(f, "[{},]", ts[0])
423 } else {
424 join(ts.iter())
425 .surround_with("[", "]")
426 .separator(", ")
427 .to_fmt(f)
428 }
429 }
430 Ty::FnPtr(sig) => { 421 Ty::FnPtr(sig) => {
431 join(sig.input.iter()) 422 join(sig.input.iter())
432 .surround_with("fn(", ")") 423 .surround_with("fn(", ")")
@@ -1116,12 +1107,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1116 Ty::Tuple(Arc::from(ty_vec)) 1107 Ty::Tuple(Arc::from(ty_vec))
1117 }, 1108 },
1118 Expr::Array { exprs } => { 1109 Expr::Array { exprs } => {
1119 let mut ty_vec = Vec::with_capacity(exprs.len()); 1110 let mut elem_ty = match &expected.ty {
1120 for arg in exprs.iter() { 1111 Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner),
1121 ty_vec.push(self.infer_expr(*arg, &Expectation::none())); 1112 _ => self.new_type_var(),
1113 };
1114
1115 for expr in exprs.iter() {
1116 elem_ty = self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone()));
1122 } 1117 }
1123 1118
1124 Ty::Array(Arc::from(ty_vec)) 1119 Ty::Array(Arc::new(elem_ty))
1125 }, 1120 },
1126 Expr::Literal(lit) => match lit { 1121 Expr::Literal(lit) => match lit {
1127 Literal::Bool(..) => Ty::Bool, 1122 Literal::Bool(..) => Ty::Bool,