diff options
author | Aleksey Kladov <[email protected]> | 2019-02-08 11:49:43 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-08 11:49:43 +0000 |
commit | 12e3b4c70b5ef23b2fdfc197296d483680e125f9 (patch) | |
tree | 71baa0e0a62f9f6b61450501c5f821f67badf9e4 /crates/ra_hir/src/expr.rs | |
parent | 5cb1d41a30d25cbe136402644bf5434dd667f1e5 (diff) |
reformat the world
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 150 |
1 files changed, 27 insertions, 123 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 6826e966b..4e61d87ff 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -121,9 +121,7 @@ impl BodySyntaxMapping { | |||
121 | } | 121 | } |
122 | 122 | ||
123 | pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { | 123 | pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { |
124 | self.expr_syntax_mapping | 124 | self.expr_syntax_mapping.get(&SyntaxNodePtr::new(node.syntax())).cloned() |
125 | .get(&SyntaxNodePtr::new(node.syntax())) | ||
126 | .cloned() | ||
127 | } | 125 | } |
128 | 126 | ||
129 | pub fn pat_syntax(&self, pat: PatId) -> Option<SyntaxNodePtr> { | 127 | pub fn pat_syntax(&self, pat: PatId) -> Option<SyntaxNodePtr> { |
@@ -135,9 +133,7 @@ impl BodySyntaxMapping { | |||
135 | } | 133 | } |
136 | 134 | ||
137 | pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { | 135 | pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { |
138 | self.pat_syntax_mapping | 136 | self.pat_syntax_mapping.get(&SyntaxNodePtr::new(node.syntax())).cloned() |
139 | .get(&SyntaxNodePtr::new(node.syntax())) | ||
140 | .cloned() | ||
141 | } | 137 | } |
142 | 138 | ||
143 | pub fn body(&self) -> &Arc<Body> { | 139 | pub fn body(&self) -> &Arc<Body> { |
@@ -262,11 +258,7 @@ pub struct StructLitField { | |||
262 | 258 | ||
263 | #[derive(Debug, Clone, Eq, PartialEq)] | 259 | #[derive(Debug, Clone, Eq, PartialEq)] |
264 | pub enum Statement { | 260 | pub enum Statement { |
265 | Let { | 261 | Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> }, |
266 | pat: PatId, | ||
267 | type_ref: Option<TypeRef>, | ||
268 | initializer: Option<ExprId>, | ||
269 | }, | ||
270 | Expr(ExprId), | 262 | Expr(ExprId), |
271 | } | 263 | } |
272 | 264 | ||
@@ -275,11 +267,7 @@ impl Expr { | |||
275 | match self { | 267 | match self { |
276 | Expr::Missing => {} | 268 | Expr::Missing => {} |
277 | Expr::Path(_) => {} | 269 | Expr::Path(_) => {} |
278 | Expr::If { | 270 | Expr::If { condition, then_branch, else_branch } => { |
279 | condition, | ||
280 | then_branch, | ||
281 | else_branch, | ||
282 | } => { | ||
283 | f(*condition); | 271 | f(*condition); |
284 | f(*then_branch); | 272 | f(*then_branch); |
285 | if let Some(else_branch) = else_branch { | 273 | if let Some(else_branch) = else_branch { |
@@ -457,11 +445,7 @@ impl Pat { | |||
457 | args.iter().map(|pat| *pat).for_each(f); | 445 | args.iter().map(|pat| *pat).for_each(f); |
458 | } | 446 | } |
459 | Pat::Ref { pat, .. } => f(*pat), | 447 | Pat::Ref { pat, .. } => f(*pat), |
460 | Pat::Slice { | 448 | Pat::Slice { prefix, rest, suffix } => { |
461 | prefix, | ||
462 | rest, | ||
463 | suffix, | ||
464 | } => { | ||
465 | let total_iter = prefix.iter().chain(rest.iter()).chain(suffix.iter()); | 449 | let total_iter = prefix.iter().chain(rest.iter()).chain(suffix.iter()); |
466 | total_iter.map(|pat| *pat).for_each(f); | 450 | total_iter.map(|pat| *pat).for_each(f); |
467 | } | 451 | } |
@@ -520,10 +504,7 @@ impl ExprCollector { | |||
520 | } | 504 | } |
521 | 505 | ||
522 | fn empty_block(&mut self) -> ExprId { | 506 | fn empty_block(&mut self) -> ExprId { |
523 | let block = Expr::Block { | 507 | let block = Expr::Block { statements: Vec::new(), tail: None }; |
524 | statements: Vec::new(), | ||
525 | tail: None, | ||
526 | }; | ||
527 | self.exprs.alloc(block) | 508 | self.exprs.alloc(block) |
528 | } | 509 | } |
529 | 510 | ||
@@ -549,24 +530,10 @@ impl ExprCollector { | |||
549 | .unwrap_or_else(|| self.empty_block()); | 530 | .unwrap_or_else(|| self.empty_block()); |
550 | let placeholder_pat = self.pats.alloc(Pat::Missing); | 531 | let placeholder_pat = self.pats.alloc(Pat::Missing); |
551 | let arms = vec![ | 532 | let arms = vec![ |
552 | MatchArm { | 533 | MatchArm { pats: vec![pat], expr: then_branch, guard: None }, |
553 | pats: vec![pat], | 534 | MatchArm { pats: vec![placeholder_pat], expr: else_branch, guard: None }, |
554 | expr: then_branch, | ||
555 | guard: None, | ||
556 | }, | ||
557 | MatchArm { | ||
558 | pats: vec![placeholder_pat], | ||
559 | expr: else_branch, | ||
560 | guard: None, | ||
561 | }, | ||
562 | ]; | 535 | ]; |
563 | self.alloc_expr( | 536 | self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr) |
564 | Expr::Match { | ||
565 | expr: match_expr, | ||
566 | arms, | ||
567 | }, | ||
568 | syntax_ptr, | ||
569 | ) | ||
570 | } else { | 537 | } else { |
571 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); | 538 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); |
572 | let then_branch = self.collect_block_opt(e.then_branch()); | 539 | let then_branch = self.collect_block_opt(e.then_branch()); |
@@ -577,14 +544,7 @@ impl ExprCollector { | |||
577 | self.collect_expr(expr) | 544 | self.collect_expr(expr) |
578 | } | 545 | } |
579 | }); | 546 | }); |
580 | self.alloc_expr( | 547 | self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) |
581 | Expr::If { | ||
582 | condition, | ||
583 | then_branch, | ||
584 | else_branch, | ||
585 | }, | ||
586 | syntax_ptr, | ||
587 | ) | ||
588 | } | 548 | } |
589 | } | 549 | } |
590 | ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()), | 550 | ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()), |
@@ -610,14 +570,7 @@ impl ExprCollector { | |||
610 | let iterable = self.collect_expr_opt(e.iterable()); | 570 | let iterable = self.collect_expr_opt(e.iterable()); |
611 | let pat = self.collect_pat_opt(e.pat()); | 571 | let pat = self.collect_pat_opt(e.pat()); |
612 | let body = self.collect_block_opt(e.loop_body()); | 572 | let body = self.collect_block_opt(e.loop_body()); |
613 | self.alloc_expr( | 573 | self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) |
614 | Expr::For { | ||
615 | iterable, | ||
616 | pat, | ||
617 | body, | ||
618 | }, | ||
619 | syntax_ptr, | ||
620 | ) | ||
621 | } | 574 | } |
622 | ast::ExprKind::CallExpr(e) => { | 575 | ast::ExprKind::CallExpr(e) => { |
623 | let callee = self.collect_expr_opt(e.expr()); | 576 | let callee = self.collect_expr_opt(e.expr()); |
@@ -635,18 +588,8 @@ impl ExprCollector { | |||
635 | } else { | 588 | } else { |
636 | Vec::new() | 589 | Vec::new() |
637 | }; | 590 | }; |
638 | let method_name = e | 591 | let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
639 | .name_ref() | 592 | self.alloc_expr(Expr::MethodCall { receiver, method_name, args }, syntax_ptr) |
640 | .map(|nr| nr.as_name()) | ||
641 | .unwrap_or_else(Name::missing); | ||
642 | self.alloc_expr( | ||
643 | Expr::MethodCall { | ||
644 | receiver, | ||
645 | method_name, | ||
646 | args, | ||
647 | }, | ||
648 | syntax_ptr, | ||
649 | ) | ||
650 | } | 593 | } |
651 | ast::ExprKind::MatchExpr(e) => { | 594 | ast::ExprKind::MatchExpr(e) => { |
652 | let expr = self.collect_expr_opt(e.expr()); | 595 | let expr = self.collect_expr_opt(e.expr()); |
@@ -668,11 +611,8 @@ impl ExprCollector { | |||
668 | self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) | 611 | self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) |
669 | } | 612 | } |
670 | ast::ExprKind::PathExpr(e) => { | 613 | ast::ExprKind::PathExpr(e) => { |
671 | let path = e | 614 | let path = |
672 | .path() | 615 | e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing); |
673 | .and_then(Path::from_ast) | ||
674 | .map(Expr::Path) | ||
675 | .unwrap_or(Expr::Missing); | ||
676 | self.alloc_expr(path, syntax_ptr) | 616 | self.alloc_expr(path, syntax_ptr) |
677 | } | 617 | } |
678 | ast::ExprKind::ContinueExpr(_e) => { | 618 | ast::ExprKind::ContinueExpr(_e) => { |
@@ -721,21 +661,11 @@ impl ExprCollector { | |||
721 | Vec::new() | 661 | Vec::new() |
722 | }; | 662 | }; |
723 | let spread = e.spread().map(|s| self.collect_expr(s)); | 663 | let spread = e.spread().map(|s| self.collect_expr(s)); |
724 | self.alloc_expr( | 664 | self.alloc_expr(Expr::StructLit { path, fields, spread }, syntax_ptr) |
725 | Expr::StructLit { | ||
726 | path, | ||
727 | fields, | ||
728 | spread, | ||
729 | }, | ||
730 | syntax_ptr, | ||
731 | ) | ||
732 | } | 665 | } |
733 | ast::ExprKind::FieldExpr(e) => { | 666 | ast::ExprKind::FieldExpr(e) => { |
734 | let expr = self.collect_expr_opt(e.expr()); | 667 | let expr = self.collect_expr_opt(e.expr()); |
735 | let name = e | 668 | let name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
736 | .name_ref() | ||
737 | .map(|nr| nr.as_name()) | ||
738 | .unwrap_or_else(Name::missing); | ||
739 | self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) | 669 | self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) |
740 | } | 670 | } |
741 | ast::ExprKind::TryExpr(e) => { | 671 | ast::ExprKind::TryExpr(e) => { |
@@ -772,14 +702,7 @@ impl ExprCollector { | |||
772 | } | 702 | } |
773 | } | 703 | } |
774 | let body = self.collect_expr_opt(e.body()); | 704 | let body = self.collect_expr_opt(e.body()); |
775 | self.alloc_expr( | 705 | self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) |
776 | Expr::Lambda { | ||
777 | args, | ||
778 | arg_types, | ||
779 | body, | ||
780 | }, | ||
781 | syntax_ptr, | ||
782 | ) | ||
783 | } | 706 | } |
784 | ast::ExprKind::BinExpr(e) => { | 707 | ast::ExprKind::BinExpr(e) => { |
785 | let lhs = self.collect_expr_opt(e.lhs()); | 708 | let lhs = self.collect_expr_opt(e.lhs()); |
@@ -804,9 +727,8 @@ impl ExprCollector { | |||
804 | 727 | ||
805 | let lit = match child.flavor() { | 728 | let lit = match child.flavor() { |
806 | LiteralFlavor::IntNumber { suffix } => { | 729 | LiteralFlavor::IntNumber { suffix } => { |
807 | let known_name = suffix | 730 | let known_name = |
808 | .map(Name::new) | 731 | suffix.map(Name::new).and_then(|name| UncertainIntTy::from_name(&name)); |
809 | .and_then(|name| UncertainIntTy::from_name(&name)); | ||
810 | 732 | ||
811 | Literal::Int( | 733 | Literal::Int( |
812 | Default::default(), | 734 | Default::default(), |
@@ -857,11 +779,7 @@ impl ExprCollector { | |||
857 | let pat = self.collect_pat_opt(stmt.pat()); | 779 | let pat = self.collect_pat_opt(stmt.pat()); |
858 | let type_ref = stmt.type_ref().map(TypeRef::from_ast); | 780 | let type_ref = stmt.type_ref().map(TypeRef::from_ast); |
859 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); | 781 | let initializer = stmt.initializer().map(|e| self.collect_expr(e)); |
860 | Statement::Let { | 782 | Statement::Let { pat, type_ref, initializer } |
861 | pat, | ||
862 | type_ref, | ||
863 | initializer, | ||
864 | } | ||
865 | } | 783 | } |
866 | ast::StmtKind::ExprStmt(stmt) => { | 784 | ast::StmtKind::ExprStmt(stmt) => { |
867 | Statement::Expr(self.collect_expr_opt(stmt.expr())) | 785 | Statement::Expr(self.collect_expr_opt(stmt.expr())) |
@@ -869,10 +787,7 @@ impl ExprCollector { | |||
869 | }) | 787 | }) |
870 | .collect(); | 788 | .collect(); |
871 | let tail = block.expr().map(|e| self.collect_expr(e)); | 789 | let tail = block.expr().map(|e| self.collect_expr(e)); |
872 | self.alloc_expr( | 790 | self.alloc_expr(Expr::Block { statements, tail }, SyntaxNodePtr::new(block.syntax())) |
873 | Expr::Block { statements, tail }, | ||
874 | SyntaxNodePtr::new(block.syntax()), | ||
875 | ) | ||
876 | } | 791 | } |
877 | 792 | ||
878 | fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId { | 793 | fn collect_block_opt(&mut self, block: Option<&ast::Block>) -> ExprId { |
@@ -886,17 +801,10 @@ impl ExprCollector { | |||
886 | fn collect_pat(&mut self, pat: &ast::Pat) -> PatId { | 801 | fn collect_pat(&mut self, pat: &ast::Pat) -> PatId { |
887 | let pattern = match pat.kind() { | 802 | let pattern = match pat.kind() { |
888 | ast::PatKind::BindPat(bp) => { | 803 | ast::PatKind::BindPat(bp) => { |
889 | let name = bp | 804 | let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); |
890 | .name() | ||
891 | .map(|nr| nr.as_name()) | ||
892 | .unwrap_or_else(Name::missing); | ||
893 | let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); | 805 | let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); |
894 | let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); | 806 | let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); |
895 | Pat::Bind { | 807 | Pat::Bind { name, mode: annotation, subpat } |
896 | name, | ||
897 | mode: annotation, | ||
898 | subpat, | ||
899 | } | ||
900 | } | 808 | } |
901 | ast::PatKind::TupleStructPat(p) => { | 809 | ast::PatKind::TupleStructPat(p) => { |
902 | let path = p.path().and_then(Path::from_ast); | 810 | let path = p.path().and_then(Path::from_ast); |
@@ -919,9 +827,8 @@ impl ExprCollector { | |||
919 | ast::PatKind::PlaceholderPat(_) => Pat::Wild, | 827 | ast::PatKind::PlaceholderPat(_) => Pat::Wild, |
920 | ast::PatKind::StructPat(p) => { | 828 | ast::PatKind::StructPat(p) => { |
921 | let path = p.path().and_then(Path::from_ast); | 829 | let path = p.path().and_then(Path::from_ast); |
922 | let field_pat_list = p | 830 | let field_pat_list = |
923 | .field_pat_list() | 831 | p.field_pat_list().expect("every struct should have a field list"); |
924 | .expect("every struct should have a field list"); | ||
925 | let mut fields: Vec<_> = field_pat_list | 832 | let mut fields: Vec<_> = field_pat_list |
926 | .bind_pats() | 833 | .bind_pats() |
927 | .map(|bind_pat| { | 834 | .map(|bind_pat| { |
@@ -961,10 +868,7 @@ impl ExprCollector { | |||
961 | if let Some(param_list) = node.param_list() { | 868 | if let Some(param_list) = node.param_list() { |
962 | if let Some(self_param) = param_list.self_param() { | 869 | if let Some(self_param) = param_list.self_param() { |
963 | let self_param = SyntaxNodePtr::new( | 870 | let self_param = SyntaxNodePtr::new( |
964 | self_param | 871 | self_param.self_kw().expect("self param without self keyword").syntax(), |
965 | .self_kw() | ||
966 | .expect("self param without self keyword") | ||
967 | .syntax(), | ||
968 | ); | 872 | ); |
969 | let param_pat = self.alloc_pat( | 873 | let param_pat = self.alloc_pat( |
970 | Pat::Bind { | 874 | Pat::Bind { |