aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/handlers/change_return_type_to_result.rs2
-rw-r--r--crates/ra_hir_def/src/body/lower.rs30
-rw-r--r--crates/ra_syntax/src/ast/generated.rs8
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs6
-rw-r--r--xtask/src/codegen/gen_syntax.rs3
-rw-r--r--xtask/src/codegen/rust.ungram2
6 files changed, 36 insertions, 15 deletions
diff --git a/crates/ra_assists/src/handlers/change_return_type_to_result.rs b/crates/ra_assists/src/handlers/change_return_type_to_result.rs
index 167e162d8..4b73c41da 100644
--- a/crates/ra_assists/src/handlers/change_return_type_to_result.rs
+++ b/crates/ra_assists/src/handlers/change_return_type_to_result.rs
@@ -74,6 +74,7 @@ impl TailReturnCollector {
74 let expr = match &stmt { 74 let expr = match &stmt {
75 ast::Stmt::ExprStmt(stmt) => stmt.expr(), 75 ast::Stmt::ExprStmt(stmt) => stmt.expr(),
76 ast::Stmt::LetStmt(stmt) => stmt.initializer(), 76 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
77 ast::Stmt::Item(_) => continue,
77 }; 78 };
78 if let Some(expr) = &expr { 79 if let Some(expr) = &expr {
79 self.handle_exprs(expr, collect_break); 80 self.handle_exprs(expr, collect_break);
@@ -94,6 +95,7 @@ impl TailReturnCollector {
94 let expr_stmt = match &expr_stmt { 95 let expr_stmt = match &expr_stmt {
95 ast::Stmt::ExprStmt(stmt) => stmt.expr(), 96 ast::Stmt::ExprStmt(stmt) => stmt.expr(),
96 ast::Stmt::LetStmt(stmt) => stmt.initializer(), 97 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
98 ast::Stmt::Item(_) => None,
97 }; 99 };
98 if let Some(expr) = &expr_stmt { 100 if let Some(expr) = &expr_stmt {
99 self.handle_exprs(expr, collect_break); 101 self.handle_exprs(expr, collect_break);
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 827ced4ad..5816bf566 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -10,7 +10,7 @@ use hir_expand::{
10use ra_arena::Arena; 10use ra_arena::Arena;
11use ra_syntax::{ 11use ra_syntax::{
12 ast::{ 12 ast::{
13 self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner, 13 self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
14 SlicePatComponents, 14 SlicePatComponents,
15 }, 15 },
16 AstNode, AstPtr, 16 AstNode, AstPtr,
@@ -601,14 +601,20 @@ impl ExprCollector<'_> {
601 self.collect_block_items(&block); 601 self.collect_block_items(&block);
602 let statements = block 602 let statements = block
603 .statements() 603 .statements()
604 .map(|s| match s { 604 .filter_map(|s| {
605 ast::Stmt::LetStmt(stmt) => { 605 let stmt = match s {
606 let pat = self.collect_pat_opt(stmt.pat()); 606 ast::Stmt::LetStmt(stmt) => {
607 let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); 607 let pat = self.collect_pat_opt(stmt.pat());
608 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 608 let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
609 Statement::Let { pat, type_ref, initializer } 609 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
610 } 610 Statement::Let { pat, type_ref, initializer }
611 ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())), 611 }
612 ast::Stmt::ExprStmt(stmt) => {
613 Statement::Expr(self.collect_expr_opt(stmt.expr()))
614 }
615 ast::Stmt::Item(_) => return None,
616 };
617 Some(stmt)
612 }) 618 })
613 .collect(); 619 .collect();
614 let tail = block.expr().map(|e| self.collect_expr(e)); 620 let tail = block.expr().map(|e| self.collect_expr(e));
@@ -620,7 +626,11 @@ impl ExprCollector<'_> {
620 let container = ContainerId::DefWithBodyId(self.def); 626 let container = ContainerId::DefWithBodyId(self.def);
621 627
622 let items = block 628 let items = block
623 .items() 629 .statements()
630 .filter_map(|stmt| match stmt {
631 ast::Stmt::Item(it) => Some(it),
632 ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None,
633 })
624 .filter_map(|item| { 634 .filter_map(|item| {
625 let (def, name): (ModuleDefId, Option<ast::Name>) = match item { 635 let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
626 ast::Item::Fn(def) => { 636 ast::Item::Fn(def) => {
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index ba55f1c42..4a6f41ee7 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -17,14 +17,17 @@ impl AstNode for Stmt {
17 fn can_cast(kind: SyntaxKind) -> bool { 17 fn can_cast(kind: SyntaxKind) -> bool {
18 match kind { 18 match kind {
19 LET_STMT | EXPR_STMT => true, 19 LET_STMT | EXPR_STMT => true,
20 _ => false, 20 _ => Item::can_cast(kind),
21 } 21 }
22 } 22 }
23 fn cast(syntax: SyntaxNode) -> Option<Self> { 23 fn cast(syntax: SyntaxNode) -> Option<Self> {
24 let res = match syntax.kind() { 24 let res = match syntax.kind() {
25 LET_STMT => Stmt::LetStmt(LetStmt { syntax }), 25 LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
26 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }), 26 EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
27 _ => return None, 27 _ => {
28 let item = Item::cast(syntax)?;
29 Stmt::Item(item)
30 }
28 }; 31 };
29 Some(res) 32 Some(res)
30 } 33 }
@@ -32,6 +35,7 @@ impl AstNode for Stmt {
32 match self { 35 match self {
33 Stmt::LetStmt(it) => &it.syntax, 36 Stmt::LetStmt(it) => &it.syntax,
34 Stmt::ExprStmt(it) => &it.syntax, 37 Stmt::ExprStmt(it) => &it.syntax,
38 Stmt::Item(it) => it.syntax(),
35 } 39 }
36 } 40 }
37} 41}
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index 8ef72fec7..763fd20f4 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -348,7 +348,6 @@ pub struct BlockExpr {
348 pub(crate) syntax: SyntaxNode, 348 pub(crate) syntax: SyntaxNode,
349} 349}
350impl ast::AttrsOwner for BlockExpr {} 350impl ast::AttrsOwner for BlockExpr {}
351impl ast::ModuleItemOwner for BlockExpr {}
352impl BlockExpr { 351impl BlockExpr {
353 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } 352 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
354 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } 353 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
@@ -1395,8 +1394,8 @@ impl ast::AttrsOwner for GenericParam {}
1395pub enum Stmt { 1394pub enum Stmt {
1396 LetStmt(LetStmt), 1395 LetStmt(LetStmt),
1397 ExprStmt(ExprStmt), 1396 ExprStmt(ExprStmt),
1397 Item(Item),
1398} 1398}
1399impl ast::AttrsOwner for Stmt {}
1400impl AstNode for SourceFile { 1399impl AstNode for SourceFile {
1401 fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE } 1400 fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE }
1402 fn cast(syntax: SyntaxNode) -> Option<Self> { 1401 fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3380,6 +3379,9 @@ impl From<LetStmt> for Stmt {
3380impl From<ExprStmt> for Stmt { 3379impl From<ExprStmt> for Stmt {
3381 fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) } 3380 fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
3382} 3381}
3382impl From<Item> for Stmt {
3383 fn from(node: Item) -> Stmt { Stmt::Item(node) }
3384}
3383impl std::fmt::Display for Item { 3385impl std::fmt::Display for Item {
3384 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 3386 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3385 std::fmt::Display::fmt(self.syntax(), f) 3387 std::fmt::Display::fmt(self.syntax(), f)
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index e3d4269f6..d9f358513 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -694,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
694 694
695fn extract_enum_traits(ast: &mut AstSrc) { 695fn extract_enum_traits(ast: &mut AstSrc) {
696 for enm in &mut ast.enums { 696 for enm in &mut ast.enums {
697 if enm.name == "Stmt" {
698 continue;
699 }
697 let nodes = &ast.nodes; 700 let nodes = &ast.nodes;
698 let mut variant_traits = enm 701 let mut variant_traits = enm
699 .variants 702 .variants
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram
index 8271509cf..17de36d7a 100644
--- a/xtask/src/codegen/rust.ungram
+++ b/xtask/src/codegen/rust.ungram
@@ -197,6 +197,7 @@ Attr =
197Stmt = 197Stmt =
198 LetStmt 198 LetStmt
199| ExprStmt 199| ExprStmt
200| Item
200 201
201LetStmt = 202LetStmt =
202 Attr* 'let' Pat (':' Type)? 203 Attr* 'let' Pat (':' Type)?
@@ -316,7 +317,6 @@ Label =
316BlockExpr = 317BlockExpr =
317 Attr* Label 318 Attr* Label
318 '{' 319 '{'
319 Item*
320 statements:Stmt* 320 statements:Stmt*
321 Expr? 321 Expr?
322 '}' 322 '}'