Item is a Stmt

This commit is contained in:
Aleksey Kladov 2020-07-31 15:46:12 +02:00
parent a7ca6583fb
commit d4d986c7f8
6 changed files with 36 additions and 15 deletions

View File

@ -74,6 +74,7 @@ impl TailReturnCollector {
let expr = match &stmt {
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
ast::Stmt::Item(_) => continue,
};
if let Some(expr) = &expr {
self.handle_exprs(expr, collect_break);
@ -94,6 +95,7 @@ impl TailReturnCollector {
let expr_stmt = match &expr_stmt {
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
ast::Stmt::Item(_) => None,
};
if let Some(expr) = &expr_stmt {
self.handle_exprs(expr, collect_break);

View File

@ -10,7 +10,7 @@ use hir_expand::{
use ra_arena::Arena;
use ra_syntax::{
ast::{
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner,
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
SlicePatComponents,
},
AstNode, AstPtr,
@ -601,14 +601,20 @@ impl ExprCollector<'_> {
self.collect_block_items(&block);
let statements = block
.statements()
.map(|s| match s {
ast::Stmt::LetStmt(stmt) => {
let pat = self.collect_pat_opt(stmt.pat());
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
Statement::Let { pat, type_ref, initializer }
}
ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
.filter_map(|s| {
let stmt = match s {
ast::Stmt::LetStmt(stmt) => {
let pat = self.collect_pat_opt(stmt.pat());
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
Statement::Let { pat, type_ref, initializer }
}
ast::Stmt::ExprStmt(stmt) => {
Statement::Expr(self.collect_expr_opt(stmt.expr()))
}
ast::Stmt::Item(_) => return None,
};
Some(stmt)
})
.collect();
let tail = block.expr().map(|e| self.collect_expr(e));
@ -620,7 +626,11 @@ impl ExprCollector<'_> {
let container = ContainerId::DefWithBodyId(self.def);
let items = block
.items()
.statements()
.filter_map(|stmt| match stmt {
ast::Stmt::Item(it) => Some(it),
ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None,
})
.filter_map(|item| {
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
ast::Item::Fn(def) => {

View File

@ -17,14 +17,17 @@ impl AstNode for Stmt {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
LET_STMT | EXPR_STMT => true,
_ => false,
_ => Item::can_cast(kind),
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
_ => return None,
_ => {
let item = Item::cast(syntax)?;
Stmt::Item(item)
}
};
Some(res)
}
@ -32,6 +35,7 @@ impl AstNode for Stmt {
match self {
Stmt::LetStmt(it) => &it.syntax,
Stmt::ExprStmt(it) => &it.syntax,
Stmt::Item(it) => it.syntax(),
}
}
}

View File

@ -348,7 +348,6 @@ pub struct BlockExpr {
pub(crate) syntax: SyntaxNode,
}
impl ast::AttrsOwner for BlockExpr {}
impl ast::ModuleItemOwner for BlockExpr {}
impl BlockExpr {
pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
@ -1395,8 +1394,8 @@ impl ast::AttrsOwner for GenericParam {}
pub enum Stmt {
LetStmt(LetStmt),
ExprStmt(ExprStmt),
Item(Item),
}
impl ast::AttrsOwner for Stmt {}
impl AstNode for SourceFile {
fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE }
fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -3380,6 +3379,9 @@ impl From<LetStmt> for Stmt {
impl From<ExprStmt> for Stmt {
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
}
impl From<Item> for Stmt {
fn from(node: Item) -> Stmt { Stmt::Item(node) }
}
impl std::fmt::Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)

View File

@ -694,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
fn extract_enum_traits(ast: &mut AstSrc) {
for enm in &mut ast.enums {
if enm.name == "Stmt" {
continue;
}
let nodes = &ast.nodes;
let mut variant_traits = enm
.variants

View File

@ -197,6 +197,7 @@ Attr =
Stmt =
LetStmt
| ExprStmt
| Item
LetStmt =
Attr* 'let' Pat (':' Type)?
@ -316,7 +317,6 @@ Label =
BlockExpr =
Attr* Label
'{'
Item*
statements:Stmt*
Expr?
'}'