Skip to content

Commit d4d986c

Browse files
committed
Item is a Stmt
1 parent a7ca658 commit d4d986c

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

crates/ra_assists/src/handlers/change_return_type_to_result.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl TailReturnCollector {
7474
let expr = match &stmt {
7575
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
7676
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
77+
ast::Stmt::Item(_) => continue,
7778
};
7879
if let Some(expr) = &expr {
7980
self.handle_exprs(expr, collect_break);
@@ -94,6 +95,7 @@ impl TailReturnCollector {
9495
let expr_stmt = match &expr_stmt {
9596
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
9697
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
98+
ast::Stmt::Item(_) => None,
9799
};
98100
if let Some(expr) = &expr_stmt {
99101
self.handle_exprs(expr, collect_break);

crates/ra_hir_def/src/body/lower.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::{
1010
use ra_arena::Arena;
1111
use ra_syntax::{
1212
ast::{
13-
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner,
13+
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
1414
SlicePatComponents,
1515
},
1616
AstNode, AstPtr,
@@ -601,14 +601,20 @@ impl ExprCollector<'_> {
601601
self.collect_block_items(&block);
602602
let statements = block
603603
.statements()
604-
.map(|s| match s {
605-
ast::Stmt::LetStmt(stmt) => {
606-
let pat = self.collect_pat_opt(stmt.pat());
607-
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
608-
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
609-
Statement::Let { pat, type_ref, initializer }
610-
}
611-
ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
604+
.filter_map(|s| {
605+
let stmt = match s {
606+
ast::Stmt::LetStmt(stmt) => {
607+
let pat = self.collect_pat_opt(stmt.pat());
608+
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
609+
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
610+
Statement::Let { pat, type_ref, initializer }
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)
612618
})
613619
.collect();
614620
let tail = block.expr().map(|e| self.collect_expr(e));
@@ -620,7 +626,11 @@ impl ExprCollector<'_> {
620626
let container = ContainerId::DefWithBodyId(self.def);
621627

622628
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+
})
624634
.filter_map(|item| {
625635
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
626636
ast::Item::Fn(def) => {

crates/ra_syntax/src/ast/generated.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@ impl AstNode for Stmt {
1717
fn can_cast(kind: SyntaxKind) -> bool {
1818
match kind {
1919
LET_STMT | EXPR_STMT => true,
20-
_ => false,
20+
_ => Item::can_cast(kind),
2121
}
2222
}
2323
fn cast(syntax: SyntaxNode) -> Option<Self> {
2424
let res = match syntax.kind() {
2525
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
2626
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
27-
_ => return None,
27+
_ => {
28+
let item = Item::cast(syntax)?;
29+
Stmt::Item(item)
30+
}
2831
};
2932
Some(res)
3033
}
3134
fn syntax(&self) -> &SyntaxNode {
3235
match self {
3336
Stmt::LetStmt(it) => &it.syntax,
3437
Stmt::ExprStmt(it) => &it.syntax,
38+
Stmt::Item(it) => it.syntax(),
3539
}
3640
}
3741
}

crates/ra_syntax/src/ast/generated/nodes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ pub struct BlockExpr {
348348
pub(crate) syntax: SyntaxNode,
349349
}
350350
impl ast::AttrsOwner for BlockExpr {}
351-
impl ast::ModuleItemOwner for BlockExpr {}
352351
impl BlockExpr {
353352
pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
354353
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
@@ -1395,8 +1394,8 @@ impl ast::AttrsOwner for GenericParam {}
13951394
pub enum Stmt {
13961395
LetStmt(LetStmt),
13971396
ExprStmt(ExprStmt),
1397+
Item(Item),
13981398
}
1399-
impl ast::AttrsOwner for Stmt {}
14001399
impl AstNode for SourceFile {
14011400
fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE }
14021401
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3380,6 +3379,9 @@ impl From<LetStmt> for Stmt {
33803379
impl From<ExprStmt> for Stmt {
33813380
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
33823381
}
3382+
impl From<Item> for Stmt {
3383+
fn from(node: Item) -> Stmt { Stmt::Item(node) }
3384+
}
33833385
impl std::fmt::Display for Item {
33843386
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33853387
std::fmt::Display::fmt(self.syntax(), f)

xtask/src/codegen/gen_syntax.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
694694

695695
fn extract_enum_traits(ast: &mut AstSrc) {
696696
for enm in &mut ast.enums {
697+
if enm.name == "Stmt" {
698+
continue;
699+
}
697700
let nodes = &ast.nodes;
698701
let mut variant_traits = enm
699702
.variants

xtask/src/codegen/rust.ungram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ Attr =
197197
Stmt =
198198
LetStmt
199199
| ExprStmt
200+
| Item
200201

201202
LetStmt =
202203
Attr* 'let' Pat (':' Type)?
@@ -316,7 +317,6 @@ Label =
316317
BlockExpr =
317318
Attr* Label
318319
'{'
319-
Item*
320320
statements:Stmt*
321321
Expr?
322322
'}'

0 commit comments

Comments
 (0)