Skip to content

Commit ad239f6

Browse files
bors[bot]matklad
andauthored
Merge #5623
5623: Item is a Stmt r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 3407d6f + d4d986c commit ad239f6

File tree

7 files changed

+94
-63
lines changed

7 files changed

+94
-63
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.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717

1818
pub use self::{
1919
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
20-
generated::{nodes::*, tokens::*},
20+
generated::*,
2121
node_ext::{
2222
AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
2323
StructKind, TypeBoundKind, VisibilityKind,

crates/ra_syntax/src/ast/generated.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
//! This file is actually hand-written, but the submodules are indeed generated.
2-
32
#[rustfmt::skip]
4-
pub(super) mod nodes;
3+
mod nodes;
54
#[rustfmt::skip]
6-
pub(super) mod tokens;
5+
mod tokens;
6+
7+
use crate::{
8+
AstNode,
9+
SyntaxKind::{self, *},
10+
SyntaxNode,
11+
};
12+
13+
pub use {nodes::*, tokens::*};
14+
15+
// Stmt is the only nested enum, so it's easier to just hand-write it
16+
impl AstNode for Stmt {
17+
fn can_cast(kind: SyntaxKind) -> bool {
18+
match kind {
19+
LET_STMT | EXPR_STMT => true,
20+
_ => Item::can_cast(kind),
21+
}
22+
}
23+
fn cast(syntax: SyntaxNode) -> Option<Self> {
24+
let res = match syntax.kind() {
25+
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
26+
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
27+
_ => {
28+
let item = Item::cast(syntax)?;
29+
Stmt::Item(item)
30+
}
31+
};
32+
Some(res)
33+
}
34+
fn syntax(&self) -> &SyntaxNode {
35+
match self {
36+
Stmt::LetStmt(it) => &it.syntax,
37+
Stmt::ExprStmt(it) => &it.syntax,
38+
Stmt::Item(it) => it.syntax(),
39+
}
40+
}
41+
}

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

Lines changed: 3 additions & 23 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,27 +3379,8 @@ impl From<LetStmt> for Stmt {
33803379
impl From<ExprStmt> for Stmt {
33813380
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
33823381
}
3383-
impl AstNode for Stmt {
3384-
fn can_cast(kind: SyntaxKind) -> bool {
3385-
match kind {
3386-
LET_STMT | EXPR_STMT => true,
3387-
_ => false,
3388-
}
3389-
}
3390-
fn cast(syntax: SyntaxNode) -> Option<Self> {
3391-
let res = match syntax.kind() {
3392-
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
3393-
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
3394-
_ => return None,
3395-
};
3396-
Some(res)
3397-
}
3398-
fn syntax(&self) -> &SyntaxNode {
3399-
match self {
3400-
Stmt::LetStmt(it) => &it.syntax,
3401-
Stmt::ExprStmt(it) => &it.syntax,
3402-
}
3403-
}
3382+
impl From<Item> for Stmt {
3383+
fn from(node: Item) -> Stmt { Stmt::Item(node) }
34043384
}
34053385
impl std::fmt::Display for Item {
34063386
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

xtask/src/codegen/gen_syntax.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
153153
quote!(impl ast::#trait_name for #name {})
154154
});
155155

156-
(
157-
quote! {
158-
#[pretty_doc_comment_placeholder_workaround]
159-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
160-
pub enum #name {
161-
#(#variants(#variants),)*
162-
}
163-
164-
#(#traits)*
165-
},
156+
let ast_node = if en.name == "Stmt" {
157+
quote! {}
158+
} else {
166159
quote! {
167-
#(
168-
impl From<#variants> for #name {
169-
fn from(node: #variants) -> #name {
170-
#name::#variants(node)
171-
}
172-
}
173-
)*
174-
175160
impl AstNode for #name {
176161
fn can_cast(kind: SyntaxKind) -> bool {
177162
match kind {
@@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
196181
}
197182
}
198183
}
184+
}
185+
};
186+
187+
(
188+
quote! {
189+
#[pretty_doc_comment_placeholder_workaround]
190+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
191+
pub enum #name {
192+
#(#variants(#variants),)*
193+
}
194+
195+
#(#traits)*
196+
},
197+
quote! {
198+
#(
199+
impl From<#variants> for #name {
200+
fn from(node: #variants) -> #name {
201+
#name::#variants(node)
202+
}
203+
}
204+
)*
205+
#ast_node
199206
},
200207
)
201208
})
@@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
497504
let mut res = AstSrc::default();
498505
res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
499506

500-
let nodes = grammar
501-
.iter()
502-
.filter(|&node| match grammar[node].rule {
503-
Rule::Node(it) if it == node => false,
504-
_ => true,
505-
})
506-
.collect::<Vec<_>>();
507+
let nodes = grammar.iter().collect::<Vec<_>>();
507508

508509
for &node in &nodes {
509510
let name = grammar[node].name.clone();
@@ -693,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
693694

694695
fn extract_enum_traits(ast: &mut AstSrc) {
695696
for enm in &mut ast.enums {
697+
if enm.name == "Stmt" {
698+
continue;
699+
}
696700
let nodes = &ast.nodes;
697701
let mut variant_traits = enm
698702
.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)