aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/add_explicit_type.rs6
-rw-r--r--crates/ra_assists/src/add_missing_impl_members.rs24
-rw-r--r--crates/ra_assists/src/fill_match_arms.rs6
-rw-r--r--crates/ra_assists/src/inline_local_variable.rs50
-rw-r--r--crates/ra_assists/src/merge_match_arms.rs4
-rw-r--r--crates/ra_hir/src/expr.rs90
-rw-r--r--crates/ra_hir/src/generics.rs4
-rw-r--r--crates/ra_hir/src/impl_block.rs8
-rw-r--r--crates/ra_hir/src/nameres/raw.rs24
-rw-r--r--crates/ra_hir/src/traits.rs8
-rw-r--r--crates/ra_hir/src/type_ref.rs35
-rw-r--r--crates/ra_ide_api/src/call_info.rs4
-rw-r--r--crates/ra_ide_api/src/impls.rs8
-rw-r--r--crates/ra_ide_api/src/inlay_hints.rs22
-rw-r--r--crates/ra_ide_api/src/references.rs2
-rw-r--r--crates/ra_ide_api/src/runnables.rs4
-rw-r--r--crates/ra_syntax/src/ast/generated.rs684
-rw-r--r--crates/ra_syntax/src/lib.rs16
-rw-r--r--crates/ra_tools/src/boilerplate_gen.rs87
19 files changed, 547 insertions, 539 deletions
diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs
index 88970929f..78f0f7f28 100644
--- a/crates/ra_assists/src/add_explicit_type.rs
+++ b/crates/ra_assists/src/add_explicit_type.rs
@@ -1,6 +1,6 @@
1use hir::{db::HirDatabase, HirDisplay, Ty}; 1use hir::{db::HirDatabase, HirDisplay, Ty};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{AstNode, LetStmt, NameOwner, PatKind}, 3 ast::{self, AstNode, LetStmt, NameOwner},
4 T, 4 T,
5}; 5};
6 6
@@ -12,8 +12,8 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
12 let expr = stmt.initializer()?; 12 let expr = stmt.initializer()?;
13 let pat = stmt.pat()?; 13 let pat = stmt.pat()?;
14 // Must be a binding 14 // Must be a binding
15 let pat = match pat.kind() { 15 let pat = match pat {
16 PatKind::BindPat(bind_pat) => bind_pat, 16 ast::Pat::BindPat(bind_pat) => bind_pat,
17 _ => return None, 17 _ => return None,
18 }; 18 };
19 let pat_range = pat.syntax().text_range(); 19 let pat_range = pat.syntax().text_range();
diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs
index 995e44d5e..31c7d4e80 100644
--- a/crates/ra_assists/src/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/add_missing_impl_members.rs
@@ -5,7 +5,7 @@ use crate::{
5 5
6use hir::{db::HirDatabase, HasSource}; 6use hir::{db::HirDatabase, HasSource};
7use ra_db::FilePosition; 7use ra_db::FilePosition;
8use ra_syntax::ast::{self, AstNode, ImplItemKind, NameOwner}; 8use ra_syntax::ast::{self, AstNode, NameOwner};
9use ra_syntax::SmolStr; 9use ra_syntax::SmolStr;
10 10
11#[derive(PartialEq)] 11#[derive(PartialEq)]
@@ -49,11 +49,11 @@ fn add_missing_impl_members_inner(
49 resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? 49 resolve_target_trait_def(ctx.db, &analyzer, &impl_node)?
50 }; 50 };
51 51
52 let def_name = |kind| -> Option<SmolStr> { 52 let def_name = |item: &ast::ImplItem| -> Option<SmolStr> {
53 match kind { 53 match item {
54 ast::ImplItemKind::FnDef(def) => def.name(), 54 ast::ImplItem::FnDef(def) => def.name(),
55 ast::ImplItemKind::TypeAliasDef(def) => def.name(), 55 ast::ImplItem::TypeAliasDef(def) => def.name(),
56 ast::ImplItemKind::ConstDef(def) => def.name(), 56 ast::ImplItem::ConstDef(def) => def.name(),
57 } 57 }
58 .map(|it| it.text().clone()) 58 .map(|it| it.text().clone())
59 }; 59 };
@@ -62,15 +62,15 @@ fn add_missing_impl_members_inner(
62 let impl_items = impl_item_list.impl_items().collect::<Vec<_>>(); 62 let impl_items = impl_item_list.impl_items().collect::<Vec<_>>();
63 63
64 let missing_items: Vec<_> = trait_items 64 let missing_items: Vec<_> = trait_items
65 .filter(|t| def_name(t.kind()).is_some()) 65 .filter(|t| def_name(t).is_some())
66 .filter(|t| match t.kind() { 66 .filter(|t| match t {
67 ImplItemKind::FnDef(def) => match mode { 67 ast::ImplItem::FnDef(def) => match mode {
68 AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), 68 AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
69 AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), 69 AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
70 }, 70 },
71 _ => mode == AddMissingImplMembersMode::NoDefaultMethods, 71 _ => mode == AddMissingImplMembersMode::NoDefaultMethods,
72 }) 72 })
73 .filter(|t| impl_items.iter().all(|i| def_name(i.kind()) != def_name(t.kind()))) 73 .filter(|t| impl_items.iter().all(|i| def_name(i) != def_name(t)))
74 .collect(); 74 .collect();
75 if missing_items.is_empty() { 75 if missing_items.is_empty() {
76 return None; 76 return None;
@@ -78,8 +78,8 @@ fn add_missing_impl_members_inner(
78 78
79 ctx.add_action(AssistId(assist_id), label, |edit| { 79 ctx.add_action(AssistId(assist_id), label, |edit| {
80 let n_existing_items = impl_item_list.impl_items().count(); 80 let n_existing_items = impl_item_list.impl_items().count();
81 let items = missing_items.into_iter().map(|it| match it.kind() { 81 let items = missing_items.into_iter().map(|it| match it {
82 ImplItemKind::FnDef(def) => strip_docstring(add_body(def).into()), 82 ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()),
83 _ => strip_docstring(it), 83 _ => strip_docstring(it),
84 }); 84 });
85 let mut ast_editor = AstEditor::new(impl_item_list); 85 let mut ast_editor = AstEditor::new(impl_item_list);
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs
index 939429892..85ff5c052 100644
--- a/crates/ra_assists/src/fill_match_arms.rs
+++ b/crates/ra_assists/src/fill_match_arms.rs
@@ -7,12 +7,12 @@ use ra_syntax::ast::{self, AstNode};
7use crate::{Assist, AssistCtx, AssistId}; 7use crate::{Assist, AssistCtx, AssistId};
8 8
9fn is_trivial_arm(arm: &ast::MatchArm) -> bool { 9fn is_trivial_arm(arm: &ast::MatchArm) -> bool {
10 fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> { 10 fn single_pattern(arm: &ast::MatchArm) -> Option<ast::Pat> {
11 let (pat,) = arm.pats().collect_tuple()?; 11 let (pat,) = arm.pats().collect_tuple()?;
12 Some(pat.kind()) 12 Some(pat)
13 } 13 }
14 match single_pattern(arm) { 14 match single_pattern(arm) {
15 Some(ast::PatKind::PlaceholderPat(..)) => true, 15 Some(ast::Pat::PlaceholderPat(..)) => true,
16 _ => false, 16 _ => false,
17 } 17 }
18} 18}
diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs
index 82c4d54b0..eedb29199 100644
--- a/crates/ra_assists/src/inline_local_variable.rs
+++ b/crates/ra_assists/src/inline_local_variable.rs
@@ -1,6 +1,6 @@
1use hir::db::HirDatabase; 1use hir::db::HirDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, AstNode, AstToken, ExprKind, PatKind}, 3 ast::{self, AstNode, AstToken},
4 TextRange, 4 TextRange,
5}; 5};
6 6
@@ -9,8 +9,8 @@ use crate::{Assist, AssistCtx, AssistId};
9 9
10pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?; 11 let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?;
12 let bind_pat = match let_stmt.pat()?.kind() { 12 let bind_pat = match let_stmt.pat()? {
13 PatKind::BindPat(pat) => pat, 13 ast::Pat::BindPat(pat) => pat,
14 _ => return None, 14 _ => return None,
15 }; 15 };
16 if bind_pat.is_mutable() { 16 if bind_pat.is_mutable() {
@@ -48,28 +48,28 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Opt
48 } 48 }
49 }; 49 };
50 50
51 wrap_in_parens[i] = match (initializer_expr.kind(), usage_parent.kind()) { 51 wrap_in_parens[i] = match (&initializer_expr, usage_parent) {
52 (ExprKind::CallExpr(_), _) 52 (ast::Expr::CallExpr(_), _)
53 | (ExprKind::IndexExpr(_), _) 53 | (ast::Expr::IndexExpr(_), _)
54 | (ExprKind::MethodCallExpr(_), _) 54 | (ast::Expr::MethodCallExpr(_), _)
55 | (ExprKind::FieldExpr(_), _) 55 | (ast::Expr::FieldExpr(_), _)
56 | (ExprKind::TryExpr(_), _) 56 | (ast::Expr::TryExpr(_), _)
57 | (ExprKind::RefExpr(_), _) 57 | (ast::Expr::RefExpr(_), _)
58 | (ExprKind::Literal(_), _) 58 | (ast::Expr::Literal(_), _)
59 | (ExprKind::TupleExpr(_), _) 59 | (ast::Expr::TupleExpr(_), _)
60 | (ExprKind::ArrayExpr(_), _) 60 | (ast::Expr::ArrayExpr(_), _)
61 | (ExprKind::ParenExpr(_), _) 61 | (ast::Expr::ParenExpr(_), _)
62 | (ExprKind::PathExpr(_), _) 62 | (ast::Expr::PathExpr(_), _)
63 | (ExprKind::BlockExpr(_), _) 63 | (ast::Expr::BlockExpr(_), _)
64 | (_, ExprKind::CallExpr(_)) 64 | (_, ast::Expr::CallExpr(_))
65 | (_, ExprKind::TupleExpr(_)) 65 | (_, ast::Expr::TupleExpr(_))
66 | (_, ExprKind::ArrayExpr(_)) 66 | (_, ast::Expr::ArrayExpr(_))
67 | (_, ExprKind::ParenExpr(_)) 67 | (_, ast::Expr::ParenExpr(_))
68 | (_, ExprKind::ForExpr(_)) 68 | (_, ast::Expr::ForExpr(_))
69 | (_, ExprKind::WhileExpr(_)) 69 | (_, ast::Expr::WhileExpr(_))
70 | (_, ExprKind::BreakExpr(_)) 70 | (_, ast::Expr::BreakExpr(_))
71 | (_, ExprKind::ReturnExpr(_)) 71 | (_, ast::Expr::ReturnExpr(_))
72 | (_, ExprKind::MatchExpr(_)) => false, 72 | (_, ast::Expr::MatchExpr(_)) => false,
73 _ => true, 73 _ => true,
74 }; 74 };
75 } 75 }
diff --git a/crates/ra_assists/src/merge_match_arms.rs b/crates/ra_assists/src/merge_match_arms.rs
index bc5f6f17c..225a48d3a 100644
--- a/crates/ra_assists/src/merge_match_arms.rs
+++ b/crates/ra_assists/src/merge_match_arms.rs
@@ -30,8 +30,8 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<A
30 30
31 ctx.add_action(AssistId("merge_match_arms"), "merge match arms", |edit| { 31 ctx.add_action(AssistId("merge_match_arms"), "merge match arms", |edit| {
32 fn contains_placeholder(a: &MatchArm) -> bool { 32 fn contains_placeholder(a: &MatchArm) -> bool {
33 a.pats().any(|x| match x.kind() { 33 a.pats().any(|x| match x {
34 ra_syntax::ast::PatKind::PlaceholderPat(..) => true, 34 ra_syntax::ast::Pat::PlaceholderPat(..) => true,
35 _ => false, 35 _ => false,
36 }) 36 })
37 } 37 }
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index a16561d11..328d635d4 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -602,8 +602,8 @@ where
602 602
603 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { 603 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
604 let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); 604 let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
605 match expr.kind() { 605 match expr {
606 ast::ExprKind::IfExpr(e) => { 606 ast::Expr::IfExpr(e) => {
607 let then_branch = self.collect_block_opt(e.then_branch()); 607 let then_branch = self.collect_block_opt(e.then_branch());
608 608
609 let else_branch = e.else_branch().map(|b| match b { 609 let else_branch = e.else_branch().map(|b| match b {
@@ -639,16 +639,16 @@ where
639 639
640 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) 640 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
641 } 641 }
642 ast::ExprKind::TryBlockExpr(e) => { 642 ast::Expr::TryBlockExpr(e) => {
643 let body = self.collect_block_opt(e.try_body()); 643 let body = self.collect_block_opt(e.try_body());
644 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) 644 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
645 } 645 }
646 ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()), 646 ast::Expr::BlockExpr(e) => self.collect_block_opt(e.block()),
647 ast::ExprKind::LoopExpr(e) => { 647 ast::Expr::LoopExpr(e) => {
648 let body = self.collect_block_opt(e.loop_body()); 648 let body = self.collect_block_opt(e.loop_body());
649 self.alloc_expr(Expr::Loop { body }, syntax_ptr) 649 self.alloc_expr(Expr::Loop { body }, syntax_ptr)
650 } 650 }
651 ast::ExprKind::WhileExpr(e) => { 651 ast::Expr::WhileExpr(e) => {
652 let body = self.collect_block_opt(e.loop_body()); 652 let body = self.collect_block_opt(e.loop_body());
653 653
654 let condition = match e.condition() { 654 let condition = match e.condition() {
@@ -675,13 +675,13 @@ where
675 675
676 self.alloc_expr(Expr::While { condition, body }, syntax_ptr) 676 self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
677 } 677 }
678 ast::ExprKind::ForExpr(e) => { 678 ast::Expr::ForExpr(e) => {
679 let iterable = self.collect_expr_opt(e.iterable()); 679 let iterable = self.collect_expr_opt(e.iterable());
680 let pat = self.collect_pat_opt(e.pat()); 680 let pat = self.collect_pat_opt(e.pat());
681 let body = self.collect_block_opt(e.loop_body()); 681 let body = self.collect_block_opt(e.loop_body());
682 self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) 682 self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr)
683 } 683 }
684 ast::ExprKind::CallExpr(e) => { 684 ast::Expr::CallExpr(e) => {
685 let callee = self.collect_expr_opt(e.expr()); 685 let callee = self.collect_expr_opt(e.expr());
686 let args = if let Some(arg_list) = e.arg_list() { 686 let args = if let Some(arg_list) = e.arg_list() {
687 arg_list.args().map(|e| self.collect_expr(e)).collect() 687 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -690,7 +690,7 @@ where
690 }; 690 };
691 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr) 691 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
692 } 692 }
693 ast::ExprKind::MethodCallExpr(e) => { 693 ast::Expr::MethodCallExpr(e) => {
694 let receiver = self.collect_expr_opt(e.expr()); 694 let receiver = self.collect_expr_opt(e.expr());
695 let args = if let Some(arg_list) = e.arg_list() { 695 let args = if let Some(arg_list) = e.arg_list() {
696 arg_list.args().map(|e| self.collect_expr(e)).collect() 696 arg_list.args().map(|e| self.collect_expr(e)).collect()
@@ -704,7 +704,7 @@ where
704 syntax_ptr, 704 syntax_ptr,
705 ) 705 )
706 } 706 }
707 ast::ExprKind::MatchExpr(e) => { 707 ast::Expr::MatchExpr(e) => {
708 let expr = self.collect_expr_opt(e.expr()); 708 let expr = self.collect_expr_opt(e.expr());
709 let arms = if let Some(match_arm_list) = e.match_arm_list() { 709 let arms = if let Some(match_arm_list) = e.match_arm_list() {
710 match_arm_list 710 match_arm_list
@@ -723,30 +723,30 @@ where
723 }; 723 };
724 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr) 724 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
725 } 725 }
726 ast::ExprKind::PathExpr(e) => { 726 ast::Expr::PathExpr(e) => {
727 let path = 727 let path =
728 e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing); 728 e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing);
729 self.alloc_expr(path, syntax_ptr) 729 self.alloc_expr(path, syntax_ptr)
730 } 730 }
731 ast::ExprKind::ContinueExpr(_e) => { 731 ast::Expr::ContinueExpr(_e) => {
732 // FIXME: labels 732 // FIXME: labels
733 self.alloc_expr(Expr::Continue, syntax_ptr) 733 self.alloc_expr(Expr::Continue, syntax_ptr)
734 } 734 }
735 ast::ExprKind::BreakExpr(e) => { 735 ast::Expr::BreakExpr(e) => {
736 let expr = e.expr().map(|e| self.collect_expr(e)); 736 let expr = e.expr().map(|e| self.collect_expr(e));
737 self.alloc_expr(Expr::Break { expr }, syntax_ptr) 737 self.alloc_expr(Expr::Break { expr }, syntax_ptr)
738 } 738 }
739 ast::ExprKind::ParenExpr(e) => { 739 ast::Expr::ParenExpr(e) => {
740 let inner = self.collect_expr_opt(e.expr()); 740 let inner = self.collect_expr_opt(e.expr());
741 // make the paren expr point to the inner expression as well 741 // make the paren expr point to the inner expression as well
742 self.source_map.expr_map.insert(syntax_ptr, inner); 742 self.source_map.expr_map.insert(syntax_ptr, inner);
743 inner 743 inner
744 } 744 }
745 ast::ExprKind::ReturnExpr(e) => { 745 ast::Expr::ReturnExpr(e) => {
746 let expr = e.expr().map(|e| self.collect_expr(e)); 746 let expr = e.expr().map(|e| self.collect_expr(e));
747 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 747 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
748 } 748 }
749 ast::ExprKind::StructLit(e) => { 749 ast::Expr::StructLit(e) => {
750 let path = e.path().and_then(Path::from_ast); 750 let path = e.path().and_then(Path::from_ast);
751 let mut field_ptrs = Vec::new(); 751 let mut field_ptrs = Vec::new();
752 let struct_lit = if let Some(nfl) = e.named_field_list() { 752 let struct_lit = if let Some(nfl) = e.named_field_list() {
@@ -787,7 +787,7 @@ where
787 } 787 }
788 res 788 res
789 } 789 }
790 ast::ExprKind::FieldExpr(e) => { 790 ast::Expr::FieldExpr(e) => {
791 let expr = self.collect_expr_opt(e.expr()); 791 let expr = self.collect_expr_opt(e.expr());
792 let name = match e.field_access() { 792 let name = match e.field_access() {
793 Some(kind) => kind.as_name(), 793 Some(kind) => kind.as_name(),
@@ -795,25 +795,25 @@ where
795 }; 795 };
796 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr) 796 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
797 } 797 }
798 ast::ExprKind::AwaitExpr(e) => { 798 ast::Expr::AwaitExpr(e) => {
799 let expr = self.collect_expr_opt(e.expr()); 799 let expr = self.collect_expr_opt(e.expr());
800 self.alloc_expr(Expr::Await { expr }, syntax_ptr) 800 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
801 } 801 }
802 ast::ExprKind::TryExpr(e) => { 802 ast::Expr::TryExpr(e) => {
803 let expr = self.collect_expr_opt(e.expr()); 803 let expr = self.collect_expr_opt(e.expr());
804 self.alloc_expr(Expr::Try { expr }, syntax_ptr) 804 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
805 } 805 }
806 ast::ExprKind::CastExpr(e) => { 806 ast::Expr::CastExpr(e) => {
807 let expr = self.collect_expr_opt(e.expr()); 807 let expr = self.collect_expr_opt(e.expr());
808 let type_ref = TypeRef::from_ast_opt(e.type_ref()); 808 let type_ref = TypeRef::from_ast_opt(e.type_ref());
809 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) 809 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
810 } 810 }
811 ast::ExprKind::RefExpr(e) => { 811 ast::Expr::RefExpr(e) => {
812 let expr = self.collect_expr_opt(e.expr()); 812 let expr = self.collect_expr_opt(e.expr());
813 let mutability = Mutability::from_mutable(e.is_mut()); 813 let mutability = Mutability::from_mutable(e.is_mut());
814 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr) 814 self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
815 } 815 }
816 ast::ExprKind::PrefixExpr(e) => { 816 ast::Expr::PrefixExpr(e) => {
817 let expr = self.collect_expr_opt(e.expr()); 817 let expr = self.collect_expr_opt(e.expr());
818 if let Some(op) = e.op_kind() { 818 if let Some(op) = e.op_kind() {
819 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) 819 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
@@ -821,7 +821,7 @@ where
821 self.alloc_expr(Expr::Missing, syntax_ptr) 821 self.alloc_expr(Expr::Missing, syntax_ptr)
822 } 822 }
823 } 823 }
824 ast::ExprKind::LambdaExpr(e) => { 824 ast::Expr::LambdaExpr(e) => {
825 let mut args = Vec::new(); 825 let mut args = Vec::new();
826 let mut arg_types = Vec::new(); 826 let mut arg_types = Vec::new();
827 if let Some(pl) = e.param_list() { 827 if let Some(pl) = e.param_list() {
@@ -835,18 +835,18 @@ where
835 let body = self.collect_expr_opt(e.body()); 835 let body = self.collect_expr_opt(e.body());
836 self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr) 836 self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr)
837 } 837 }
838 ast::ExprKind::BinExpr(e) => { 838 ast::Expr::BinExpr(e) => {
839 let lhs = self.collect_expr_opt(e.lhs()); 839 let lhs = self.collect_expr_opt(e.lhs());
840 let rhs = self.collect_expr_opt(e.rhs()); 840 let rhs = self.collect_expr_opt(e.rhs());
841 let op = e.op_kind().map(BinaryOp::from); 841 let op = e.op_kind().map(BinaryOp::from);
842 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) 842 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
843 } 843 }
844 ast::ExprKind::TupleExpr(e) => { 844 ast::Expr::TupleExpr(e) => {
845 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect(); 845 let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
846 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr) 846 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
847 } 847 }
848 848
849 ast::ExprKind::ArrayExpr(e) => { 849 ast::Expr::ArrayExpr(e) => {
850 let kind = e.kind(); 850 let kind = e.kind();
851 851
852 match kind { 852 match kind {
@@ -865,7 +865,7 @@ where
865 } 865 }
866 } 866 }
867 867
868 ast::ExprKind::Literal(e) => { 868 ast::Expr::Literal(e) => {
869 let lit = match e.kind() { 869 let lit = match e.kind() {
870 LiteralKind::IntNumber { suffix } => { 870 LiteralKind::IntNumber { suffix } => {
871 let known_name = suffix 871 let known_name = suffix
@@ -895,16 +895,16 @@ where
895 }; 895 };
896 self.alloc_expr(Expr::Literal(lit), syntax_ptr) 896 self.alloc_expr(Expr::Literal(lit), syntax_ptr)
897 } 897 }
898 ast::ExprKind::IndexExpr(e) => { 898 ast::Expr::IndexExpr(e) => {
899 let base = self.collect_expr_opt(e.base()); 899 let base = self.collect_expr_opt(e.base());
900 let index = self.collect_expr_opt(e.index()); 900 let index = self.collect_expr_opt(e.index());
901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr) 901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
902 } 902 }
903 903
904 // FIXME implement HIR for these: 904 // FIXME implement HIR for these:
905 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 905 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
906 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 906 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
907 ast::ExprKind::MacroCall(e) => { 907 ast::Expr::MacroCall(e) => {
908 let ast_id = self 908 let ast_id = self
909 .db 909 .db
910 .ast_id_map(self.current_file_id) 910 .ast_id_map(self.current_file_id)
@@ -945,16 +945,14 @@ where
945 fn collect_block(&mut self, block: ast::Block) -> ExprId { 945 fn collect_block(&mut self, block: ast::Block) -> ExprId {
946 let statements = block 946 let statements = block
947 .statements() 947 .statements()
948 .map(|s| match s.kind() { 948 .map(|s| match s {
949 ast::StmtKind::LetStmt(stmt) => { 949 ast::Stmt::LetStmt(stmt) => {
950 let pat = self.collect_pat_opt(stmt.pat()); 950 let pat = self.collect_pat_opt(stmt.pat());
951 let type_ref = stmt.ascribed_type().map(TypeRef::from_ast); 951 let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
952 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 952 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
953 Statement::Let { pat, type_ref, initializer } 953 Statement::Let { pat, type_ref, initializer }
954 } 954 }
955 ast::StmtKind::ExprStmt(stmt) => { 955 ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
956 Statement::Expr(self.collect_expr_opt(stmt.expr()))
957 }
958 }) 956 })
959 .collect(); 957 .collect();
960 let tail = block.expr().map(|e| self.collect_expr(e)); 958 let tail = block.expr().map(|e| self.collect_expr(e));
@@ -970,33 +968,33 @@ where
970 } 968 }
971 969
972 fn collect_pat(&mut self, pat: ast::Pat) -> PatId { 970 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
973 let pattern = match pat.kind() { 971 let pattern = match &pat {
974 ast::PatKind::BindPat(bp) => { 972 ast::Pat::BindPat(bp) => {
975 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); 973 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
976 let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref()); 974 let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref());
977 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat)); 975 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
978 Pat::Bind { name, mode: annotation, subpat } 976 Pat::Bind { name, mode: annotation, subpat }
979 } 977 }
980 ast::PatKind::TupleStructPat(p) => { 978 ast::Pat::TupleStructPat(p) => {
981 let path = p.path().and_then(Path::from_ast); 979 let path = p.path().and_then(Path::from_ast);
982 let args = p.args().map(|p| self.collect_pat(p)).collect(); 980 let args = p.args().map(|p| self.collect_pat(p)).collect();
983 Pat::TupleStruct { path, args } 981 Pat::TupleStruct { path, args }
984 } 982 }
985 ast::PatKind::RefPat(p) => { 983 ast::Pat::RefPat(p) => {
986 let pat = self.collect_pat_opt(p.pat()); 984 let pat = self.collect_pat_opt(p.pat());
987 let mutability = Mutability::from_mutable(p.is_mut()); 985 let mutability = Mutability::from_mutable(p.is_mut());
988 Pat::Ref { pat, mutability } 986 Pat::Ref { pat, mutability }
989 } 987 }
990 ast::PatKind::PathPat(p) => { 988 ast::Pat::PathPat(p) => {
991 let path = p.path().and_then(Path::from_ast); 989 let path = p.path().and_then(Path::from_ast);
992 path.map(Pat::Path).unwrap_or(Pat::Missing) 990 path.map(Pat::Path).unwrap_or(Pat::Missing)
993 } 991 }
994 ast::PatKind::TuplePat(p) => { 992 ast::Pat::TuplePat(p) => {
995 let args = p.args().map(|p| self.collect_pat(p)).collect(); 993 let args = p.args().map(|p| self.collect_pat(p)).collect();
996 Pat::Tuple(args) 994 Pat::Tuple(args)
997 } 995 }
998 ast::PatKind::PlaceholderPat(_) => Pat::Wild, 996 ast::Pat::PlaceholderPat(_) => Pat::Wild,
999 ast::PatKind::StructPat(p) => { 997 ast::Pat::StructPat(p) => {
1000 let path = p.path().and_then(Path::from_ast); 998 let path = p.path().and_then(Path::from_ast);
1001 let field_pat_list = 999 let field_pat_list =
1002 p.field_pat_list().expect("every struct should have a field list"); 1000 p.field_pat_list().expect("every struct should have a field list");
@@ -1022,8 +1020,8 @@ where
1022 } 1020 }
1023 1021
1024 // FIXME: implement 1022 // FIXME: implement
1025 ast::PatKind::LiteralPat(_) => Pat::Missing, 1023 ast::Pat::LiteralPat(_) => Pat::Missing,
1026 ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, 1024 ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
1027 }; 1025 };
1028 let ptr = AstPtr::new(&pat); 1026 let ptr = AstPtr::new(&pat);
1029 self.alloc_pat(pattern, Either::A(ptr)) 1027 self.alloc_pat(pattern, Either::A(ptr))
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
index bcbb4988d..e6482180d 100644
--- a/crates/ra_hir/src/generics.rs
+++ b/crates/ra_hir/src/generics.rs
@@ -137,8 +137,8 @@ impl GenericParams {
137 fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) { 137 fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
138 let path = bound 138 let path = bound
139 .type_ref() 139 .type_ref()
140 .and_then(|tr| match tr.kind() { 140 .and_then(|tr| match tr {
141 ast::TypeRefKind::PathType(path) => path.path(), 141 ast::TypeRef::PathType(path) => path.path(),
142 _ => None, 142 _ => None,
143 }) 143 })
144 .and_then(Path::from_ast); 144 .and_then(Path::from_ast);
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index 8e62cf66d..62a41719a 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -131,10 +131,10 @@ impl ImplData {
131 let items = if let Some(item_list) = node.item_list() { 131 let items = if let Some(item_list) = node.item_list() {
132 item_list 132 item_list
133 .impl_items() 133 .impl_items()
134 .map(|item_node| match item_node.kind() { 134 .map(|item_node| match item_node {
135 ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(), 135 ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
136 ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(), 136 ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
137 ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(), 137 ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
138 }) 138 })
139 .collect() 139 .collect()
140 } else { 140 } else {
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index 584e15e29..2f973359f 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -207,24 +207,24 @@ impl RawItemsCollector {
207 } 207 }
208 208
209 fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) { 209 fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) {
210 let (kind, name) = match item.kind() { 210 let (kind, name) = match item {
211 ast::ModuleItemKind::Module(module) => { 211 ast::ModuleItem::Module(module) => {
212 self.add_module(current_module, module); 212 self.add_module(current_module, module);
213 return; 213 return;
214 } 214 }
215 ast::ModuleItemKind::UseItem(use_item) => { 215 ast::ModuleItem::UseItem(use_item) => {
216 self.add_use_item(current_module, use_item); 216 self.add_use_item(current_module, use_item);
217 return; 217 return;
218 } 218 }
219 ast::ModuleItemKind::ExternCrateItem(extern_crate) => { 219 ast::ModuleItem::ExternCrateItem(extern_crate) => {
220 self.add_extern_crate_item(current_module, extern_crate); 220 self.add_extern_crate_item(current_module, extern_crate);
221 return; 221 return;
222 } 222 }
223 ast::ModuleItemKind::ImplBlock(_) => { 223 ast::ModuleItem::ImplBlock(_) => {
224 // impls don't participate in name resolution 224 // impls don't participate in name resolution
225 return; 225 return;
226 } 226 }
227 ast::ModuleItemKind::StructDef(it) => { 227 ast::ModuleItem::StructDef(it) => {
228 let id = self.source_ast_id_map.ast_id(&it); 228 let id = self.source_ast_id_map.ast_id(&it);
229 let name = it.name(); 229 let name = it.name();
230 if it.is_union() { 230 if it.is_union() {
@@ -233,22 +233,22 @@ impl RawItemsCollector {
233 (DefKind::Struct(id), name) 233 (DefKind::Struct(id), name)
234 } 234 }
235 } 235 }
236 ast::ModuleItemKind::EnumDef(it) => { 236 ast::ModuleItem::EnumDef(it) => {
237 (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) 237 (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name())
238 } 238 }
239 ast::ModuleItemKind::FnDef(it) => { 239 ast::ModuleItem::FnDef(it) => {
240 (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name()) 240 (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name())
241 } 241 }
242 ast::ModuleItemKind::TraitDef(it) => { 242 ast::ModuleItem::TraitDef(it) => {
243 (DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name()) 243 (DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name())
244 } 244 }
245 ast::ModuleItemKind::TypeAliasDef(it) => { 245 ast::ModuleItem::TypeAliasDef(it) => {
246 (DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name()) 246 (DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name())
247 } 247 }
248 ast::ModuleItemKind::ConstDef(it) => { 248 ast::ModuleItem::ConstDef(it) => {
249 (DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name()) 249 (DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name())
250 } 250 }
251 ast::ModuleItemKind::StaticDef(it) => { 251 ast::ModuleItem::StaticDef(it) => {
252 (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) 252 (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
253 } 253 }
254 }; 254 };
diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs
index de26f1a68..6cdd3020a 100644
--- a/crates/ra_hir/src/traits.rs
+++ b/crates/ra_hir/src/traits.rs
@@ -30,10 +30,10 @@ impl TraitData {
30 let items = if let Some(item_list) = src.ast.item_list() { 30 let items = if let Some(item_list) = src.ast.item_list() {
31 item_list 31 item_list
32 .impl_items() 32 .impl_items()
33 .map(|item_node| match item_node.kind() { 33 .map(|item_node| match item_node {
34 ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(), 34 ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
35 ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(), 35 ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
36 ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(), 36 ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
37 }) 37 })
38 .collect() 38 .collect()
39 } else { 39 } else {
diff --git a/crates/ra_hir/src/type_ref.rs b/crates/ra_hir/src/type_ref.rs
index 8536ae44a..b92a0b55a 100644
--- a/crates/ra_hir/src/type_ref.rs
+++ b/crates/ra_hir/src/type_ref.rs
@@ -57,28 +57,33 @@ pub enum TypeRef {
57impl TypeRef { 57impl TypeRef {
58 /// Converts an `ast::TypeRef` to a `hir::TypeRef`. 58 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
59 pub(crate) fn from_ast(node: ast::TypeRef) -> Self { 59 pub(crate) fn from_ast(node: ast::TypeRef) -> Self {
60 use ra_syntax::ast::TypeRefKind::*; 60 match node {
61 match node.kind() { 61 ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
62 ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()), 62 ast::TypeRef::TupleType(inner) => {
63 TupleType(inner) => TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect()), 63 TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect())
64 NeverType(..) => TypeRef::Never, 64 }
65 PathType(inner) => { 65 ast::TypeRef::NeverType(..) => TypeRef::Never,
66 ast::TypeRef::PathType(inner) => {
66 inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error) 67 inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
67 } 68 }
68 PointerType(inner) => { 69 ast::TypeRef::PointerType(inner) => {
69 let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); 70 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
70 let mutability = Mutability::from_mutable(inner.is_mut()); 71 let mutability = Mutability::from_mutable(inner.is_mut());
71 TypeRef::RawPtr(Box::new(inner_ty), mutability) 72 TypeRef::RawPtr(Box::new(inner_ty), mutability)
72 } 73 }
73 ArrayType(inner) => TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref()))), 74 ast::TypeRef::ArrayType(inner) => {
74 SliceType(inner) => TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref()))), 75 TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
75 ReferenceType(inner) => { 76 }
77 ast::TypeRef::SliceType(inner) => {
78 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
79 }
80 ast::TypeRef::ReferenceType(inner) => {
76 let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); 81 let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
77 let mutability = Mutability::from_mutable(inner.is_mut()); 82 let mutability = Mutability::from_mutable(inner.is_mut());
78 TypeRef::Reference(Box::new(inner_ty), mutability) 83 TypeRef::Reference(Box::new(inner_ty), mutability)
79 } 84 }
80 PlaceholderType(_inner) => TypeRef::Placeholder, 85 ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,
81 FnPointerType(inner) => { 86 ast::TypeRef::FnPointerType(inner) => {
82 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref())); 87 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
83 let mut params = if let Some(pl) = inner.param_list() { 88 let mut params = if let Some(pl) = inner.param_list() {
84 pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect() 89 pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
@@ -89,9 +94,9 @@ impl TypeRef {
89 TypeRef::Fn(params) 94 TypeRef::Fn(params)
90 } 95 }
91 // for types are close enough for our purposes to the inner type for now... 96 // for types are close enough for our purposes to the inner type for now...
92 ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()), 97 ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
93 ImplTraitType(_inner) => TypeRef::Error, 98 ast::TypeRef::ImplTraitType(_inner) => TypeRef::Error,
94 DynTraitType(_inner) => TypeRef::Error, 99 ast::TypeRef::DynTraitType(_inner) => TypeRef::Error,
95 } 100 }
96 } 101 }
97 102
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index 212448d41..d5e116526 100644
--- a/crates/ra_ide_api/src/call_info.rs
+++ b/crates/ra_ide_api/src/call_info.rs
@@ -91,8 +91,8 @@ impl FnCallNode {
91 91
92 fn name_ref(&self) -> Option<ast::NameRef> { 92 fn name_ref(&self) -> Option<ast::NameRef> {
93 match self { 93 match self {
94 FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()?.kind() { 94 FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
95 ast::ExprKind::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?, 95 ast::Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
96 _ => return None, 96 _ => return None,
97 }), 97 }),
98 98
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs
index 638f4cbde..c5620dd52 100644
--- a/crates/ra_ide_api/src/impls.rs
+++ b/crates/ra_ide_api/src/impls.rs
@@ -33,13 +33,11 @@ fn impls_for_def(
33 node: &ast::NominalDef, 33 node: &ast::NominalDef,
34 module: hir::Module, 34 module: hir::Module,
35) -> Option<Vec<NavigationTarget>> { 35) -> Option<Vec<NavigationTarget>> {
36 let ty = match node.kind() { 36 let ty = match node {
37 ast::NominalDefKind::StructDef(def) => { 37 ast::NominalDef::StructDef(def) => {
38 source_binder::struct_from_module(db, module, &def).ty(db) 38 source_binder::struct_from_module(db, module, &def).ty(db)
39 } 39 }
40 ast::NominalDefKind::EnumDef(def) => { 40 ast::NominalDef::EnumDef(def) => source_binder::enum_from_module(db, module, &def).ty(db),
41 source_binder::enum_from_module(db, module, &def).ty(db)
42 }
43 }; 41 };
44 42
45 let krate = module.krate(db)?; 43 let krate = module.krate(db)?;
diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs
index 0b3c96d26..058cd68e2 100644
--- a/crates/ra_ide_api/src/inlay_hints.rs
+++ b/crates/ra_ide_api/src/inlay_hints.rs
@@ -3,7 +3,7 @@ use hir::{HirDisplay, SourceAnalyzer, Ty};
3use ra_syntax::{ 3use ra_syntax::{
4 algo::visit::{visitor, Visitor}, 4 algo::visit::{visitor, Visitor},
5 ast::{ 5 ast::{
6 AstNode, ForExpr, IfExpr, LambdaExpr, LetStmt, MatchArmList, Pat, PatKind, SourceFile, 6 self, AstNode, ForExpr, IfExpr, LambdaExpr, LetStmt, MatchArmList, SourceFile,
7 TypeAscriptionOwner, WhileExpr, 7 TypeAscriptionOwner, WhileExpr,
8 }, 8 },
9 SmolStr, SyntaxKind, SyntaxNode, TextRange, 9 SmolStr, SyntaxKind, SyntaxNode, TextRange,
@@ -88,7 +88,7 @@ fn get_inlay_hints(
88fn get_pat_type_hints( 88fn get_pat_type_hints(
89 db: &RootDatabase, 89 db: &RootDatabase,
90 analyzer: &SourceAnalyzer, 90 analyzer: &SourceAnalyzer,
91 root_pat: Pat, 91 root_pat: ast::Pat,
92 skip_root_pat_hint: bool, 92 skip_root_pat_hint: bool,
93) -> Vec<InlayHint> { 93) -> Vec<InlayHint> {
94 let original_pat = &root_pat.clone(); 94 let original_pat = &root_pat.clone();
@@ -108,27 +108,27 @@ fn get_pat_type_hints(
108 .collect() 108 .collect()
109} 109}
110 110
111fn get_leaf_pats(root_pat: Pat) -> Vec<Pat> { 111fn get_leaf_pats(root_pat: ast::Pat) -> Vec<ast::Pat> {
112 let mut pats_to_process = std::collections::VecDeque::<Pat>::new(); 112 let mut pats_to_process = std::collections::VecDeque::<ast::Pat>::new();
113 pats_to_process.push_back(root_pat); 113 pats_to_process.push_back(root_pat);
114 114
115 let mut leaf_pats = Vec::new(); 115 let mut leaf_pats = Vec::new();
116 116
117 while let Some(maybe_leaf_pat) = pats_to_process.pop_front() { 117 while let Some(maybe_leaf_pat) = pats_to_process.pop_front() {
118 match maybe_leaf_pat.kind() { 118 match &maybe_leaf_pat {
119 PatKind::BindPat(bind_pat) => { 119 ast::Pat::BindPat(bind_pat) => {
120 if let Some(pat) = bind_pat.pat() { 120 if let Some(pat) = bind_pat.pat() {
121 pats_to_process.push_back(pat); 121 pats_to_process.push_back(pat);
122 } else { 122 } else {
123 leaf_pats.push(maybe_leaf_pat); 123 leaf_pats.push(maybe_leaf_pat);
124 } 124 }
125 } 125 }
126 PatKind::TuplePat(tuple_pat) => { 126 ast::Pat::TuplePat(tuple_pat) => {
127 for arg_pat in tuple_pat.args() { 127 for arg_pat in tuple_pat.args() {
128 pats_to_process.push_back(arg_pat); 128 pats_to_process.push_back(arg_pat);
129 } 129 }
130 } 130 }
131 PatKind::StructPat(struct_pat) => { 131 ast::Pat::StructPat(struct_pat) => {
132 if let Some(pat_list) = struct_pat.field_pat_list() { 132 if let Some(pat_list) = struct_pat.field_pat_list() {
133 pats_to_process.extend( 133 pats_to_process.extend(
134 pat_list 134 pat_list
@@ -139,12 +139,12 @@ fn get_leaf_pats(root_pat: Pat) -> Vec<Pat> {
139 .filter(|pat| pat.syntax().kind() != SyntaxKind::BIND_PAT) 139 .filter(|pat| pat.syntax().kind() != SyntaxKind::BIND_PAT)
140 }) 140 })
141 .chain(pat_list.bind_pats().map(|bind_pat| { 141 .chain(pat_list.bind_pats().map(|bind_pat| {
142 bind_pat.pat().unwrap_or_else(|| Pat::from(bind_pat)) 142 bind_pat.pat().unwrap_or_else(|| ast::Pat::from(bind_pat))
143 })), 143 })),
144 ); 144 );
145 } 145 }
146 } 146 }
147 PatKind::TupleStructPat(tuple_struct_pat) => { 147 ast::Pat::TupleStructPat(tuple_struct_pat) => {
148 for arg_pat in tuple_struct_pat.args() { 148 for arg_pat in tuple_struct_pat.args() {
149 pats_to_process.push_back(arg_pat); 149 pats_to_process.push_back(arg_pat);
150 } 150 }
@@ -158,7 +158,7 @@ fn get_leaf_pats(root_pat: Pat) -> Vec<Pat> {
158fn get_node_displayable_type( 158fn get_node_displayable_type(
159 db: &RootDatabase, 159 db: &RootDatabase,
160 analyzer: &SourceAnalyzer, 160 analyzer: &SourceAnalyzer,
161 node_pat: &Pat, 161 node_pat: &ast::Pat,
162) -> Option<Ty> { 162) -> Option<Ty> {
163 analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| { 163 analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| {
164 if let Ty::Apply(_) = resolved_type { 164 if let Ty::Apply(_) = resolved_type {
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index 2118e7ad3..342e73fd3 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -75,7 +75,7 @@ pub(crate) fn find_all_refs(
75 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); 75 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
76 let resolved = analyzer.resolve_local_name(&name_ref)?; 76 let resolved = analyzer.resolve_local_name(&name_ref)?;
77 if let Either::A(ptr) = resolved.ptr() { 77 if let Either::A(ptr) = resolved.ptr() {
78 if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file.syntax()).kind() { 78 if let ast::Pat::BindPat(binding) = ptr.to_node(source_file.syntax()) {
79 return Some((binding, analyzer)); 79 return Some((binding, analyzer));
80 } 80 }
81 } 81 }
diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs
index 09c082de9..9ea96909e 100644
--- a/crates/ra_ide_api/src/runnables.rs
+++ b/crates/ra_ide_api/src/runnables.rs
@@ -54,8 +54,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti
54 let has_test_function = module 54 let has_test_function = module
55 .item_list()? 55 .item_list()?
56 .items() 56 .items()
57 .filter_map(|it| match it.kind() { 57 .filter_map(|it| match it {
58 ast::ModuleItemKind::FnDef(it) => Some(it), 58 ast::ModuleItem::FnDef(it) => Some(it),
59 _ => None, 59 _ => None,
60 }) 60 })
61 .any(|f| f.has_atom_attr("test")); 61 .any(|f| f.has_atom_attr("test"));
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 016668380..9c5789701 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -591,34 +591,7 @@ impl EnumVariantList {
591 } 591 }
592} 592}
593#[derive(Debug, Clone, PartialEq, Eq, Hash)] 593#[derive(Debug, Clone, PartialEq, Eq, Hash)]
594pub struct Expr { 594pub enum Expr {
595 pub(crate) syntax: SyntaxNode,
596}
597impl AstNode for Expr {
598 fn can_cast(kind: SyntaxKind) -> bool {
599 match kind {
600 TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR
601 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL
602 | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | STRUCT_LIT | CALL_EXPR | INDEX_EXPR
603 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR
604 | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL => {
605 true
606 }
607 _ => false,
608 }
609 }
610 fn cast(syntax: SyntaxNode) -> Option<Self> {
611 if Self::can_cast(syntax.kind()) {
612 Some(Self { syntax })
613 } else {
614 None
615 }
616 }
617 fn syntax(&self) -> &SyntaxNode {
618 &self.syntax
619 }
620}
621pub enum ExprKind {
622 TupleExpr(TupleExpr), 595 TupleExpr(TupleExpr),
623 ArrayExpr(ArrayExpr), 596 ArrayExpr(ArrayExpr),
624 ParenExpr(ParenExpr), 597 ParenExpr(ParenExpr),
@@ -652,189 +625,235 @@ pub enum ExprKind {
652} 625}
653impl From<TupleExpr> for Expr { 626impl From<TupleExpr> for Expr {
654 fn from(node: TupleExpr) -> Expr { 627 fn from(node: TupleExpr) -> Expr {
655 Expr { syntax: node.syntax } 628 Expr::TupleExpr(node)
656 } 629 }
657} 630}
658impl From<ArrayExpr> for Expr { 631impl From<ArrayExpr> for Expr {
659 fn from(node: ArrayExpr) -> Expr { 632 fn from(node: ArrayExpr) -> Expr {
660 Expr { syntax: node.syntax } 633 Expr::ArrayExpr(node)
661 } 634 }
662} 635}
663impl From<ParenExpr> for Expr { 636impl From<ParenExpr> for Expr {
664 fn from(node: ParenExpr) -> Expr { 637 fn from(node: ParenExpr) -> Expr {
665 Expr { syntax: node.syntax } 638 Expr::ParenExpr(node)
666 } 639 }
667} 640}
668impl From<PathExpr> for Expr { 641impl From<PathExpr> for Expr {
669 fn from(node: PathExpr) -> Expr { 642 fn from(node: PathExpr) -> Expr {
670 Expr { syntax: node.syntax } 643 Expr::PathExpr(node)
671 } 644 }
672} 645}
673impl From<LambdaExpr> for Expr { 646impl From<LambdaExpr> for Expr {
674 fn from(node: LambdaExpr) -> Expr { 647 fn from(node: LambdaExpr) -> Expr {
675 Expr { syntax: node.syntax } 648 Expr::LambdaExpr(node)
676 } 649 }
677} 650}
678impl From<IfExpr> for Expr { 651impl From<IfExpr> for Expr {
679 fn from(node: IfExpr) -> Expr { 652 fn from(node: IfExpr) -> Expr {
680 Expr { syntax: node.syntax } 653 Expr::IfExpr(node)
681 } 654 }
682} 655}
683impl From<LoopExpr> for Expr { 656impl From<LoopExpr> for Expr {
684 fn from(node: LoopExpr) -> Expr { 657 fn from(node: LoopExpr) -> Expr {
685 Expr { syntax: node.syntax } 658 Expr::LoopExpr(node)
686 } 659 }
687} 660}
688impl From<ForExpr> for Expr { 661impl From<ForExpr> for Expr {
689 fn from(node: ForExpr) -> Expr { 662 fn from(node: ForExpr) -> Expr {
690 Expr { syntax: node.syntax } 663 Expr::ForExpr(node)
691 } 664 }
692} 665}
693impl From<WhileExpr> for Expr { 666impl From<WhileExpr> for Expr {
694 fn from(node: WhileExpr) -> Expr { 667 fn from(node: WhileExpr) -> Expr {
695 Expr { syntax: node.syntax } 668 Expr::WhileExpr(node)
696 } 669 }
697} 670}
698impl From<ContinueExpr> for Expr { 671impl From<ContinueExpr> for Expr {
699 fn from(node: ContinueExpr) -> Expr { 672 fn from(node: ContinueExpr) -> Expr {
700 Expr { syntax: node.syntax } 673 Expr::ContinueExpr(node)
701 } 674 }
702} 675}
703impl From<BreakExpr> for Expr { 676impl From<BreakExpr> for Expr {
704 fn from(node: BreakExpr) -> Expr { 677 fn from(node: BreakExpr) -> Expr {
705 Expr { syntax: node.syntax } 678 Expr::BreakExpr(node)
706 } 679 }
707} 680}
708impl From<Label> for Expr { 681impl From<Label> for Expr {
709 fn from(node: Label) -> Expr { 682 fn from(node: Label) -> Expr {
710 Expr { syntax: node.syntax } 683 Expr::Label(node)
711 } 684 }
712} 685}
713impl From<BlockExpr> for Expr { 686impl From<BlockExpr> for Expr {
714 fn from(node: BlockExpr) -> Expr { 687 fn from(node: BlockExpr) -> Expr {
715 Expr { syntax: node.syntax } 688 Expr::BlockExpr(node)
716 } 689 }
717} 690}
718impl From<ReturnExpr> for Expr { 691impl From<ReturnExpr> for Expr {
719 fn from(node: ReturnExpr) -> Expr { 692 fn from(node: ReturnExpr) -> Expr {
720 Expr { syntax: node.syntax } 693 Expr::ReturnExpr(node)
721 } 694 }
722} 695}
723impl From<MatchExpr> for Expr { 696impl From<MatchExpr> for Expr {
724 fn from(node: MatchExpr) -> Expr { 697 fn from(node: MatchExpr) -> Expr {
725 Expr { syntax: node.syntax } 698 Expr::MatchExpr(node)
726 } 699 }
727} 700}
728impl From<StructLit> for Expr { 701impl From<StructLit> for Expr {
729 fn from(node: StructLit) -> Expr { 702 fn from(node: StructLit) -> Expr {
730 Expr { syntax: node.syntax } 703 Expr::StructLit(node)
731 } 704 }
732} 705}
733impl From<CallExpr> for Expr { 706impl From<CallExpr> for Expr {
734 fn from(node: CallExpr) -> Expr { 707 fn from(node: CallExpr) -> Expr {
735 Expr { syntax: node.syntax } 708 Expr::CallExpr(node)
736 } 709 }
737} 710}
738impl From<IndexExpr> for Expr { 711impl From<IndexExpr> for Expr {
739 fn from(node: IndexExpr) -> Expr { 712 fn from(node: IndexExpr) -> Expr {
740 Expr { syntax: node.syntax } 713 Expr::IndexExpr(node)
741 } 714 }
742} 715}
743impl From<MethodCallExpr> for Expr { 716impl From<MethodCallExpr> for Expr {
744 fn from(node: MethodCallExpr) -> Expr { 717 fn from(node: MethodCallExpr) -> Expr {
745 Expr { syntax: node.syntax } 718 Expr::MethodCallExpr(node)
746 } 719 }
747} 720}
748impl From<FieldExpr> for Expr { 721impl From<FieldExpr> for Expr {
749 fn from(node: FieldExpr) -> Expr { 722 fn from(node: FieldExpr) -> Expr {
750 Expr { syntax: node.syntax } 723 Expr::FieldExpr(node)
751 } 724 }
752} 725}
753impl From<AwaitExpr> for Expr { 726impl From<AwaitExpr> for Expr {
754 fn from(node: AwaitExpr) -> Expr { 727 fn from(node: AwaitExpr) -> Expr {
755 Expr { syntax: node.syntax } 728 Expr::AwaitExpr(node)
756 } 729 }
757} 730}
758impl From<TryExpr> for Expr { 731impl From<TryExpr> for Expr {
759 fn from(node: TryExpr) -> Expr { 732 fn from(node: TryExpr) -> Expr {
760 Expr { syntax: node.syntax } 733 Expr::TryExpr(node)
761 } 734 }
762} 735}
763impl From<TryBlockExpr> for Expr { 736impl From<TryBlockExpr> for Expr {
764 fn from(node: TryBlockExpr) -> Expr { 737 fn from(node: TryBlockExpr) -> Expr {
765 Expr { syntax: node.syntax } 738 Expr::TryBlockExpr(node)
766 } 739 }
767} 740}
768impl From<CastExpr> for Expr { 741impl From<CastExpr> for Expr {
769 fn from(node: CastExpr) -> Expr { 742 fn from(node: CastExpr) -> Expr {
770 Expr { syntax: node.syntax } 743 Expr::CastExpr(node)
771 } 744 }
772} 745}
773impl From<RefExpr> for Expr { 746impl From<RefExpr> for Expr {
774 fn from(node: RefExpr) -> Expr { 747 fn from(node: RefExpr) -> Expr {
775 Expr { syntax: node.syntax } 748 Expr::RefExpr(node)
776 } 749 }
777} 750}
778impl From<PrefixExpr> for Expr { 751impl From<PrefixExpr> for Expr {
779 fn from(node: PrefixExpr) -> Expr { 752 fn from(node: PrefixExpr) -> Expr {
780 Expr { syntax: node.syntax } 753 Expr::PrefixExpr(node)
781 } 754 }
782} 755}
783impl From<RangeExpr> for Expr { 756impl From<RangeExpr> for Expr {
784 fn from(node: RangeExpr) -> Expr { 757 fn from(node: RangeExpr) -> Expr {
785 Expr { syntax: node.syntax } 758 Expr::RangeExpr(node)
786 } 759 }
787} 760}
788impl From<BinExpr> for Expr { 761impl From<BinExpr> for Expr {
789 fn from(node: BinExpr) -> Expr { 762 fn from(node: BinExpr) -> Expr {
790 Expr { syntax: node.syntax } 763 Expr::BinExpr(node)
791 } 764 }
792} 765}
793impl From<Literal> for Expr { 766impl From<Literal> for Expr {
794 fn from(node: Literal) -> Expr { 767 fn from(node: Literal) -> Expr {
795 Expr { syntax: node.syntax } 768 Expr::Literal(node)
796 } 769 }
797} 770}
798impl From<MacroCall> for Expr { 771impl From<MacroCall> for Expr {
799 fn from(node: MacroCall) -> Expr { 772 fn from(node: MacroCall) -> Expr {
800 Expr { syntax: node.syntax } 773 Expr::MacroCall(node)
801 } 774 }
802} 775}
803impl Expr { 776impl AstNode for Expr {
804 pub fn kind(&self) -> ExprKind { 777 fn can_cast(kind: SyntaxKind) -> bool {
805 let syntax = self.syntax.clone(); 778 match kind {
806 match syntax.kind() { 779 TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR
807 TUPLE_EXPR => ExprKind::TupleExpr(TupleExpr { syntax }), 780 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL
808 ARRAY_EXPR => ExprKind::ArrayExpr(ArrayExpr { syntax }), 781 | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | STRUCT_LIT | CALL_EXPR | INDEX_EXPR
809 PAREN_EXPR => ExprKind::ParenExpr(ParenExpr { syntax }), 782 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR
810 PATH_EXPR => ExprKind::PathExpr(PathExpr { syntax }), 783 | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL => {
811 LAMBDA_EXPR => ExprKind::LambdaExpr(LambdaExpr { syntax }), 784 true
812 IF_EXPR => ExprKind::IfExpr(IfExpr { syntax }), 785 }
813 LOOP_EXPR => ExprKind::LoopExpr(LoopExpr { syntax }), 786 _ => false,
814 FOR_EXPR => ExprKind::ForExpr(ForExpr { syntax }), 787 }
815 WHILE_EXPR => ExprKind::WhileExpr(WhileExpr { syntax }), 788 }
816 CONTINUE_EXPR => ExprKind::ContinueExpr(ContinueExpr { syntax }), 789 fn cast(syntax: SyntaxNode) -> Option<Self> {
817 BREAK_EXPR => ExprKind::BreakExpr(BreakExpr { syntax }), 790 let res = match syntax.kind() {
818 LABEL => ExprKind::Label(Label { syntax }), 791 TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }),
819 BLOCK_EXPR => ExprKind::BlockExpr(BlockExpr { syntax }), 792 ARRAY_EXPR => Expr::ArrayExpr(ArrayExpr { syntax }),
820 RETURN_EXPR => ExprKind::ReturnExpr(ReturnExpr { syntax }), 793 PAREN_EXPR => Expr::ParenExpr(ParenExpr { syntax }),
821 MATCH_EXPR => ExprKind::MatchExpr(MatchExpr { syntax }), 794 PATH_EXPR => Expr::PathExpr(PathExpr { syntax }),
822 STRUCT_LIT => ExprKind::StructLit(StructLit { syntax }), 795 LAMBDA_EXPR => Expr::LambdaExpr(LambdaExpr { syntax }),
823 CALL_EXPR => ExprKind::CallExpr(CallExpr { syntax }), 796 IF_EXPR => Expr::IfExpr(IfExpr { syntax }),
824 INDEX_EXPR => ExprKind::IndexExpr(IndexExpr { syntax }), 797 LOOP_EXPR => Expr::LoopExpr(LoopExpr { syntax }),
825 METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr { syntax }), 798 FOR_EXPR => Expr::ForExpr(ForExpr { syntax }),
826 FIELD_EXPR => ExprKind::FieldExpr(FieldExpr { syntax }), 799 WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }),
827 AWAIT_EXPR => ExprKind::AwaitExpr(AwaitExpr { syntax }), 800 CONTINUE_EXPR => Expr::ContinueExpr(ContinueExpr { syntax }),
828 TRY_EXPR => ExprKind::TryExpr(TryExpr { syntax }), 801 BREAK_EXPR => Expr::BreakExpr(BreakExpr { syntax }),
829 TRY_BLOCK_EXPR => ExprKind::TryBlockExpr(TryBlockExpr { syntax }), 802 LABEL => Expr::Label(Label { syntax }),
830 CAST_EXPR => ExprKind::CastExpr(CastExpr { syntax }), 803 BLOCK_EXPR => Expr::BlockExpr(BlockExpr { syntax }),
831 REF_EXPR => ExprKind::RefExpr(RefExpr { syntax }), 804 RETURN_EXPR => Expr::ReturnExpr(ReturnExpr { syntax }),
832 PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr { syntax }), 805 MATCH_EXPR => Expr::MatchExpr(MatchExpr { syntax }),
833 RANGE_EXPR => ExprKind::RangeExpr(RangeExpr { syntax }), 806 STRUCT_LIT => Expr::StructLit(StructLit { syntax }),
834 BIN_EXPR => ExprKind::BinExpr(BinExpr { syntax }), 807 CALL_EXPR => Expr::CallExpr(CallExpr { syntax }),
835 LITERAL => ExprKind::Literal(Literal { syntax }), 808 INDEX_EXPR => Expr::IndexExpr(IndexExpr { syntax }),
836 MACRO_CALL => ExprKind::MacroCall(MacroCall { syntax }), 809 METHOD_CALL_EXPR => Expr::MethodCallExpr(MethodCallExpr { syntax }),
837 _ => unreachable!(), 810 FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }),
811 AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }),
812 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }),
813 TRY_BLOCK_EXPR => Expr::TryBlockExpr(TryBlockExpr { syntax }),
814 CAST_EXPR => Expr::CastExpr(CastExpr { syntax }),
815 REF_EXPR => Expr::RefExpr(RefExpr { syntax }),
816 PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }),
817 RANGE_EXPR => Expr::RangeExpr(RangeExpr { syntax }),
818 BIN_EXPR => Expr::BinExpr(BinExpr { syntax }),
819 LITERAL => Expr::Literal(Literal { syntax }),
820 MACRO_CALL => Expr::MacroCall(MacroCall { syntax }),
821 _ => return None,
822 };
823 Some(res)
824 }
825 fn syntax(&self) -> &SyntaxNode {
826 match self {
827 Expr::TupleExpr(it) => &it.syntax,
828 Expr::ArrayExpr(it) => &it.syntax,
829 Expr::ParenExpr(it) => &it.syntax,
830 Expr::PathExpr(it) => &it.syntax,
831 Expr::LambdaExpr(it) => &it.syntax,
832 Expr::IfExpr(it) => &it.syntax,
833 Expr::LoopExpr(it) => &it.syntax,
834 Expr::ForExpr(it) => &it.syntax,
835 Expr::WhileExpr(it) => &it.syntax,
836 Expr::ContinueExpr(it) => &it.syntax,
837 Expr::BreakExpr(it) => &it.syntax,
838 Expr::Label(it) => &it.syntax,
839 Expr::BlockExpr(it) => &it.syntax,
840 Expr::ReturnExpr(it) => &it.syntax,
841 Expr::MatchExpr(it) => &it.syntax,
842 Expr::StructLit(it) => &it.syntax,
843 Expr::CallExpr(it) => &it.syntax,
844 Expr::IndexExpr(it) => &it.syntax,
845 Expr::MethodCallExpr(it) => &it.syntax,
846 Expr::FieldExpr(it) => &it.syntax,
847 Expr::AwaitExpr(it) => &it.syntax,
848 Expr::TryExpr(it) => &it.syntax,
849 Expr::TryBlockExpr(it) => &it.syntax,
850 Expr::CastExpr(it) => &it.syntax,
851 Expr::RefExpr(it) => &it.syntax,
852 Expr::PrefixExpr(it) => &it.syntax,
853 Expr::RangeExpr(it) => &it.syntax,
854 Expr::BinExpr(it) => &it.syntax,
855 Expr::Literal(it) => &it.syntax,
856 Expr::MacroCall(it) => &it.syntax,
838 } 857 }
839 } 858 }
840} 859}
@@ -1167,55 +1186,47 @@ impl ImplBlock {
1167 } 1186 }
1168} 1187}
1169#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1188#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1170pub struct ImplItem { 1189pub enum ImplItem {
1171 pub(crate) syntax: SyntaxNode,
1172}
1173impl AstNode for ImplItem {
1174 fn can_cast(kind: SyntaxKind) -> bool {
1175 match kind {
1176 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
1177 _ => false,
1178 }
1179 }
1180 fn cast(syntax: SyntaxNode) -> Option<Self> {
1181 if Self::can_cast(syntax.kind()) {
1182 Some(Self { syntax })
1183 } else {
1184 None
1185 }
1186 }
1187 fn syntax(&self) -> &SyntaxNode {
1188 &self.syntax
1189 }
1190}
1191pub enum ImplItemKind {
1192 FnDef(FnDef), 1190 FnDef(FnDef),
1193 TypeAliasDef(TypeAliasDef), 1191 TypeAliasDef(TypeAliasDef),
1194 ConstDef(ConstDef), 1192 ConstDef(ConstDef),
1195} 1193}
1196impl From<FnDef> for ImplItem { 1194impl From<FnDef> for ImplItem {
1197 fn from(node: FnDef) -> ImplItem { 1195 fn from(node: FnDef) -> ImplItem {
1198 ImplItem { syntax: node.syntax } 1196 ImplItem::FnDef(node)
1199 } 1197 }
1200} 1198}
1201impl From<TypeAliasDef> for ImplItem { 1199impl From<TypeAliasDef> for ImplItem {
1202 fn from(node: TypeAliasDef) -> ImplItem { 1200 fn from(node: TypeAliasDef) -> ImplItem {
1203 ImplItem { syntax: node.syntax } 1201 ImplItem::TypeAliasDef(node)
1204 } 1202 }
1205} 1203}
1206impl From<ConstDef> for ImplItem { 1204impl From<ConstDef> for ImplItem {
1207 fn from(node: ConstDef) -> ImplItem { 1205 fn from(node: ConstDef) -> ImplItem {
1208 ImplItem { syntax: node.syntax } 1206 ImplItem::ConstDef(node)
1209 } 1207 }
1210} 1208}
1211impl ImplItem { 1209impl AstNode for ImplItem {
1212 pub fn kind(&self) -> ImplItemKind { 1210 fn can_cast(kind: SyntaxKind) -> bool {
1213 let syntax = self.syntax.clone(); 1211 match kind {
1214 match syntax.kind() { 1212 FN_DEF | TYPE_ALIAS_DEF | CONST_DEF => true,
1215 FN_DEF => ImplItemKind::FnDef(FnDef { syntax }), 1213 _ => false,
1216 TYPE_ALIAS_DEF => ImplItemKind::TypeAliasDef(TypeAliasDef { syntax }), 1214 }
1217 CONST_DEF => ImplItemKind::ConstDef(ConstDef { syntax }), 1215 }
1218 _ => unreachable!(), 1216 fn cast(syntax: SyntaxNode) -> Option<Self> {
1217 let res = match syntax.kind() {
1218 FN_DEF => ImplItem::FnDef(FnDef { syntax }),
1219 TYPE_ALIAS_DEF => ImplItem::TypeAliasDef(TypeAliasDef { syntax }),
1220 CONST_DEF => ImplItem::ConstDef(ConstDef { syntax }),
1221 _ => return None,
1222 };
1223 Some(res)
1224 }
1225 fn syntax(&self) -> &SyntaxNode {
1226 match self {
1227 ImplItem::FnDef(it) => &it.syntax,
1228 ImplItem::TypeAliasDef(it) => &it.syntax,
1229 ImplItem::ConstDef(it) => &it.syntax,
1219 } 1230 }
1220 } 1231 }
1221} 1232}
@@ -1774,29 +1785,7 @@ impl Module {
1774 } 1785 }
1775} 1786}
1776#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1787#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1777pub struct ModuleItem { 1788pub enum ModuleItem {
1778 pub(crate) syntax: SyntaxNode,
1779}
1780impl AstNode for ModuleItem {
1781 fn can_cast(kind: SyntaxKind) -> bool {
1782 match kind {
1783 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_BLOCK | USE_ITEM
1784 | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
1785 _ => false,
1786 }
1787 }
1788 fn cast(syntax: SyntaxNode) -> Option<Self> {
1789 if Self::can_cast(syntax.kind()) {
1790 Some(Self { syntax })
1791 } else {
1792 None
1793 }
1794 }
1795 fn syntax(&self) -> &SyntaxNode {
1796 &self.syntax
1797 }
1798}
1799pub enum ModuleItemKind {
1800 StructDef(StructDef), 1789 StructDef(StructDef),
1801 EnumDef(EnumDef), 1790 EnumDef(EnumDef),
1802 FnDef(FnDef), 1791 FnDef(FnDef),
@@ -1811,75 +1800,97 @@ pub enum ModuleItemKind {
1811} 1800}
1812impl From<StructDef> for ModuleItem { 1801impl From<StructDef> for ModuleItem {
1813 fn from(node: StructDef) -> ModuleItem { 1802 fn from(node: StructDef) -> ModuleItem {
1814 ModuleItem { syntax: node.syntax } 1803 ModuleItem::StructDef(node)
1815 } 1804 }
1816} 1805}
1817impl From<EnumDef> for ModuleItem { 1806impl From<EnumDef> for ModuleItem {
1818 fn from(node: EnumDef) -> ModuleItem { 1807 fn from(node: EnumDef) -> ModuleItem {
1819 ModuleItem { syntax: node.syntax } 1808 ModuleItem::EnumDef(node)
1820 } 1809 }
1821} 1810}
1822impl From<FnDef> for ModuleItem { 1811impl From<FnDef> for ModuleItem {
1823 fn from(node: FnDef) -> ModuleItem { 1812 fn from(node: FnDef) -> ModuleItem {
1824 ModuleItem { syntax: node.syntax } 1813 ModuleItem::FnDef(node)
1825 } 1814 }
1826} 1815}
1827impl From<TraitDef> for ModuleItem { 1816impl From<TraitDef> for ModuleItem {
1828 fn from(node: TraitDef) -> ModuleItem { 1817 fn from(node: TraitDef) -> ModuleItem {
1829 ModuleItem { syntax: node.syntax } 1818 ModuleItem::TraitDef(node)
1830 } 1819 }
1831} 1820}
1832impl From<TypeAliasDef> for ModuleItem { 1821impl From<TypeAliasDef> for ModuleItem {
1833 fn from(node: TypeAliasDef) -> ModuleItem { 1822 fn from(node: TypeAliasDef) -> ModuleItem {
1834 ModuleItem { syntax: node.syntax } 1823 ModuleItem::TypeAliasDef(node)
1835 } 1824 }
1836} 1825}
1837impl From<ImplBlock> for ModuleItem { 1826impl From<ImplBlock> for ModuleItem {
1838 fn from(node: ImplBlock) -> ModuleItem { 1827 fn from(node: ImplBlock) -> ModuleItem {
1839 ModuleItem { syntax: node.syntax } 1828 ModuleItem::ImplBlock(node)
1840 } 1829 }
1841} 1830}
1842impl From<UseItem> for ModuleItem { 1831impl From<UseItem> for ModuleItem {
1843 fn from(node: UseItem) -> ModuleItem { 1832 fn from(node: UseItem) -> ModuleItem {
1844 ModuleItem { syntax: node.syntax } 1833 ModuleItem::UseItem(node)
1845 } 1834 }
1846} 1835}
1847impl From<ExternCrateItem> for ModuleItem { 1836impl From<ExternCrateItem> for ModuleItem {
1848 fn from(node: ExternCrateItem) -> ModuleItem { 1837 fn from(node: ExternCrateItem) -> ModuleItem {
1849 ModuleItem { syntax: node.syntax } 1838 ModuleItem::ExternCrateItem(node)
1850 } 1839 }
1851} 1840}
1852impl From<ConstDef> for ModuleItem { 1841impl From<ConstDef> for ModuleItem {
1853 fn from(node: ConstDef) -> ModuleItem { 1842 fn from(node: ConstDef) -> ModuleItem {
1854 ModuleItem { syntax: node.syntax } 1843 ModuleItem::ConstDef(node)
1855 } 1844 }
1856} 1845}
1857impl From<StaticDef> for ModuleItem { 1846impl From<StaticDef> for ModuleItem {
1858 fn from(node: StaticDef) -> ModuleItem { 1847 fn from(node: StaticDef) -> ModuleItem {
1859 ModuleItem { syntax: node.syntax } 1848 ModuleItem::StaticDef(node)
1860 } 1849 }
1861} 1850}
1862impl From<Module> for ModuleItem { 1851impl From<Module> for ModuleItem {
1863 fn from(node: Module) -> ModuleItem { 1852 fn from(node: Module) -> ModuleItem {
1864 ModuleItem { syntax: node.syntax } 1853 ModuleItem::Module(node)
1865 } 1854 }
1866} 1855}
1867impl ModuleItem { 1856impl AstNode for ModuleItem {
1868 pub fn kind(&self) -> ModuleItemKind { 1857 fn can_cast(kind: SyntaxKind) -> bool {
1869 let syntax = self.syntax.clone(); 1858 match kind {
1870 match syntax.kind() { 1859 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_BLOCK | USE_ITEM
1871 STRUCT_DEF => ModuleItemKind::StructDef(StructDef { syntax }), 1860 | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true,
1872 ENUM_DEF => ModuleItemKind::EnumDef(EnumDef { syntax }), 1861 _ => false,
1873 FN_DEF => ModuleItemKind::FnDef(FnDef { syntax }), 1862 }
1874 TRAIT_DEF => ModuleItemKind::TraitDef(TraitDef { syntax }), 1863 }
1875 TYPE_ALIAS_DEF => ModuleItemKind::TypeAliasDef(TypeAliasDef { syntax }), 1864 fn cast(syntax: SyntaxNode) -> Option<Self> {
1876 IMPL_BLOCK => ModuleItemKind::ImplBlock(ImplBlock { syntax }), 1865 let res = match syntax.kind() {
1877 USE_ITEM => ModuleItemKind::UseItem(UseItem { syntax }), 1866 STRUCT_DEF => ModuleItem::StructDef(StructDef { syntax }),
1878 EXTERN_CRATE_ITEM => ModuleItemKind::ExternCrateItem(ExternCrateItem { syntax }), 1867 ENUM_DEF => ModuleItem::EnumDef(EnumDef { syntax }),
1879 CONST_DEF => ModuleItemKind::ConstDef(ConstDef { syntax }), 1868 FN_DEF => ModuleItem::FnDef(FnDef { syntax }),
1880 STATIC_DEF => ModuleItemKind::StaticDef(StaticDef { syntax }), 1869 TRAIT_DEF => ModuleItem::TraitDef(TraitDef { syntax }),
1881 MODULE => ModuleItemKind::Module(Module { syntax }), 1870 TYPE_ALIAS_DEF => ModuleItem::TypeAliasDef(TypeAliasDef { syntax }),
1882 _ => unreachable!(), 1871 IMPL_BLOCK => ModuleItem::ImplBlock(ImplBlock { syntax }),
1872 USE_ITEM => ModuleItem::UseItem(UseItem { syntax }),
1873 EXTERN_CRATE_ITEM => ModuleItem::ExternCrateItem(ExternCrateItem { syntax }),
1874 CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }),
1875 STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }),
1876 MODULE => ModuleItem::Module(Module { syntax }),
1877 _ => return None,
1878 };
1879 Some(res)
1880 }
1881 fn syntax(&self) -> &SyntaxNode {
1882 match self {
1883 ModuleItem::StructDef(it) => &it.syntax,
1884 ModuleItem::EnumDef(it) => &it.syntax,
1885 ModuleItem::FnDef(it) => &it.syntax,
1886 ModuleItem::TraitDef(it) => &it.syntax,
1887 ModuleItem::TypeAliasDef(it) => &it.syntax,
1888 ModuleItem::ImplBlock(it) => &it.syntax,
1889 ModuleItem::UseItem(it) => &it.syntax,
1890 ModuleItem::ExternCrateItem(it) => &it.syntax,
1891 ModuleItem::ConstDef(it) => &it.syntax,
1892 ModuleItem::StaticDef(it) => &it.syntax,
1893 ModuleItem::Module(it) => &it.syntax,
1883 } 1894 }
1884 } 1895 }
1885} 1896}
@@ -2069,48 +2080,39 @@ impl AstNode for NeverType {
2069} 2080}
2070impl NeverType {} 2081impl NeverType {}
2071#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2082#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2072pub struct NominalDef { 2083pub enum NominalDef {
2073 pub(crate) syntax: SyntaxNode,
2074}
2075impl AstNode for NominalDef {
2076 fn can_cast(kind: SyntaxKind) -> bool {
2077 match kind {
2078 STRUCT_DEF | ENUM_DEF => true,
2079 _ => false,
2080 }
2081 }
2082 fn cast(syntax: SyntaxNode) -> Option<Self> {
2083 if Self::can_cast(syntax.kind()) {
2084 Some(Self { syntax })
2085 } else {
2086 None
2087 }
2088 }
2089 fn syntax(&self) -> &SyntaxNode {
2090 &self.syntax
2091 }
2092}
2093pub enum NominalDefKind {
2094 StructDef(StructDef), 2084 StructDef(StructDef),
2095 EnumDef(EnumDef), 2085 EnumDef(EnumDef),
2096} 2086}
2097impl From<StructDef> for NominalDef { 2087impl From<StructDef> for NominalDef {
2098 fn from(node: StructDef) -> NominalDef { 2088 fn from(node: StructDef) -> NominalDef {
2099 NominalDef { syntax: node.syntax } 2089 NominalDef::StructDef(node)
2100 } 2090 }
2101} 2091}
2102impl From<EnumDef> for NominalDef { 2092impl From<EnumDef> for NominalDef {
2103 fn from(node: EnumDef) -> NominalDef { 2093 fn from(node: EnumDef) -> NominalDef {
2104 NominalDef { syntax: node.syntax } 2094 NominalDef::EnumDef(node)
2105 } 2095 }
2106} 2096}
2107impl NominalDef { 2097impl AstNode for NominalDef {
2108 pub fn kind(&self) -> NominalDefKind { 2098 fn can_cast(kind: SyntaxKind) -> bool {
2109 let syntax = self.syntax.clone(); 2099 match kind {
2110 match syntax.kind() { 2100 STRUCT_DEF | ENUM_DEF => true,
2111 STRUCT_DEF => NominalDefKind::StructDef(StructDef { syntax }), 2101 _ => false,
2112 ENUM_DEF => NominalDefKind::EnumDef(EnumDef { syntax }), 2102 }
2113 _ => unreachable!(), 2103 }
2104 fn cast(syntax: SyntaxNode) -> Option<Self> {
2105 let res = match syntax.kind() {
2106 STRUCT_DEF => NominalDef::StructDef(StructDef { syntax }),
2107 ENUM_DEF => NominalDef::EnumDef(EnumDef { syntax }),
2108 _ => return None,
2109 };
2110 Some(res)
2111 }
2112 fn syntax(&self) -> &SyntaxNode {
2113 match self {
2114 NominalDef::StructDef(it) => &it.syntax,
2115 NominalDef::EnumDef(it) => &it.syntax,
2114 } 2116 }
2115 } 2117 }
2116} 2118}
@@ -2232,29 +2234,7 @@ impl ParenType {
2232 } 2234 }
2233} 2235}
2234#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2235pub struct Pat { 2237pub enum Pat {
2236 pub(crate) syntax: SyntaxNode,
2237}
2238impl AstNode for Pat {
2239 fn can_cast(kind: SyntaxKind) -> bool {
2240 match kind {
2241 REF_PAT | BIND_PAT | PLACEHOLDER_PAT | PATH_PAT | STRUCT_PAT | TUPLE_STRUCT_PAT
2242 | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
2243 _ => false,
2244 }
2245 }
2246 fn cast(syntax: SyntaxNode) -> Option<Self> {
2247 if Self::can_cast(syntax.kind()) {
2248 Some(Self { syntax })
2249 } else {
2250 None
2251 }
2252 }
2253 fn syntax(&self) -> &SyntaxNode {
2254 &self.syntax
2255 }
2256}
2257pub enum PatKind {
2258 RefPat(RefPat), 2238 RefPat(RefPat),
2259 BindPat(BindPat), 2239 BindPat(BindPat),
2260 PlaceholderPat(PlaceholderPat), 2240 PlaceholderPat(PlaceholderPat),
@@ -2268,69 +2248,90 @@ pub enum PatKind {
2268} 2248}
2269impl From<RefPat> for Pat { 2249impl From<RefPat> for Pat {
2270 fn from(node: RefPat) -> Pat { 2250 fn from(node: RefPat) -> Pat {
2271 Pat { syntax: node.syntax } 2251 Pat::RefPat(node)
2272 } 2252 }
2273} 2253}
2274impl From<BindPat> for Pat { 2254impl From<BindPat> for Pat {
2275 fn from(node: BindPat) -> Pat { 2255 fn from(node: BindPat) -> Pat {
2276 Pat { syntax: node.syntax } 2256 Pat::BindPat(node)
2277 } 2257 }
2278} 2258}
2279impl From<PlaceholderPat> for Pat { 2259impl From<PlaceholderPat> for Pat {
2280 fn from(node: PlaceholderPat) -> Pat { 2260 fn from(node: PlaceholderPat) -> Pat {
2281 Pat { syntax: node.syntax } 2261 Pat::PlaceholderPat(node)
2282 } 2262 }
2283} 2263}
2284impl From<PathPat> for Pat { 2264impl From<PathPat> for Pat {
2285 fn from(node: PathPat) -> Pat { 2265 fn from(node: PathPat) -> Pat {
2286 Pat { syntax: node.syntax } 2266 Pat::PathPat(node)
2287 } 2267 }
2288} 2268}
2289impl From<StructPat> for Pat { 2269impl From<StructPat> for Pat {
2290 fn from(node: StructPat) -> Pat { 2270 fn from(node: StructPat) -> Pat {
2291 Pat { syntax: node.syntax } 2271 Pat::StructPat(node)
2292 } 2272 }
2293} 2273}
2294impl From<TupleStructPat> for Pat { 2274impl From<TupleStructPat> for Pat {
2295 fn from(node: TupleStructPat) -> Pat { 2275 fn from(node: TupleStructPat) -> Pat {
2296 Pat { syntax: node.syntax } 2276 Pat::TupleStructPat(node)
2297 } 2277 }
2298} 2278}
2299impl From<TuplePat> for Pat { 2279impl From<TuplePat> for Pat {
2300 fn from(node: TuplePat) -> Pat { 2280 fn from(node: TuplePat) -> Pat {
2301 Pat { syntax: node.syntax } 2281 Pat::TuplePat(node)
2302 } 2282 }
2303} 2283}
2304impl From<SlicePat> for Pat { 2284impl From<SlicePat> for Pat {
2305 fn from(node: SlicePat) -> Pat { 2285 fn from(node: SlicePat) -> Pat {
2306 Pat { syntax: node.syntax } 2286 Pat::SlicePat(node)
2307 } 2287 }
2308} 2288}
2309impl From<RangePat> for Pat { 2289impl From<RangePat> for Pat {
2310 fn from(node: RangePat) -> Pat { 2290 fn from(node: RangePat) -> Pat {
2311 Pat { syntax: node.syntax } 2291 Pat::RangePat(node)
2312 } 2292 }
2313} 2293}
2314impl From<LiteralPat> for Pat { 2294impl From<LiteralPat> for Pat {
2315 fn from(node: LiteralPat) -> Pat { 2295 fn from(node: LiteralPat) -> Pat {
2316 Pat { syntax: node.syntax } 2296 Pat::LiteralPat(node)
2317 } 2297 }
2318} 2298}
2319impl Pat { 2299impl AstNode for Pat {
2320 pub fn kind(&self) -> PatKind { 2300 fn can_cast(kind: SyntaxKind) -> bool {
2321 let syntax = self.syntax.clone(); 2301 match kind {
2322 match syntax.kind() { 2302 REF_PAT | BIND_PAT | PLACEHOLDER_PAT | PATH_PAT | STRUCT_PAT | TUPLE_STRUCT_PAT
2323 REF_PAT => PatKind::RefPat(RefPat { syntax }), 2303 | TUPLE_PAT | SLICE_PAT | RANGE_PAT | LITERAL_PAT => true,
2324 BIND_PAT => PatKind::BindPat(BindPat { syntax }), 2304 _ => false,
2325 PLACEHOLDER_PAT => PatKind::PlaceholderPat(PlaceholderPat { syntax }), 2305 }
2326 PATH_PAT => PatKind::PathPat(PathPat { syntax }), 2306 }
2327 STRUCT_PAT => PatKind::StructPat(StructPat { syntax }), 2307 fn cast(syntax: SyntaxNode) -> Option<Self> {
2328 TUPLE_STRUCT_PAT => PatKind::TupleStructPat(TupleStructPat { syntax }), 2308 let res = match syntax.kind() {
2329 TUPLE_PAT => PatKind::TuplePat(TuplePat { syntax }), 2309 REF_PAT => Pat::RefPat(RefPat { syntax }),
2330 SLICE_PAT => PatKind::SlicePat(SlicePat { syntax }), 2310 BIND_PAT => Pat::BindPat(BindPat { syntax }),
2331 RANGE_PAT => PatKind::RangePat(RangePat { syntax }), 2311 PLACEHOLDER_PAT => Pat::PlaceholderPat(PlaceholderPat { syntax }),
2332 LITERAL_PAT => PatKind::LiteralPat(LiteralPat { syntax }), 2312 PATH_PAT => Pat::PathPat(PathPat { syntax }),
2333 _ => unreachable!(), 2313 STRUCT_PAT => Pat::StructPat(StructPat { syntax }),
2314 TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }),
2315 TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }),
2316 SLICE_PAT => Pat::SlicePat(SlicePat { syntax }),
2317 RANGE_PAT => Pat::RangePat(RangePat { syntax }),
2318 LITERAL_PAT => Pat::LiteralPat(LiteralPat { syntax }),
2319 _ => return None,
2320 };
2321 Some(res)
2322 }
2323 fn syntax(&self) -> &SyntaxNode {
2324 match self {
2325 Pat::RefPat(it) => &it.syntax,
2326 Pat::BindPat(it) => &it.syntax,
2327 Pat::PlaceholderPat(it) => &it.syntax,
2328 Pat::PathPat(it) => &it.syntax,
2329 Pat::StructPat(it) => &it.syntax,
2330 Pat::TupleStructPat(it) => &it.syntax,
2331 Pat::TuplePat(it) => &it.syntax,
2332 Pat::SlicePat(it) => &it.syntax,
2333 Pat::RangePat(it) => &it.syntax,
2334 Pat::LiteralPat(it) => &it.syntax,
2334 } 2335 }
2335 } 2336 }
2336} 2337}
@@ -2951,48 +2952,39 @@ impl StaticDef {
2951 } 2952 }
2952} 2953}
2953#[derive(Debug, Clone, PartialEq, Eq, Hash)] 2954#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2954pub struct Stmt { 2955pub enum Stmt {
2955 pub(crate) syntax: SyntaxNode,
2956}
2957impl AstNode for Stmt {
2958 fn can_cast(kind: SyntaxKind) -> bool {
2959 match kind {
2960 EXPR_STMT | LET_STMT => true,
2961 _ => false,
2962 }
2963 }
2964 fn cast(syntax: SyntaxNode) -> Option<Self> {
2965 if Self::can_cast(syntax.kind()) {
2966 Some(Self { syntax })
2967 } else {
2968 None
2969 }
2970 }
2971 fn syntax(&self) -> &SyntaxNode {
2972 &self.syntax
2973 }
2974}
2975pub enum StmtKind {
2976 ExprStmt(ExprStmt), 2956 ExprStmt(ExprStmt),
2977 LetStmt(LetStmt), 2957 LetStmt(LetStmt),
2978} 2958}
2979impl From<ExprStmt> for Stmt { 2959impl From<ExprStmt> for Stmt {
2980 fn from(node: ExprStmt) -> Stmt { 2960 fn from(node: ExprStmt) -> Stmt {
2981 Stmt { syntax: node.syntax } 2961 Stmt::ExprStmt(node)
2982 } 2962 }
2983} 2963}
2984impl From<LetStmt> for Stmt { 2964impl From<LetStmt> for Stmt {
2985 fn from(node: LetStmt) -> Stmt { 2965 fn from(node: LetStmt) -> Stmt {
2986 Stmt { syntax: node.syntax } 2966 Stmt::LetStmt(node)
2987 } 2967 }
2988} 2968}
2989impl Stmt { 2969impl AstNode for Stmt {
2990 pub fn kind(&self) -> StmtKind { 2970 fn can_cast(kind: SyntaxKind) -> bool {
2991 let syntax = self.syntax.clone(); 2971 match kind {
2992 match syntax.kind() { 2972 EXPR_STMT | LET_STMT => true,
2993 EXPR_STMT => StmtKind::ExprStmt(ExprStmt { syntax }), 2973 _ => false,
2994 LET_STMT => StmtKind::LetStmt(LetStmt { syntax }), 2974 }
2995 _ => unreachable!(), 2975 }
2976 fn cast(syntax: SyntaxNode) -> Option<Self> {
2977 let res = match syntax.kind() {
2978 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
2979 LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
2980 _ => return None,
2981 };
2982 Some(res)
2983 }
2984 fn syntax(&self) -> &SyntaxNode {
2985 match self {
2986 Stmt::ExprStmt(it) => &it.syntax,
2987 Stmt::LetStmt(it) => &it.syntax,
2996 } 2988 }
2997 } 2989 }
2998} 2990}
@@ -3508,30 +3500,7 @@ impl TypeParamList {
3508 } 3500 }
3509} 3501}
3510#[derive(Debug, Clone, PartialEq, Eq, Hash)] 3502#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3511pub struct TypeRef { 3503pub enum TypeRef {
3512 pub(crate) syntax: SyntaxNode,
3513}
3514impl AstNode for TypeRef {
3515 fn can_cast(kind: SyntaxKind) -> bool {
3516 match kind {
3517 PAREN_TYPE | TUPLE_TYPE | NEVER_TYPE | PATH_TYPE | POINTER_TYPE | ARRAY_TYPE
3518 | SLICE_TYPE | REFERENCE_TYPE | PLACEHOLDER_TYPE | FN_POINTER_TYPE | FOR_TYPE
3519 | IMPL_TRAIT_TYPE | DYN_TRAIT_TYPE => true,
3520 _ => false,
3521 }
3522 }
3523 fn cast(syntax: SyntaxNode) -> Option<Self> {
3524 if Self::can_cast(syntax.kind()) {
3525 Some(Self { syntax })
3526 } else {
3527 None
3528 }
3529 }
3530 fn syntax(&self) -> &SyntaxNode {
3531 &self.syntax
3532 }
3533}
3534pub enum TypeRefKind {
3535 ParenType(ParenType), 3504 ParenType(ParenType),
3536 TupleType(TupleType), 3505 TupleType(TupleType),
3537 NeverType(NeverType), 3506 NeverType(NeverType),
@@ -3548,87 +3517,112 @@ pub enum TypeRefKind {
3548} 3517}
3549impl From<ParenType> for TypeRef { 3518impl From<ParenType> for TypeRef {
3550 fn from(node: ParenType) -> TypeRef { 3519 fn from(node: ParenType) -> TypeRef {
3551 TypeRef { syntax: node.syntax } 3520 TypeRef::ParenType(node)
3552 } 3521 }
3553} 3522}
3554impl From<TupleType> for TypeRef { 3523impl From<TupleType> for TypeRef {
3555 fn from(node: TupleType) -> TypeRef { 3524 fn from(node: TupleType) -> TypeRef {
3556 TypeRef { syntax: node.syntax } 3525 TypeRef::TupleType(node)
3557 } 3526 }
3558} 3527}
3559impl From<NeverType> for TypeRef { 3528impl From<NeverType> for TypeRef {
3560 fn from(node: NeverType) -> TypeRef { 3529 fn from(node: NeverType) -> TypeRef {
3561 TypeRef { syntax: node.syntax } 3530 TypeRef::NeverType(node)
3562 } 3531 }
3563} 3532}
3564impl From<PathType> for TypeRef { 3533impl From<PathType> for TypeRef {
3565 fn from(node: PathType) -> TypeRef { 3534 fn from(node: PathType) -> TypeRef {
3566 TypeRef { syntax: node.syntax } 3535 TypeRef::PathType(node)
3567 } 3536 }
3568} 3537}
3569impl From<PointerType> for TypeRef { 3538impl From<PointerType> for TypeRef {
3570 fn from(node: PointerType) -> TypeRef { 3539 fn from(node: PointerType) -> TypeRef {
3571 TypeRef { syntax: node.syntax } 3540 TypeRef::PointerType(node)
3572 } 3541 }
3573} 3542}
3574impl From<ArrayType> for TypeRef { 3543impl From<ArrayType> for TypeRef {
3575 fn from(node: ArrayType) -> TypeRef { 3544 fn from(node: ArrayType) -> TypeRef {
3576 TypeRef { syntax: node.syntax } 3545 TypeRef::ArrayType(node)
3577 } 3546 }
3578} 3547}
3579impl From<SliceType> for TypeRef { 3548impl From<SliceType> for TypeRef {
3580 fn from(node: SliceType) -> TypeRef { 3549 fn from(node: SliceType) -> TypeRef {
3581 TypeRef { syntax: node.syntax } 3550 TypeRef::SliceType(node)
3582 } 3551 }
3583} 3552}
3584impl From<ReferenceType> for TypeRef { 3553impl From<ReferenceType> for TypeRef {
3585 fn from(node: ReferenceType) -> TypeRef { 3554 fn from(node: ReferenceType) -> TypeRef {
3586 TypeRef { syntax: node.syntax } 3555 TypeRef::ReferenceType(node)
3587 } 3556 }
3588} 3557}
3589impl From<PlaceholderType> for TypeRef { 3558impl From<PlaceholderType> for TypeRef {
3590 fn from(node: PlaceholderType) -> TypeRef { 3559 fn from(node: PlaceholderType) -> TypeRef {
3591 TypeRef { syntax: node.syntax } 3560 TypeRef::PlaceholderType(node)
3592 } 3561 }
3593} 3562}
3594impl From<FnPointerType> for TypeRef { 3563impl From<FnPointerType> for TypeRef {
3595 fn from(node: FnPointerType) -> TypeRef { 3564 fn from(node: FnPointerType) -> TypeRef {
3596 TypeRef { syntax: node.syntax } 3565 TypeRef::FnPointerType(node)
3597 } 3566 }
3598} 3567}
3599impl From<ForType> for TypeRef { 3568impl From<ForType> for TypeRef {
3600 fn from(node: ForType) -> TypeRef { 3569 fn from(node: ForType) -> TypeRef {
3601 TypeRef { syntax: node.syntax } 3570 TypeRef::ForType(node)
3602 } 3571 }
3603} 3572}
3604impl From<ImplTraitType> for TypeRef { 3573impl From<ImplTraitType> for TypeRef {
3605 fn from(node: ImplTraitType) -> TypeRef { 3574 fn from(node: ImplTraitType) -> TypeRef {
3606 TypeRef { syntax: node.syntax } 3575 TypeRef::ImplTraitType(node)
3607 } 3576 }
3608} 3577}
3609impl From<DynTraitType> for TypeRef { 3578impl From<DynTraitType> for TypeRef {
3610 fn from(node: DynTraitType) -> TypeRef { 3579 fn from(node: DynTraitType) -> TypeRef {
3611 TypeRef { syntax: node.syntax } 3580 TypeRef::DynTraitType(node)
3612 } 3581 }
3613} 3582}
3614impl TypeRef { 3583impl AstNode for TypeRef {
3615 pub fn kind(&self) -> TypeRefKind { 3584 fn can_cast(kind: SyntaxKind) -> bool {
3616 let syntax = self.syntax.clone(); 3585 match kind {
3617 match syntax.kind() { 3586 PAREN_TYPE | TUPLE_TYPE | NEVER_TYPE | PATH_TYPE | POINTER_TYPE | ARRAY_TYPE
3618 PAREN_TYPE => TypeRefKind::ParenType(ParenType { syntax }), 3587 | SLICE_TYPE | REFERENCE_TYPE | PLACEHOLDER_TYPE | FN_POINTER_TYPE | FOR_TYPE
3619 TUPLE_TYPE => TypeRefKind::TupleType(TupleType { syntax }), 3588 | IMPL_TRAIT_TYPE | DYN_TRAIT_TYPE => true,
3620 NEVER_TYPE => TypeRefKind::NeverType(NeverType { syntax }), 3589 _ => false,
3621 PATH_TYPE => TypeRefKind::PathType(PathType { syntax }), 3590 }
3622 POINTER_TYPE => TypeRefKind::PointerType(PointerType { syntax }), 3591 }
3623 ARRAY_TYPE => TypeRefKind::ArrayType(ArrayType { syntax }), 3592 fn cast(syntax: SyntaxNode) -> Option<Self> {
3624 SLICE_TYPE => TypeRefKind::SliceType(SliceType { syntax }), 3593 let res = match syntax.kind() {
3625 REFERENCE_TYPE => TypeRefKind::ReferenceType(ReferenceType { syntax }), 3594 PAREN_TYPE => TypeRef::ParenType(ParenType { syntax }),
3626 PLACEHOLDER_TYPE => TypeRefKind::PlaceholderType(PlaceholderType { syntax }), 3595 TUPLE_TYPE => TypeRef::TupleType(TupleType { syntax }),
3627 FN_POINTER_TYPE => TypeRefKind::FnPointerType(FnPointerType { syntax }), 3596 NEVER_TYPE => TypeRef::NeverType(NeverType { syntax }),
3628 FOR_TYPE => TypeRefKind::ForType(ForType { syntax }), 3597 PATH_TYPE => TypeRef::PathType(PathType { syntax }),
3629 IMPL_TRAIT_TYPE => TypeRefKind::ImplTraitType(ImplTraitType { syntax }), 3598 POINTER_TYPE => TypeRef::PointerType(PointerType { syntax }),
3630 DYN_TRAIT_TYPE => TypeRefKind::DynTraitType(DynTraitType { syntax }), 3599 ARRAY_TYPE => TypeRef::ArrayType(ArrayType { syntax }),
3631 _ => unreachable!(), 3600 SLICE_TYPE => TypeRef::SliceType(SliceType { syntax }),
3601 REFERENCE_TYPE => TypeRef::ReferenceType(ReferenceType { syntax }),
3602 PLACEHOLDER_TYPE => TypeRef::PlaceholderType(PlaceholderType { syntax }),
3603 FN_POINTER_TYPE => TypeRef::FnPointerType(FnPointerType { syntax }),
3604 FOR_TYPE => TypeRef::ForType(ForType { syntax }),
3605 IMPL_TRAIT_TYPE => TypeRef::ImplTraitType(ImplTraitType { syntax }),
3606 DYN_TRAIT_TYPE => TypeRef::DynTraitType(DynTraitType { syntax }),
3607 _ => return None,
3608 };
3609 Some(res)
3610 }
3611 fn syntax(&self) -> &SyntaxNode {
3612 match self {
3613 TypeRef::ParenType(it) => &it.syntax,
3614 TypeRef::TupleType(it) => &it.syntax,
3615 TypeRef::NeverType(it) => &it.syntax,
3616 TypeRef::PathType(it) => &it.syntax,
3617 TypeRef::PointerType(it) => &it.syntax,
3618 TypeRef::ArrayType(it) => &it.syntax,
3619 TypeRef::SliceType(it) => &it.syntax,
3620 TypeRef::ReferenceType(it) => &it.syntax,
3621 TypeRef::PlaceholderType(it) => &it.syntax,
3622 TypeRef::FnPointerType(it) => &it.syntax,
3623 TypeRef::ForType(it) => &it.syntax,
3624 TypeRef::ImplTraitType(it) => &it.syntax,
3625 TypeRef::DynTraitType(it) => &it.syntax,
3632 } 3626 }
3633 } 3627 }
3634} 3628}
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 4c4e0580a..2bced1867 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -186,8 +186,8 @@ fn api_walkthrough() {
186 // Let's fetch the `foo` function. 186 // Let's fetch the `foo` function.
187 let mut func = None; 187 let mut func = None;
188 for item in file.items() { 188 for item in file.items() {
189 match item.kind() { 189 match item {
190 ast::ModuleItemKind::FnDef(f) => func = Some(f), 190 ast::ModuleItem::FnDef(f) => func = Some(f),
191 _ => unreachable!(), 191 _ => unreachable!(),
192 } 192 }
193 } 193 }
@@ -206,12 +206,12 @@ fn api_walkthrough() {
206 let block: ast::Block = func.body().unwrap(); 206 let block: ast::Block = func.body().unwrap();
207 let expr: ast::Expr = block.expr().unwrap(); 207 let expr: ast::Expr = block.expr().unwrap();
208 208
209 // "Enum"-like nodes are represented using the "kind" pattern. It allows us 209 // Enums are used to group related ast nodes together, and can be used for
210 // to match exhaustively against all flavors of nodes, while maintaining 210 // matching. However, because there are no public fields, it's possible to
211 // internal representation flexibility. The drawback is that one can't write 211 // match only the top level enum: that is the price we pay for increased API
212 // nested matches as one pattern. 212 // flexibility
213 let bin_expr: ast::BinExpr = match expr.kind() { 213 let bin_expr: &ast::BinExpr = match &expr {
214 ast::ExprKind::BinExpr(e) => e, 214 ast::Expr::BinExpr(e) => e,
215 _ => unreachable!(), 215 _ => unreachable!(),
216 }; 216 };
217 217
diff --git a/crates/ra_tools/src/boilerplate_gen.rs b/crates/ra_tools/src/boilerplate_gen.rs
index 486a3fdec..01d092d48 100644
--- a/crates/ra_tools/src/boilerplate_gen.rs
+++ b/crates/ra_tools/src/boilerplate_gen.rs
@@ -37,41 +37,72 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
37 ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>(); 37 ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
38 let name = format_ident!("{}", name); 38 let name = format_ident!("{}", name);
39 39
40 let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() } 40 let adt = if variants.is_empty() {
41 .into_iter() 41 let kind = format_ident!("{}", name.to_string().to_shouty_snake_case());
42 .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) 42 quote! {
43 .collect::<Vec<_>>(); 43 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
44 pub struct #name {
45 pub(crate) syntax: SyntaxNode,
46 }
44 47
45 let variants = if variants.is_empty() { 48 impl AstNode for #name {
46 None 49 fn can_cast(kind: SyntaxKind) -> bool {
50 match kind {
51 #kind => true,
52 _ => false,
53 }
54 }
55 fn cast(syntax: SyntaxNode) -> Option<Self> {
56 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
57 }
58 fn syntax(&self) -> &SyntaxNode { &self.syntax }
59 }
60 }
47 } else { 61 } else {
48 let kind_enum = format_ident!("{}Kind", name); 62 let kinds = variants
49 Some(quote!( 63 .iter()
50 pub enum #kind_enum { 64 .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
65 .collect::<Vec<_>>();
66
67 quote! {
68 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
69 pub enum #name {
51 #(#variants(#variants),)* 70 #(#variants(#variants),)*
52 } 71 }
53 72
54 #( 73 #(
55 impl From<#variants> for #name { 74 impl From<#variants> for #name {
56 fn from(node: #variants) -> #name { 75 fn from(node: #variants) -> #name {
57 #name { syntax: node.syntax } 76 #name::#variants(node)
58 } 77 }
59 } 78 }
60 )* 79 )*
61 80
62 impl #name { 81 impl AstNode for #name {
63 pub fn kind(&self) -> #kind_enum { 82 fn can_cast(kind: SyntaxKind) -> bool {
64 let syntax = self.syntax.clone(); 83 match kind {
65 match syntax.kind() { 84 #(#kinds)|* => true,
85 _ => false,
86 }
87 }
88 fn cast(syntax: SyntaxNode) -> Option<Self> {
89 let res = match syntax.kind() {
66 #( 90 #(
67 #kinds => 91 #kinds => #name::#variants(#variants { syntax }),
68 #kind_enum::#variants(#variants { syntax }), 92 )*
93 _ => return None,
94 };
95 Some(res)
96 }
97 fn syntax(&self) -> &SyntaxNode {
98 match self {
99 #(
100 #name::#variants(it) => &it.syntax,
69 )* 101 )*
70 _ => unreachable!(),
71 } 102 }
72 } 103 }
73 } 104 }
74 )) 105 }
75 }; 106 };
76 107
77 let traits = ast_node.traits.iter().map(|trait_name| { 108 let traits = ast_node.traits.iter().map(|trait_name| {
@@ -105,25 +136,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
105 }); 136 });
106 137
107 quote! { 138 quote! {
108 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 139 #adt
109 pub struct #name {
110 pub(crate) syntax: SyntaxNode,
111 }
112
113 impl AstNode for #name {
114 fn can_cast(kind: SyntaxKind) -> bool {
115 match kind {
116 #(#kinds)|* => true,
117 _ => false,
118 }
119 }
120 fn cast(syntax: SyntaxNode) -> Option<Self> {
121 if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
122 }
123 fn syntax(&self) -> &SyntaxNode { &self.syntax }
124 }
125
126 #variants
127 140
128 #(#traits)* 141 #(#traits)*
129 142