diff options
author | Hirokazu Hata <[email protected]> | 2019-01-13 13:46:36 +0000 |
---|---|---|
committer | Hirokazu Hata <[email protected]> | 2019-01-16 14:35:06 +0000 |
commit | d665acbbec39ffd26eb56b1194653459a0c495e7 (patch) | |
tree | b3c271849eefa4cb35ed1d737d1977613ecf86d0 | |
parent | 4729a1b05427aa7d63b1dcb9ee6dceef2cfc613e (diff) |
Implement array inference
-rw-r--r-- | crates/ra_hir/src/expr.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 23 |
2 files changed, 30 insertions, 3 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 663338844..6e98ebc69 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -197,6 +197,9 @@ pub enum Expr { | |||
197 | Tuple { | 197 | Tuple { |
198 | exprs: Vec<ExprId>, | 198 | exprs: Vec<ExprId>, |
199 | }, | 199 | }, |
200 | Array { | ||
201 | exprs: Vec<ExprId>, | ||
202 | }, | ||
200 | Literal(Literal), | 203 | Literal(Literal), |
201 | } | 204 | } |
202 | 205 | ||
@@ -312,7 +315,7 @@ impl Expr { | |||
312 | | Expr::UnaryOp { expr, .. } => { | 315 | | Expr::UnaryOp { expr, .. } => { |
313 | f(*expr); | 316 | f(*expr); |
314 | } | 317 | } |
315 | Expr::Tuple { exprs } => { | 318 | Expr::Tuple { exprs } | Expr::Array { exprs } => { |
316 | for expr in exprs { | 319 | for expr in exprs { |
317 | f(*expr); | 320 | f(*expr); |
318 | } | 321 | } |
@@ -649,6 +652,10 @@ impl ExprCollector { | |||
649 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | 652 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); |
650 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | 653 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) |
651 | } | 654 | } |
655 | ast::ExprKind::ArrayExpr(e) => { | ||
656 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | ||
657 | self.alloc_expr(Expr::Array { exprs }, syntax_ptr) | ||
658 | } | ||
652 | ast::ExprKind::Literal(e) => { | 659 | ast::ExprKind::Literal(e) => { |
653 | let child = if let Some(child) = e.literal_expr() { | 660 | let child = if let Some(child) = e.literal_expr() { |
654 | child | 661 | child |
@@ -691,7 +698,6 @@ impl ExprCollector { | |||
691 | // TODO implement HIR for these: | 698 | // TODO implement HIR for these: |
692 | ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | 699 | ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), |
693 | ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | 700 | ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), |
694 | ast::ExprKind::ArrayExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
695 | ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | 701 | ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), |
696 | } | 702 | } |
697 | } | 703 | } |
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 { | |||
227 | /// A tuple type. For example, `(i32, bool)`. | 227 | /// A tuple type. For example, `(i32, bool)`. |
228 | Tuple(Arc<[Ty]>), | 228 | Tuple(Arc<[Ty]>), |
229 | 229 | ||
230 | /// A array type. For example, `[i32]`. | ||
231 | Array(Arc<[Ty]>), | ||
232 | |||
230 | // The projection of an associated type. For example, | 233 | // The projection of an associated type. For example, |
231 | // `<T as Trait<..>>::N`.pub | 234 | // `<T as Trait<..>>::N`.pub |
232 | // Projection(ProjectionTy), | 235 | // Projection(ProjectionTy), |
@@ -414,6 +417,16 @@ impl fmt::Display for Ty { | |||
414 | .to_fmt(f) | 417 | .to_fmt(f) |
415 | } | 418 | } |
416 | } | 419 | } |
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 | } | ||
417 | Ty::FnPtr(sig) => { | 430 | Ty::FnPtr(sig) => { |
418 | join(sig.input.iter()) | 431 | join(sig.input.iter()) |
419 | .surround_with("fn(", ")") | 432 | .surround_with("fn(", ")") |
@@ -1101,7 +1114,15 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1101 | } | 1114 | } |
1102 | 1115 | ||
1103 | Ty::Tuple(Arc::from(ty_vec)) | 1116 | Ty::Tuple(Arc::from(ty_vec)) |
1104 | } | 1117 | }, |
1118 | Expr::Array { exprs } => { | ||
1119 | let mut ty_vec = Vec::with_capacity(exprs.len()); | ||
1120 | for arg in exprs.iter() { | ||
1121 | ty_vec.push(self.infer_expr(*arg, &Expectation::none())); | ||
1122 | } | ||
1123 | |||
1124 | Ty::Array(Arc::from(ty_vec)) | ||
1125 | }, | ||
1105 | Expr::Literal(lit) => match lit { | 1126 | Expr::Literal(lit) => match lit { |
1106 | Literal::Bool(..) => Ty::Bool, | 1127 | Literal::Bool(..) => Ty::Bool, |
1107 | Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), | 1128 | Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), |