mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Merge #5623
5623: Item is a Stmt r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
ad239f6197
7 changed files with 95 additions and 64 deletions
|
@ -74,6 +74,7 @@ impl TailReturnCollector {
|
||||||
let expr = match &stmt {
|
let expr = match &stmt {
|
||||||
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
|
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
|
||||||
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
|
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
|
||||||
|
ast::Stmt::Item(_) => continue,
|
||||||
};
|
};
|
||||||
if let Some(expr) = &expr {
|
if let Some(expr) = &expr {
|
||||||
self.handle_exprs(expr, collect_break);
|
self.handle_exprs(expr, collect_break);
|
||||||
|
@ -94,6 +95,7 @@ impl TailReturnCollector {
|
||||||
let expr_stmt = match &expr_stmt {
|
let expr_stmt = match &expr_stmt {
|
||||||
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
|
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
|
||||||
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
|
ast::Stmt::LetStmt(stmt) => stmt.initializer(),
|
||||||
|
ast::Stmt::Item(_) => None,
|
||||||
};
|
};
|
||||||
if let Some(expr) = &expr_stmt {
|
if let Some(expr) = &expr_stmt {
|
||||||
self.handle_exprs(expr, collect_break);
|
self.handle_exprs(expr, collect_break);
|
||||||
|
|
|
@ -10,7 +10,7 @@ use hir_expand::{
|
||||||
use ra_arena::Arena;
|
use ra_arena::Arena;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{
|
ast::{
|
||||||
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, ModuleItemOwner, NameOwner,
|
self, ArgListOwner, ArrayExprKind, LiteralKind, LoopBodyOwner, NameOwner,
|
||||||
SlicePatComponents,
|
SlicePatComponents,
|
||||||
},
|
},
|
||||||
AstNode, AstPtr,
|
AstNode, AstPtr,
|
||||||
|
@ -601,14 +601,20 @@ impl ExprCollector<'_> {
|
||||||
self.collect_block_items(&block);
|
self.collect_block_items(&block);
|
||||||
let statements = block
|
let statements = block
|
||||||
.statements()
|
.statements()
|
||||||
.map(|s| match s {
|
.filter_map(|s| {
|
||||||
ast::Stmt::LetStmt(stmt) => {
|
let stmt = match s {
|
||||||
let pat = self.collect_pat_opt(stmt.pat());
|
ast::Stmt::LetStmt(stmt) => {
|
||||||
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
|
let pat = self.collect_pat_opt(stmt.pat());
|
||||||
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
|
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
|
||||||
Statement::Let { pat, type_ref, initializer }
|
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::ExprStmt(stmt) => {
|
||||||
|
Statement::Expr(self.collect_expr_opt(stmt.expr()))
|
||||||
|
}
|
||||||
|
ast::Stmt::Item(_) => return None,
|
||||||
|
};
|
||||||
|
Some(stmt)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let tail = block.expr().map(|e| self.collect_expr(e));
|
let tail = block.expr().map(|e| self.collect_expr(e));
|
||||||
|
@ -620,7 +626,11 @@ impl ExprCollector<'_> {
|
||||||
let container = ContainerId::DefWithBodyId(self.def);
|
let container = ContainerId::DefWithBodyId(self.def);
|
||||||
|
|
||||||
let items = block
|
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| {
|
.filter_map(|item| {
|
||||||
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
|
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
|
||||||
ast::Item::Fn(def) => {
|
ast::Item::Fn(def) => {
|
||||||
|
|
|
@ -17,7 +17,7 @@ use crate::{
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
|
expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
|
||||||
generated::{nodes::*, tokens::*},
|
generated::*,
|
||||||
node_ext::{
|
node_ext::{
|
||||||
AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
|
AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
|
||||||
StructKind, TypeBoundKind, VisibilityKind,
|
StructKind, TypeBoundKind, VisibilityKind,
|
||||||
|
|
|
@ -1,6 +1,41 @@
|
||||||
//! This file is actually hand-written, but the submodules are indeed generated.
|
//! This file is actually hand-written, but the submodules are indeed generated.
|
||||||
|
#[rustfmt::skip]
|
||||||
|
mod nodes;
|
||||||
|
#[rustfmt::skip]
|
||||||
|
mod tokens;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
use crate::{
|
||||||
pub(super) mod nodes;
|
AstNode,
|
||||||
#[rustfmt::skip]
|
SyntaxKind::{self, *},
|
||||||
pub(super) mod tokens;
|
SyntaxNode,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub use {nodes::*, tokens::*};
|
||||||
|
|
||||||
|
// Stmt is the only nested enum, so it's easier to just hand-write it
|
||||||
|
impl AstNode for Stmt {
|
||||||
|
fn can_cast(kind: SyntaxKind) -> bool {
|
||||||
|
match kind {
|
||||||
|
LET_STMT | EXPR_STMT => true,
|
||||||
|
_ => 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 }),
|
||||||
|
_ => {
|
||||||
|
let item = Item::cast(syntax)?;
|
||||||
|
Stmt::Item(item)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
fn syntax(&self) -> &SyntaxNode {
|
||||||
|
match self {
|
||||||
|
Stmt::LetStmt(it) => &it.syntax,
|
||||||
|
Stmt::ExprStmt(it) => &it.syntax,
|
||||||
|
Stmt::Item(it) => it.syntax(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -348,7 +348,6 @@ pub struct BlockExpr {
|
||||||
pub(crate) syntax: SyntaxNode,
|
pub(crate) syntax: SyntaxNode,
|
||||||
}
|
}
|
||||||
impl ast::AttrsOwner for BlockExpr {}
|
impl ast::AttrsOwner for BlockExpr {}
|
||||||
impl ast::ModuleItemOwner for BlockExpr {}
|
|
||||||
impl BlockExpr {
|
impl BlockExpr {
|
||||||
pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
|
pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
|
||||||
pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
|
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 {
|
pub enum Stmt {
|
||||||
LetStmt(LetStmt),
|
LetStmt(LetStmt),
|
||||||
ExprStmt(ExprStmt),
|
ExprStmt(ExprStmt),
|
||||||
|
Item(Item),
|
||||||
}
|
}
|
||||||
impl ast::AttrsOwner for Stmt {}
|
|
||||||
impl AstNode for SourceFile {
|
impl AstNode for SourceFile {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE }
|
fn can_cast(kind: SyntaxKind) -> bool { kind == SOURCE_FILE }
|
||||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||||
|
@ -3380,27 +3379,8 @@ impl From<LetStmt> for Stmt {
|
||||||
impl From<ExprStmt> for Stmt {
|
impl From<ExprStmt> for Stmt {
|
||||||
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
|
fn from(node: ExprStmt) -> Stmt { Stmt::ExprStmt(node) }
|
||||||
}
|
}
|
||||||
impl AstNode for Stmt {
|
impl From<Item> for Stmt {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool {
|
fn from(node: Item) -> Stmt { Stmt::Item(node) }
|
||||||
match kind {
|
|
||||||
LET_STMT | EXPR_STMT => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
Some(res)
|
|
||||||
}
|
|
||||||
fn syntax(&self) -> &SyntaxNode {
|
|
||||||
match self {
|
|
||||||
Stmt::LetStmt(it) => &it.syntax,
|
|
||||||
Stmt::ExprStmt(it) => &it.syntax,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for Item {
|
impl std::fmt::Display for Item {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
|
|
@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
|
||||||
quote!(impl ast::#trait_name for #name {})
|
quote!(impl ast::#trait_name for #name {})
|
||||||
});
|
});
|
||||||
|
|
||||||
(
|
let ast_node = if en.name == "Stmt" {
|
||||||
|
quote! {}
|
||||||
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
#[pretty_doc_comment_placeholder_workaround]
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
||||||
pub enum #name {
|
|
||||||
#(#variants(#variants),)*
|
|
||||||
}
|
|
||||||
|
|
||||||
#(#traits)*
|
|
||||||
},
|
|
||||||
quote! {
|
|
||||||
#(
|
|
||||||
impl From<#variants> for #name {
|
|
||||||
fn from(node: #variants) -> #name {
|
|
||||||
#name::#variants(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
|
|
||||||
impl AstNode for #name {
|
impl AstNode for #name {
|
||||||
fn can_cast(kind: SyntaxKind) -> bool {
|
fn can_cast(kind: SyntaxKind) -> bool {
|
||||||
match kind {
|
match kind {
|
||||||
|
@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
(
|
||||||
|
quote! {
|
||||||
|
#[pretty_doc_comment_placeholder_workaround]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum #name {
|
||||||
|
#(#variants(#variants),)*
|
||||||
|
}
|
||||||
|
|
||||||
|
#(#traits)*
|
||||||
|
},
|
||||||
|
quote! {
|
||||||
|
#(
|
||||||
|
impl From<#variants> for #name {
|
||||||
|
fn from(node: #variants) -> #name {
|
||||||
|
#name::#variants(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
#ast_node
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
|
||||||
let mut res = AstSrc::default();
|
let mut res = AstSrc::default();
|
||||||
res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
|
res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
|
||||||
|
|
||||||
let nodes = grammar
|
let nodes = grammar.iter().collect::<Vec<_>>();
|
||||||
.iter()
|
|
||||||
.filter(|&node| match grammar[node].rule {
|
|
||||||
Rule::Node(it) if it == node => false,
|
|
||||||
_ => true,
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for &node in &nodes {
|
for &node in &nodes {
|
||||||
let name = grammar[node].name.clone();
|
let name = grammar[node].name.clone();
|
||||||
|
@ -693,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
|
||||||
|
|
||||||
fn extract_enum_traits(ast: &mut AstSrc) {
|
fn extract_enum_traits(ast: &mut AstSrc) {
|
||||||
for enm in &mut ast.enums {
|
for enm in &mut ast.enums {
|
||||||
|
if enm.name == "Stmt" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let nodes = &ast.nodes;
|
let nodes = &ast.nodes;
|
||||||
let mut variant_traits = enm
|
let mut variant_traits = enm
|
||||||
.variants
|
.variants
|
||||||
|
|
|
@ -197,6 +197,7 @@ Attr =
|
||||||
Stmt =
|
Stmt =
|
||||||
LetStmt
|
LetStmt
|
||||||
| ExprStmt
|
| ExprStmt
|
||||||
|
| Item
|
||||||
|
|
||||||
LetStmt =
|
LetStmt =
|
||||||
Attr* 'let' Pat (':' Type)?
|
Attr* 'let' Pat (':' Type)?
|
||||||
|
@ -316,7 +317,6 @@ Label =
|
||||||
BlockExpr =
|
BlockExpr =
|
||||||
Attr* Label
|
Attr* Label
|
||||||
'{'
|
'{'
|
||||||
Item*
|
|
||||||
statements:Stmt*
|
statements:Stmt*
|
||||||
Expr?
|
Expr?
|
||||||
'}'
|
'}'
|
||||||
|
|
Loading…
Reference in a new issue