diff options
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 3a97d97ce..70af3f119 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -550,7 +550,7 @@ where | |||
550 | self.exprs.alloc(block) | 550 | self.exprs.alloc(block) |
551 | } | 551 | } |
552 | 552 | ||
553 | fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { | 553 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { |
554 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); | 554 | let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); |
555 | match expr.kind() { | 555 | match expr.kind() { |
556 | ast::ExprKind::IfExpr(e) => { | 556 | ast::ExprKind::IfExpr(e) => { |
@@ -565,7 +565,8 @@ where | |||
565 | .map(|b| match b { | 565 | .map(|b| match b { |
566 | ast::ElseBranch::Block(it) => self.collect_block(it), | 566 | ast::ElseBranch::Block(it) => self.collect_block(it), |
567 | ast::ElseBranch::IfExpr(elif) => { | 567 | ast::ElseBranch::IfExpr(elif) => { |
568 | let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); | 568 | let expr: ast::Expr = |
569 | ast::Expr::cast(elif.syntax().clone()).unwrap(); | ||
569 | self.collect_expr(expr) | 570 | self.collect_expr(expr) |
570 | } | 571 | } |
571 | }) | 572 | }) |
@@ -582,7 +583,7 @@ where | |||
582 | let else_branch = e.else_branch().map(|b| match b { | 583 | let else_branch = e.else_branch().map(|b| match b { |
583 | ast::ElseBranch::Block(it) => self.collect_block(it), | 584 | ast::ElseBranch::Block(it) => self.collect_block(it), |
584 | ast::ElseBranch::IfExpr(elif) => { | 585 | ast::ElseBranch::IfExpr(elif) => { |
585 | let expr: &ast::Expr = ast::Expr::cast(elif.syntax()).unwrap(); | 586 | let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap(); |
586 | self.collect_expr(expr) | 587 | self.collect_expr(expr) |
587 | } | 588 | } |
588 | }); | 589 | }); |
@@ -689,7 +690,7 @@ where | |||
689 | let struct_lit = if let Some(nfl) = e.named_field_list() { | 690 | let struct_lit = if let Some(nfl) = e.named_field_list() { |
690 | let fields = nfl | 691 | let fields = nfl |
691 | .fields() | 692 | .fields() |
692 | .inspect(|field| field_ptrs.push(AstPtr::new(*field))) | 693 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) |
693 | .map(|field| StructLitField { | 694 | .map(|field| StructLitField { |
694 | name: field | 695 | name: field |
695 | .name_ref() | 696 | .name_ref() |
@@ -699,7 +700,7 @@ where | |||
699 | self.collect_expr(e) | 700 | self.collect_expr(e) |
700 | } else if let Some(nr) = field.name_ref() { | 701 | } else if let Some(nr) = field.name_ref() { |
701 | // field shorthand | 702 | // field shorthand |
702 | let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr))); | 703 | let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(&nr))); |
703 | self.source_map | 704 | self.source_map |
704 | .expr_map | 705 | .expr_map |
705 | .insert(SyntaxNodePtr::new(nr.syntax()), id); | 706 | .insert(SyntaxNodePtr::new(nr.syntax()), id); |
@@ -837,7 +838,7 @@ where | |||
837 | let ast_id = self | 838 | let ast_id = self |
838 | .db | 839 | .db |
839 | .ast_id_map(self.current_file_id) | 840 | .ast_id_map(self.current_file_id) |
840 | .ast_id(e) | 841 | .ast_id(&e) |
841 | .with_file_id(self.current_file_id); | 842 | .with_file_id(self.current_file_id); |
842 | 843 | ||
843 | if let Some(path) = e.path().and_then(Path::from_ast) { | 844 | if let Some(path) = e.path().and_then(Path::from_ast) { |
@@ -845,11 +846,11 @@ where | |||
845 | let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db); | 846 | let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db); |
846 | let file_id = call_id.as_file(MacroFileKind::Expr); | 847 | let file_id = call_id.as_file(MacroFileKind::Expr); |
847 | if let Some(node) = self.db.parse_or_expand(file_id) { | 848 | if let Some(node) = self.db.parse_or_expand(file_id) { |
848 | if let Some(expr) = ast::Expr::cast(&*node) { | 849 | if let Some(expr) = ast::Expr::cast(node) { |
849 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); | 850 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); |
850 | let old_file_id = | 851 | let old_file_id = |
851 | std::mem::replace(&mut self.current_file_id, file_id); | 852 | std::mem::replace(&mut self.current_file_id, file_id); |
852 | let id = self.collect_expr(&expr); | 853 | let id = self.collect_expr(expr); |
853 | self.current_file_id = old_file_id; | 854 | self.current_file_id = old_file_id; |
854 | return id; | 855 | return id; |
855 | } | 856 | } |
@@ -863,7 +864,7 @@ where | |||
863 | } | 864 | } |
864 | } | 865 | } |
865 | 866 | ||
866 | fn collect_expr_opt(&mut self, expr: Option<&ast::Expr>) -> ExprId { | 867 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { |
867 | if let Some(expr) = expr { | 868 | if let Some(expr) = expr { |
868 | self.collect_expr(expr) | 869 | self.collect_expr(expr) |
869 | } else { | 870 | } else { |
@@ -871,7 +872,7 @@ where | |||
871 | } | 872 | } |
872 | } | 873 | } |
873 | 874 | ||
874 | fn collect_block(&mut self, block: &ast::Block) -> ExprId { | 875 | fn collect_block(&mut self, block: ast::Block) -> ExprId { |
875 | let statements = block | 876 | let statements = block |
876 | .statements() | 877 | .statements() |
877 | .map(|s| match s.kind() { | 878 | .map(|s| match s.kind() { |
@@ -890,7 +891,7 @@ where | |||
890 | self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax())) | 891 | self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax())) |
891 | } | 892 | } |
892 | 893 | ||
893 | fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId { | 894 | fn collect_block_opt(&mut self, block: Option<ast::Block>) -> ExprId { |
894 | if let Some(block) = block { | 895 | if let Some(block) = block { |
895 | self.collect_block(block) | 896 | self.collect_block(block) |
896 | } else { | 897 | } else { |
@@ -898,7 +899,7 @@ where | |||
898 | } | 899 | } |
899 | } | 900 | } |
900 | 901 | ||
901 | fn collect_pat(&mut self, pat: &ast::Pat) -> PatId { | 902 | fn collect_pat(&mut self, pat: ast::Pat) -> PatId { |
902 | let pattern = match pat.kind() { | 903 | let pattern = match pat.kind() { |
903 | ast::PatKind::BindPat(bp) => { | 904 | ast::PatKind::BindPat(bp) => { |
904 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); | 905 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
@@ -932,7 +933,8 @@ where | |||
932 | let mut fields: Vec<_> = field_pat_list | 933 | let mut fields: Vec<_> = field_pat_list |
933 | .bind_pats() | 934 | .bind_pats() |
934 | .filter_map(|bind_pat| { | 935 | .filter_map(|bind_pat| { |
935 | let ast_pat = ast::Pat::cast(bind_pat.syntax()).expect("bind pat is a pat"); | 936 | let ast_pat = |
937 | ast::Pat::cast(bind_pat.syntax().clone()).expect("bind pat is a pat"); | ||
936 | let pat = self.collect_pat(ast_pat); | 938 | let pat = self.collect_pat(ast_pat); |
937 | let name = bind_pat.name()?.as_name(); | 939 | let name = bind_pat.name()?.as_name(); |
938 | Some(FieldPat { name, pat }) | 940 | Some(FieldPat { name, pat }) |
@@ -953,11 +955,11 @@ where | |||
953 | ast::PatKind::LiteralPat(_) => Pat::Missing, | 955 | ast::PatKind::LiteralPat(_) => Pat::Missing, |
954 | ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, | 956 | ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, |
955 | }; | 957 | }; |
956 | let ptr = AstPtr::new(pat); | 958 | let ptr = AstPtr::new(&pat); |
957 | self.alloc_pat(pattern, Either::A(ptr)) | 959 | self.alloc_pat(pattern, Either::A(ptr)) |
958 | } | 960 | } |
959 | 961 | ||
960 | fn collect_pat_opt(&mut self, pat: Option<&ast::Pat>) -> PatId { | 962 | fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { |
961 | if let Some(pat) = pat { | 963 | if let Some(pat) = pat { |
962 | self.collect_pat(pat) | 964 | self.collect_pat(pat) |
963 | } else { | 965 | } else { |
@@ -965,20 +967,20 @@ where | |||
965 | } | 967 | } |
966 | } | 968 | } |
967 | 969 | ||
968 | fn collect_const_body(&mut self, node: &ast::ConstDef) { | 970 | fn collect_const_body(&mut self, node: ast::ConstDef) { |
969 | let body = self.collect_expr_opt(node.body()); | 971 | let body = self.collect_expr_opt(node.body()); |
970 | self.body_expr = Some(body); | 972 | self.body_expr = Some(body); |
971 | } | 973 | } |
972 | 974 | ||
973 | fn collect_static_body(&mut self, node: &ast::StaticDef) { | 975 | fn collect_static_body(&mut self, node: ast::StaticDef) { |
974 | let body = self.collect_expr_opt(node.body()); | 976 | let body = self.collect_expr_opt(node.body()); |
975 | self.body_expr = Some(body); | 977 | self.body_expr = Some(body); |
976 | } | 978 | } |
977 | 979 | ||
978 | fn collect_fn_body(&mut self, node: &ast::FnDef) { | 980 | fn collect_fn_body(&mut self, node: ast::FnDef) { |
979 | if let Some(param_list) = node.param_list() { | 981 | if let Some(param_list) = node.param_list() { |
980 | if let Some(self_param) = param_list.self_param() { | 982 | if let Some(self_param) = param_list.self_param() { |
981 | let ptr = AstPtr::new(self_param); | 983 | let ptr = AstPtr::new(&self_param); |
982 | let param_pat = self.alloc_pat( | 984 | let param_pat = self.alloc_pat( |
983 | Pat::Bind { | 985 | Pat::Bind { |
984 | name: SELF_PARAM, | 986 | name: SELF_PARAM, |
@@ -1027,17 +1029,17 @@ pub(crate) fn body_with_source_map_query( | |||
1027 | DefWithBody::Const(ref c) => { | 1029 | DefWithBody::Const(ref c) => { |
1028 | let src = c.source(db); | 1030 | let src = c.source(db); |
1029 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | 1031 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); |
1030 | collector.collect_const_body(&src.ast) | 1032 | collector.collect_const_body(src.ast) |
1031 | } | 1033 | } |
1032 | DefWithBody::Function(ref f) => { | 1034 | DefWithBody::Function(ref f) => { |
1033 | let src = f.source(db); | 1035 | let src = f.source(db); |
1034 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | 1036 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); |
1035 | collector.collect_fn_body(&src.ast) | 1037 | collector.collect_fn_body(src.ast) |
1036 | } | 1038 | } |
1037 | DefWithBody::Static(ref s) => { | 1039 | DefWithBody::Static(ref s) => { |
1038 | let src = s.source(db); | 1040 | let src = s.source(db); |
1039 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); | 1041 | collector = ExprCollector::new(def, src.file_id, def.resolver(db), db); |
1040 | collector.collect_static_body(&src.ast) | 1042 | collector.collect_static_body(src.ast) |
1041 | } | 1043 | } |
1042 | } | 1044 | } |
1043 | 1045 | ||