aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-16 15:11:19 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-16 15:11:19 +0000
commit454cc313589fb17de92d6f3dbf576a5ea5f4adf2 (patch)
treee67129e4e514e140463d8f0f7bd7556793cac484 /crates/ra_hir/src/ty.rs
parentd75a0368f5048243d6561e42e77835f6f574b321 (diff)
parent0aedd4fb2f28ec24902d26c7d8a24d6146263d2f (diff)
Merge #524
524: Implement array inference r=flodiebold a=h-michael related #394 Co-authored-by: Hirokazu Hata <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r--crates/ra_hir/src/ty.rs26
1 files changed, 21 insertions, 5 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 85d4dc05c..c7c063601 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
@@ -276,7 +277,10 @@ impl Ty {
276 let inner_ty = Ty::from_hir(db, module, impl_block, inner); 277 let inner_ty = Ty::from_hir(db, module, impl_block, inner);
277 Ty::RawPtr(Arc::new(inner_ty), *mutability) 278 Ty::RawPtr(Arc::new(inner_ty), *mutability)
278 } 279 }
279 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 }
280 TypeRef::Slice(inner) => { 284 TypeRef::Slice(inner) => {
281 let inner_ty = Ty::from_hir(db, module, impl_block, inner); 285 let inner_ty = Ty::from_hir(db, module, impl_block, inner);
282 Ty::Slice(Arc::new(inner_ty)) 286 Ty::Slice(Arc::new(inner_ty))
@@ -352,7 +356,7 @@ impl Ty {
352 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { 356 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
353 f(self); 357 f(self);
354 match self { 358 match self {
355 Ty::Slice(t) => Arc::make_mut(t).walk_mut(f), 359 Ty::Slice(t) | Ty::Array(t) => Arc::make_mut(t).walk_mut(f),
356 Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f), 360 Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f),
357 Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f), 361 Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f),
358 Ty::Tuple(ts) => { 362 Ty::Tuple(ts) => {
@@ -400,7 +404,7 @@ impl fmt::Display for Ty {
400 Ty::Int(t) => write!(f, "{}", t.ty_to_string()), 404 Ty::Int(t) => write!(f, "{}", t.ty_to_string()),
401 Ty::Float(t) => write!(f, "{}", t.ty_to_string()), 405 Ty::Float(t) => write!(f, "{}", t.ty_to_string()),
402 Ty::Str => write!(f, "str"), 406 Ty::Str => write!(f, "str"),
403 Ty::Slice(t) => write!(f, "[{}]", t), 407 Ty::Slice(t) | Ty::Array(t) => write!(f, "[{}]", t),
404 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),
405 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),
406 Ty::Never => write!(f, "!"), 410 Ty::Never => write!(f, "!"),
@@ -1102,6 +1106,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1102 1106
1103 Ty::Tuple(Arc::from(ty_vec)) 1107 Ty::Tuple(Arc::from(ty_vec))
1104 } 1108 }
1109 Expr::Array { exprs } => {
1110 let elem_ty = match &expected.ty {
1111 Ty::Slice(inner) | Ty::Array(inner) => Ty::clone(&inner),
1112 _ => self.new_type_var(),
1113 };
1114
1115 for expr in exprs.iter() {
1116 self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone()));
1117 }
1118
1119 Ty::Array(Arc::new(elem_ty))
1120 }
1105 Expr::Literal(lit) => match lit { 1121 Expr::Literal(lit) => match lit {
1106 Literal::Bool(..) => Ty::Bool, 1122 Literal::Bool(..) => Ty::Bool,
1107 Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), 1123 Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared),