diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 117 | ||||
-rw-r--r-- | crates/ra_hir_def/src/generics.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/item_tree/lower.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path/lower.rs | 46 | ||||
-rw-r--r-- | crates/ra_hir_def/src/type_ref.rs | 31 |
6 files changed, 113 insertions, 95 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 827ced4ad..f5c37edb3 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` | 1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` |
2 | //! representation. | 2 | //! representation. |
3 | 3 | ||
4 | use std::{any::type_name, sync::Arc}; | ||
5 | |||
4 | use either::Either; | 6 | use either::Either; |
5 | use hir_expand::{ | 7 | use hir_expand::{ |
6 | hygiene::Hygiene, | 8 | hygiene::Hygiene, |
@@ -10,11 +12,12 @@ use hir_expand::{ | |||
10 | use ra_arena::Arena; | 12 | use ra_arena::Arena; |
11 | use ra_syntax::{ | 13 | use ra_syntax::{ |
12 | ast::{ | 14 | ast::{ |
13 | self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner, | 15 | self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner, |
14 | SlicePatComponents, | 16 | SlicePatComponents, |
15 | }, | 17 | }, |
16 | AstNode, AstPtr, | 18 | AstNode, AstPtr, |
17 | }; | 19 | }; |
20 | use rustc_hash::FxHashMap; | ||
18 | use test_utils::mark; | 21 | use test_utils::mark; |
19 | 22 | ||
20 | use crate::{ | 23 | use crate::{ |
@@ -35,9 +38,6 @@ use crate::{ | |||
35 | }; | 38 | }; |
36 | 39 | ||
37 | use super::{ExprSource, PatSource}; | 40 | use super::{ExprSource, PatSource}; |
38 | use ast::AstChildren; | ||
39 | use rustc_hash::FxHashMap; | ||
40 | use std::{any::type_name, sync::Arc}; | ||
41 | 41 | ||
42 | pub(crate) struct LowerCtx { | 42 | pub(crate) struct LowerCtx { |
43 | hygiene: Hygiene, | 43 | hygiene: Hygiene, |
@@ -224,9 +224,22 @@ impl ExprCollector<'_> { | |||
224 | self.alloc_expr(Expr::Unsafe { body }, syntax_ptr) | 224 | self.alloc_expr(Expr::Unsafe { body }, syntax_ptr) |
225 | } | 225 | } |
226 | // FIXME: we need to record these effects somewhere... | 226 | // FIXME: we need to record these effects somewhere... |
227 | ast::Effect::Async(_) | ast::Effect::Label(_) => { | 227 | ast::Effect::Label(label) => match e.block_expr() { |
228 | self.collect_block_opt(e.block_expr()) | 228 | Some(block) => { |
229 | } | 229 | let res = self.collect_block(block); |
230 | match &mut self.body.exprs[res] { | ||
231 | Expr::Block { label: block_label, .. } => { | ||
232 | *block_label = | ||
233 | label.lifetime_token().map(|t| Name::new_lifetime(&t)) | ||
234 | } | ||
235 | _ => unreachable!(), | ||
236 | } | ||
237 | res | ||
238 | } | ||
239 | None => self.missing_expr(), | ||
240 | }, | ||
241 | // FIXME: we need to record these effects somewhere... | ||
242 | ast::Effect::Async(_) => self.collect_block_opt(e.block_expr()), | ||
230 | }, | 243 | }, |
231 | ast::Expr::BlockExpr(e) => self.collect_block(e), | 244 | ast::Expr::BlockExpr(e) => self.collect_block(e), |
232 | ast::Expr::LoopExpr(e) => { | 245 | ast::Expr::LoopExpr(e) => { |
@@ -324,7 +337,7 @@ impl ExprCollector<'_> { | |||
324 | }; | 337 | }; |
325 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | 338 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
326 | let generic_args = | 339 | let generic_args = |
327 | e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); | 340 | e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); |
328 | self.alloc_expr( | 341 | self.alloc_expr( |
329 | Expr::MethodCall { receiver, method_name, args, generic_args }, | 342 | Expr::MethodCall { receiver, method_name, args, generic_args }, |
330 | syntax_ptr, | 343 | syntax_ptr, |
@@ -460,7 +473,7 @@ impl ExprCollector<'_> { | |||
460 | self.alloc_expr(Expr::Missing, syntax_ptr) | 473 | self.alloc_expr(Expr::Missing, syntax_ptr) |
461 | } | 474 | } |
462 | } | 475 | } |
463 | ast::Expr::LambdaExpr(e) => { | 476 | ast::Expr::ClosureExpr(e) => { |
464 | let mut args = Vec::new(); | 477 | let mut args = Vec::new(); |
465 | let mut arg_types = Vec::new(); | 478 | let mut arg_types = Vec::new(); |
466 | if let Some(pl) = e.param_list() { | 479 | if let Some(pl) = e.param_list() { |
@@ -483,7 +496,7 @@ impl ExprCollector<'_> { | |||
483 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) | 496 | self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) |
484 | } | 497 | } |
485 | ast::Expr::TupleExpr(e) => { | 498 | ast::Expr::TupleExpr(e) => { |
486 | let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); | 499 | let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect(); |
487 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) | 500 | self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) |
488 | } | 501 | } |
489 | ast::Expr::BoxExpr(e) => { | 502 | ast::Expr::BoxExpr(e) => { |
@@ -556,9 +569,6 @@ impl ExprCollector<'_> { | |||
556 | } | 569 | } |
557 | } | 570 | } |
558 | } | 571 | } |
559 | |||
560 | // FIXME implement HIR for these: | ||
561 | ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
562 | } | 572 | } |
563 | } | 573 | } |
564 | 574 | ||
@@ -601,26 +611,35 @@ impl ExprCollector<'_> { | |||
601 | self.collect_block_items(&block); | 611 | self.collect_block_items(&block); |
602 | let statements = block | 612 | let statements = block |
603 | .statements() | 613 | .statements() |
604 | .map(|s| match s { | 614 | .filter_map(|s| { |
605 | ast::Stmt::LetStmt(stmt) => { | 615 | let stmt = match s { |
606 | let pat = self.collect_pat_opt(stmt.pat()); | 616 | ast::Stmt::LetStmt(stmt) => { |
607 | let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); | 617 | let pat = self.collect_pat_opt(stmt.pat()); |
608 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | 618 | let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); |
609 | Statement::Let { pat, type_ref, initializer } | 619 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); |
610 | } | 620 | Statement::Let { pat, type_ref, initializer } |
611 | ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), | 621 | } |
622 | ast::Stmt::ExprStmt(stmt) => { | ||
623 | Statement::Expr(self.collect_expr_opt(stmt.expr())) | ||
624 | } | ||
625 | ast::Stmt::Item(_) => return None, | ||
626 | }; | ||
627 | Some(stmt) | ||
612 | }) | 628 | }) |
613 | .collect(); | 629 | .collect(); |
614 | let tail = block.expr().map(|e| self.collect_expr(e)); | 630 | let tail = block.expr().map(|e| self.collect_expr(e)); |
615 | let label = block.label().and_then(|l| l.lifetime_token()).map(|t| Name::new_lifetime(&t)); | 631 | self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr) |
616 | self.alloc_expr(Expr::Block { statements, tail, label }, syntax_node_ptr) | ||
617 | } | 632 | } |
618 | 633 | ||
619 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { | 634 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { |
620 | let container = ContainerId::DefWithBodyId(self.def); | 635 | let container = ContainerId::DefWithBodyId(self.def); |
621 | 636 | ||
622 | let items = block | 637 | let items = block |
623 | .items() | 638 | .statements() |
639 | .filter_map(|stmt| match stmt { | ||
640 | ast::Stmt::Item(it) => Some(it), | ||
641 | ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None, | ||
642 | }) | ||
624 | .filter_map(|item| { | 643 | .filter_map(|item| { |
625 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { | 644 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { |
626 | ast::Item::Fn(def) => { | 645 | ast::Item::Fn(def) => { |
@@ -704,7 +723,7 @@ impl ExprCollector<'_> { | |||
704 | 723 | ||
705 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { | 724 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { |
706 | let pattern = match &pat { | 725 | let pattern = match &pat { |
707 | ast::Pat::BindPat(bp) => { | 726 | ast::Pat::IdentPat(bp) => { |
708 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | 727 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
709 | let annotation = | 728 | let annotation = |
710 | BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some()); | 729 | BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some()); |
@@ -743,7 +762,7 @@ impl ExprCollector<'_> { | |||
743 | } | 762 | } |
744 | ast::Pat::TupleStructPat(p) => { | 763 | ast::Pat::TupleStructPat(p) => { |
745 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 764 | let path = p.path().and_then(|path| self.expander.parse_path(path)); |
746 | let (args, ellipsis) = self.collect_tuple_pat(p.args()); | 765 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); |
747 | Pat::TupleStruct { path, args, ellipsis } | 766 | Pat::TupleStruct { path, args, ellipsis } |
748 | } | 767 | } |
749 | ast::Pat::RefPat(p) => { | 768 | ast::Pat::RefPat(p) => { |
@@ -761,40 +780,36 @@ impl ExprCollector<'_> { | |||
761 | } | 780 | } |
762 | ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()), | 781 | ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()), |
763 | ast::Pat::TuplePat(p) => { | 782 | ast::Pat::TuplePat(p) => { |
764 | let (args, ellipsis) = self.collect_tuple_pat(p.args()); | 783 | let (args, ellipsis) = self.collect_tuple_pat(p.fields()); |
765 | Pat::Tuple { args, ellipsis } | 784 | Pat::Tuple { args, ellipsis } |
766 | } | 785 | } |
767 | ast::Pat::PlaceholderPat(_) => Pat::Wild, | 786 | ast::Pat::WildcardPat(_) => Pat::Wild, |
768 | ast::Pat::RecordPat(p) => { | 787 | ast::Pat::RecordPat(p) => { |
769 | let path = p.path().and_then(|path| self.expander.parse_path(path)); | 788 | let path = p.path().and_then(|path| self.expander.parse_path(path)); |
770 | let record_field_pat_list = | 789 | let args: Vec<_> = p |
771 | p.record_field_pat_list().expect("every struct should have a field list"); | 790 | .record_pat_field_list() |
772 | let mut fields: Vec<_> = record_field_pat_list | 791 | .expect("every struct should have a field list") |
773 | .bind_pats() | 792 | .fields() |
774 | .filter_map(|bind_pat| { | 793 | .filter_map(|f| { |
775 | let ast_pat = | 794 | let ast_pat = f.pat()?; |
776 | ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat"); | ||
777 | let pat = self.collect_pat(ast_pat); | 795 | let pat = self.collect_pat(ast_pat); |
778 | let name = bind_pat.name()?.as_name(); | 796 | let name = f.field_name()?.as_name(); |
779 | Some(RecordFieldPat { name, pat }) | 797 | Some(RecordFieldPat { name, pat }) |
780 | }) | 798 | }) |
781 | .collect(); | 799 | .collect(); |
782 | let iter = record_field_pat_list.record_field_pats().filter_map(|f| { | ||
783 | let ast_pat = f.pat()?; | ||
784 | let pat = self.collect_pat(ast_pat); | ||
785 | let name = f.field_name()?.as_name(); | ||
786 | Some(RecordFieldPat { name, pat }) | ||
787 | }); | ||
788 | fields.extend(iter); | ||
789 | 800 | ||
790 | let ellipsis = record_field_pat_list.dotdot_token().is_some(); | 801 | let ellipsis = p |
802 | .record_pat_field_list() | ||
803 | .expect("every struct should have a field list") | ||
804 | .dotdot_token() | ||
805 | .is_some(); | ||
791 | 806 | ||
792 | Pat::Record { path, args: fields, ellipsis } | 807 | Pat::Record { path, args, ellipsis } |
793 | } | 808 | } |
794 | ast::Pat::SlicePat(p) => { | 809 | ast::Pat::SlicePat(p) => { |
795 | let SlicePatComponents { prefix, slice, suffix } = p.components(); | 810 | let SlicePatComponents { prefix, slice, suffix } = p.components(); |
796 | 811 | ||
797 | // FIXME properly handle `DotDotPat` | 812 | // FIXME properly handle `RestPat` |
798 | Pat::Slice { | 813 | Pat::Slice { |
799 | prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(), | 814 | prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(), |
800 | slice: slice.map(|p| self.collect_pat(p)), | 815 | slice: slice.map(|p| self.collect_pat(p)), |
@@ -811,10 +826,10 @@ impl ExprCollector<'_> { | |||
811 | Pat::Missing | 826 | Pat::Missing |
812 | } | 827 | } |
813 | } | 828 | } |
814 | ast::Pat::DotDotPat(_) => { | 829 | ast::Pat::RestPat(_) => { |
815 | // `DotDotPat` requires special handling and should not be mapped | 830 | // `RestPat` requires special handling and should not be mapped |
816 | // to a Pat. Here we are using `Pat::Missing` as a fallback for | 831 | // to a Pat. Here we are using `Pat::Missing` as a fallback for |
817 | // when `DotDotPat` is mapped to `Pat`, which can easily happen | 832 | // when `RestPat` is mapped to `Pat`, which can easily happen |
818 | // when the source code being analyzed has a malformed pattern | 833 | // when the source code being analyzed has a malformed pattern |
819 | // which includes `..` in a place where it isn't valid. | 834 | // which includes `..` in a place where it isn't valid. |
820 | 835 | ||
@@ -838,10 +853,10 @@ impl ExprCollector<'_> { | |||
838 | fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) { | 853 | fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) { |
839 | // Find the location of the `..`, if there is one. Note that we do not | 854 | // Find the location of the `..`, if there is one. Note that we do not |
840 | // consider the possiblity of there being multiple `..` here. | 855 | // consider the possiblity of there being multiple `..` here. |
841 | let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::DotDotPat(_))); | 856 | let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_))); |
842 | // We want to skip the `..` pattern here, since we account for it above. | 857 | // We want to skip the `..` pattern here, since we account for it above. |
843 | let args = args | 858 | let args = args |
844 | .filter(|p| !matches!(p, ast::Pat::DotDotPat(_))) | 859 | .filter(|p| !matches!(p, ast::Pat::RestPat(_))) |
845 | .map(|p| self.collect_pat(p)) | 860 | .map(|p| self.collect_pat(p)) |
846 | .collect(); | 861 | .collect(); |
847 | 862 | ||
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 8ea61fcf2..699ba9c92 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs | |||
@@ -253,7 +253,7 @@ impl GenericParams { | |||
253 | 253 | ||
254 | fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) { | 254 | fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) { |
255 | for pred in where_clause.predicates() { | 255 | for pred in where_clause.predicates() { |
256 | let type_ref = match pred.type_ref() { | 256 | let type_ref = match pred.ty() { |
257 | Some(type_ref) => type_ref, | 257 | Some(type_ref) => type_ref, |
258 | None => continue, | 258 | None => continue, |
259 | }; | 259 | }; |
@@ -270,7 +270,7 @@ impl GenericParams { | |||
270 | bound: ast::TypeBound, | 270 | bound: ast::TypeBound, |
271 | type_ref: TypeRef, | 271 | type_ref: TypeRef, |
272 | ) { | 272 | ) { |
273 | if bound.question_token().is_some() { | 273 | if bound.question_mark_token().is_some() { |
274 | // FIXME: remove this bound | 274 | // FIXME: remove this bound |
275 | return; | 275 | return; |
276 | } | 276 | } |
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index feb31579e..450ef8798 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs | |||
@@ -448,8 +448,8 @@ impl Ctx { | |||
448 | fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> { | 448 | fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> { |
449 | let generic_params = | 449 | let generic_params = |
450 | self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def); | 450 | self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def); |
451 | let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); | 451 | let target_trait = impl_def.trait_().map(|tr| self.lower_type_ref(&tr)); |
452 | let target_type = self.lower_type_ref(&impl_def.target_type()?); | 452 | let target_type = self.lower_type_ref(&impl_def.self_ty()?); |
453 | let is_negative = impl_def.excl_token().is_some(); | 453 | let is_negative = impl_def.excl_token().is_some(); |
454 | 454 | ||
455 | // We cannot use `assoc_items()` here as that does not include macro calls. | 455 | // We cannot use `assoc_items()` here as that does not include macro calls. |
@@ -648,10 +648,10 @@ impl Ctx { | |||
648 | self.data().vis.alloc(vis) | 648 | self.data().vis.alloc(vis) |
649 | } | 649 | } |
650 | 650 | ||
651 | fn lower_type_ref(&self, type_ref: &ast::TypeRef) -> TypeRef { | 651 | fn lower_type_ref(&self, type_ref: &ast::Type) -> TypeRef { |
652 | TypeRef::from_ast(&self.body_ctx, type_ref.clone()) | 652 | TypeRef::from_ast(&self.body_ctx, type_ref.clone()) |
653 | } | 653 | } |
654 | fn lower_type_ref_opt(&self, type_ref: Option<ast::TypeRef>) -> TypeRef { | 654 | fn lower_type_ref_opt(&self, type_ref: Option<ast::Type>) -> TypeRef { |
655 | type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error) | 655 | type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error) |
656 | } | 656 | } |
657 | 657 | ||
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 68b9f89c3..cc1726e9e 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -258,7 +258,7 @@ impl<'a> PathSegments<'a> { | |||
258 | } | 258 | } |
259 | 259 | ||
260 | impl GenericArgs { | 260 | impl GenericArgs { |
261 | pub(crate) fn from_ast(lower_ctx: &LowerCtx, node: ast::TypeArgList) -> Option<GenericArgs> { | 261 | pub(crate) fn from_ast(lower_ctx: &LowerCtx, node: ast::GenericArgList) -> Option<GenericArgs> { |
262 | lower::lower_generic_args(lower_ctx, node) | 262 | lower::lower_generic_args(lower_ctx, node) |
263 | } | 263 | } |
264 | 264 | ||
diff --git a/crates/ra_hir_def/src/path/lower.rs b/crates/ra_hir_def/src/path/lower.rs index 07d17916a..d09fc66e4 100644 --- a/crates/ra_hir_def/src/path/lower.rs +++ b/crates/ra_hir_def/src/path/lower.rs | |||
@@ -41,7 +41,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
41 | match hygiene.name_ref_to_name(name_ref) { | 41 | match hygiene.name_ref_to_name(name_ref) { |
42 | Either::Left(name) => { | 42 | Either::Left(name) => { |
43 | let args = segment | 43 | let args = segment |
44 | .type_arg_list() | 44 | .generic_arg_list() |
45 | .and_then(|it| lower_generic_args(&ctx, it)) | 45 | .and_then(|it| lower_generic_args(&ctx, it)) |
46 | .or_else(|| { | 46 | .or_else(|| { |
47 | lower_generic_args_from_fn_path( | 47 | lower_generic_args_from_fn_path( |
@@ -148,33 +148,37 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> | |||
148 | 148 | ||
149 | pub(super) fn lower_generic_args( | 149 | pub(super) fn lower_generic_args( |
150 | lower_ctx: &LowerCtx, | 150 | lower_ctx: &LowerCtx, |
151 | node: ast::TypeArgList, | 151 | node: ast::GenericArgList, |
152 | ) -> Option<GenericArgs> { | 152 | ) -> Option<GenericArgs> { |
153 | let mut args = Vec::new(); | 153 | let mut args = Vec::new(); |
154 | for type_arg in node.type_args() { | ||
155 | let type_ref = TypeRef::from_ast_opt(lower_ctx, type_arg.type_ref()); | ||
156 | args.push(GenericArg::Type(type_ref)); | ||
157 | } | ||
158 | // lifetimes ignored for now | ||
159 | let mut bindings = Vec::new(); | 154 | let mut bindings = Vec::new(); |
160 | for assoc_type_arg in node.assoc_type_args() { | 155 | for generic_arg in node.generic_args() { |
161 | let assoc_type_arg: ast::AssocTypeArg = assoc_type_arg; | 156 | match generic_arg { |
162 | if let Some(name_ref) = assoc_type_arg.name_ref() { | 157 | ast::GenericArg::TypeArg(type_arg) => { |
163 | let name = name_ref.as_name(); | 158 | let type_ref = TypeRef::from_ast_opt(lower_ctx, type_arg.ty()); |
164 | let type_ref = assoc_type_arg.type_ref().map(|it| TypeRef::from_ast(lower_ctx, it)); | 159 | args.push(GenericArg::Type(type_ref)); |
165 | let bounds = if let Some(l) = assoc_type_arg.type_bound_list() { | 160 | } |
166 | l.bounds().map(|it| TypeBound::from_ast(lower_ctx, it)).collect() | 161 | ast::GenericArg::AssocTypeArg(assoc_type_arg) => { |
167 | } else { | 162 | if let Some(name_ref) = assoc_type_arg.name_ref() { |
168 | Vec::new() | 163 | let name = name_ref.as_name(); |
169 | }; | 164 | let type_ref = assoc_type_arg.ty().map(|it| TypeRef::from_ast(lower_ctx, it)); |
170 | bindings.push(AssociatedTypeBinding { name, type_ref, bounds }); | 165 | let bounds = if let Some(l) = assoc_type_arg.type_bound_list() { |
166 | l.bounds().map(|it| TypeBound::from_ast(lower_ctx, it)).collect() | ||
167 | } else { | ||
168 | Vec::new() | ||
169 | }; | ||
170 | bindings.push(AssociatedTypeBinding { name, type_ref, bounds }); | ||
171 | } | ||
172 | } | ||
173 | // Lifetimes and constants are ignored for now. | ||
174 | ast::GenericArg::LifetimeArg(_) | ast::GenericArg::ConstArg(_) => (), | ||
171 | } | 175 | } |
172 | } | 176 | } |
177 | |||
173 | if args.is_empty() && bindings.is_empty() { | 178 | if args.is_empty() && bindings.is_empty() { |
174 | None | 179 | return None; |
175 | } else { | ||
176 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
177 | } | 180 | } |
181 | Some(GenericArgs { args, has_self_type: false, bindings }) | ||
178 | } | 182 | } |
179 | 183 | ||
180 | /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y) | 184 | /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y) |
diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs index a5dc10eac..6f7884ffe 100644 --- a/crates/ra_hir_def/src/type_ref.rs +++ b/crates/ra_hir_def/src/type_ref.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | //! HIR for references to types. Paths in these are not yet resolved. They can | 1 | //! HIR for references to types. Paths in these are not yet resolved. They can |
2 | //! be directly created from an ast::TypeRef, without further queries. | 2 | //! be directly created from an ast::TypeRef, without further queries. |
3 | |||
4 | use ra_syntax::ast::{self}; | 3 | use ra_syntax::ast::{self}; |
5 | 4 | ||
6 | use crate::{body::LowerCtx, path::Path}; | 5 | use crate::{body::LowerCtx, path::Path}; |
@@ -80,14 +79,14 @@ pub enum TypeBound { | |||
80 | 79 | ||
81 | impl TypeRef { | 80 | impl TypeRef { |
82 | /// Converts an `ast::TypeRef` to a `hir::TypeRef`. | 81 | /// Converts an `ast::TypeRef` to a `hir::TypeRef`. |
83 | pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeRef) -> Self { | 82 | pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self { |
84 | match node { | 83 | match node { |
85 | ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), | 84 | ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), |
86 | ast::TypeRef::TupleType(inner) => { | 85 | ast::Type::TupleType(inner) => { |
87 | TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect()) | 86 | TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect()) |
88 | } | 87 | } |
89 | ast::TypeRef::NeverType(..) => TypeRef::Never, | 88 | ast::Type::NeverType(..) => TypeRef::Never, |
90 | ast::TypeRef::PathType(inner) => { | 89 | ast::Type::PathType(inner) => { |
91 | // FIXME: Use `Path::from_src` | 90 | // FIXME: Use `Path::from_src` |
92 | inner | 91 | inner |
93 | .path() | 92 | .path() |
@@ -95,24 +94,24 @@ impl TypeRef { | |||
95 | .map(TypeRef::Path) | 94 | .map(TypeRef::Path) |
96 | .unwrap_or(TypeRef::Error) | 95 | .unwrap_or(TypeRef::Error) |
97 | } | 96 | } |
98 | ast::TypeRef::PointerType(inner) => { | 97 | ast::Type::PtrType(inner) => { |
99 | let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); | 98 | let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); |
100 | let mutability = Mutability::from_mutable(inner.mut_token().is_some()); | 99 | let mutability = Mutability::from_mutable(inner.mut_token().is_some()); |
101 | TypeRef::RawPtr(Box::new(inner_ty), mutability) | 100 | TypeRef::RawPtr(Box::new(inner_ty), mutability) |
102 | } | 101 | } |
103 | ast::TypeRef::ArrayType(inner) => { | 102 | ast::Type::ArrayType(inner) => { |
104 | TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty()))) | 103 | TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty()))) |
105 | } | 104 | } |
106 | ast::TypeRef::SliceType(inner) => { | 105 | ast::Type::SliceType(inner) => { |
107 | TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty()))) | 106 | TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty()))) |
108 | } | 107 | } |
109 | ast::TypeRef::ReferenceType(inner) => { | 108 | ast::Type::RefType(inner) => { |
110 | let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); | 109 | let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); |
111 | let mutability = Mutability::from_mutable(inner.mut_token().is_some()); | 110 | let mutability = Mutability::from_mutable(inner.mut_token().is_some()); |
112 | TypeRef::Reference(Box::new(inner_ty), mutability) | 111 | TypeRef::Reference(Box::new(inner_ty), mutability) |
113 | } | 112 | } |
114 | ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder, | 113 | ast::Type::InferType(_inner) => TypeRef::Placeholder, |
115 | ast::TypeRef::FnPointerType(inner) => { | 114 | ast::Type::FnPtrType(inner) => { |
116 | let ret_ty = inner | 115 | let ret_ty = inner |
117 | .ret_type() | 116 | .ret_type() |
118 | .and_then(|rt| rt.ty()) | 117 | .and_then(|rt| rt.ty()) |
@@ -132,17 +131,17 @@ impl TypeRef { | |||
132 | TypeRef::Fn(params, is_varargs) | 131 | TypeRef::Fn(params, is_varargs) |
133 | } | 132 | } |
134 | // for types are close enough for our purposes to the inner type for now... | 133 | // for types are close enough for our purposes to the inner type for now... |
135 | ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), | 134 | ast::Type::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()), |
136 | ast::TypeRef::ImplTraitType(inner) => { | 135 | ast::Type::ImplTraitType(inner) => { |
137 | TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) | 136 | TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) |
138 | } | 137 | } |
139 | ast::TypeRef::DynTraitType(inner) => { | 138 | ast::Type::DynTraitType(inner) => { |
140 | TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) | 139 | TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) |
141 | } | 140 | } |
142 | } | 141 | } |
143 | } | 142 | } |
144 | 143 | ||
145 | pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::TypeRef>) -> Self { | 144 | pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self { |
146 | if let Some(node) = node { | 145 | if let Some(node) = node { |
147 | TypeRef::from_ast(ctx, node) | 146 | TypeRef::from_ast(ctx, node) |
148 | } else { | 147 | } else { |