diff options
author | Florian Diebold <[email protected]> | 2018-12-25 12:54:38 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2018-12-25 14:16:42 +0000 |
commit | 55c941cd9fb90c9340f01981e113aabd058b185b (patch) | |
tree | f07828a8eaabab7c20b2b4d3b98519956b981f17 /crates/ra_hir/src/ty.rs | |
parent | 07a72859650254d8ba326e2c9683ae52164cb3b5 (diff) |
Type field accesses
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index e2428a37f..f931f3c87 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -384,6 +384,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
384 | }) | 384 | }) |
385 | } | 385 | } |
386 | 386 | ||
387 | fn infer_expr_opt(&mut self, expr: Option<ast::Expr>) -> Cancelable<Ty> { | ||
388 | if let Some(e) = expr { | ||
389 | self.infer_expr(e) | ||
390 | } else { | ||
391 | Ok(Ty::Unknown) | ||
392 | } | ||
393 | } | ||
394 | |||
387 | fn infer_expr(&mut self, expr: ast::Expr) -> Cancelable<Ty> { | 395 | fn infer_expr(&mut self, expr: ast::Expr) -> Cancelable<Ty> { |
388 | let ty = match expr { | 396 | let ty = match expr { |
389 | ast::Expr::IfExpr(e) => { | 397 | ast::Expr::IfExpr(e) => { |
@@ -559,7 +567,29 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
559 | Ty::Unknown | 567 | Ty::Unknown |
560 | } | 568 | } |
561 | ast::Expr::IndexExpr(_e) => Ty::Unknown, | 569 | ast::Expr::IndexExpr(_e) => Ty::Unknown, |
562 | ast::Expr::FieldExpr(_e) => Ty::Unknown, | 570 | ast::Expr::FieldExpr(e) => { |
571 | let receiver_ty = self.infer_expr_opt(e.expr())?; | ||
572 | if let Some(nr) = e.name_ref() { | ||
573 | let text = nr.text(); | ||
574 | match receiver_ty { | ||
575 | Ty::Tuple(fields) => { | ||
576 | let i = text.parse::<usize>().ok(); | ||
577 | i.and_then(|i| fields.get(i).cloned()).unwrap_or(Ty::Unknown) | ||
578 | } | ||
579 | Ty::Adt { def_id, .. } => { | ||
580 | let field_ty = match def_id.resolve(self.db)? { | ||
581 | Def::Struct(s) => s.variant_data(self.db)?.get_field_ty(&text), | ||
582 | // TODO unions | ||
583 | _ => None, | ||
584 | }; | ||
585 | field_ty.unwrap_or(Ty::Unknown) | ||
586 | } | ||
587 | _ => Ty::Unknown, | ||
588 | } | ||
589 | } else { | ||
590 | Ty::Unknown | ||
591 | } | ||
592 | }, | ||
563 | ast::Expr::TryExpr(e) => { | 593 | ast::Expr::TryExpr(e) => { |
564 | let _inner_ty = if let Some(e) = e.expr() { | 594 | let _inner_ty = if let Some(e) = e.expr() { |
565 | self.infer_expr(e)? | 595 | self.infer_expr(e)? |