From 292ba6a1f81fee4170c3081f74499fe8c3ddedd4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 22:41:14 +0200 Subject: Remove dead code, which elaborately pretends to be alive --- crates/ra_hir_def/src/body/lower.rs | 4 --- crates/ra_parser/src/grammar/expressions/atom.rs | 6 ++-- crates/ra_parser/src/syntax_kind/generated.rs | 1 - crates/ra_syntax/src/ast/expr_extensions.rs | 10 +++---- crates/ra_syntax/src/ast/generated/nodes.rs | 37 ++---------------------- xtask/src/ast_src.rs | 5 +--- 6 files changed, 11 insertions(+), 52 deletions(-) diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 571603854..f467ed3fe 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -182,10 +182,6 @@ impl ExprCollector<'_> { self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr) } - ast::Expr::TryBlockExpr(e) => { - let body = self.collect_block_opt(e.body()); - self.alloc_expr(Expr::TryBlock { body }, syntax_ptr) - } ast::Expr::BlockExpr(e) => self.collect_block(e), ast::Expr::LoopExpr(e) => { let body = self.collect_block_opt(e.loop_body()); diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 166dfc472..76aa601cb 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -84,7 +84,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar T![box] => box_expr(p, None), T![for] => for_expr(p, None), T![while] => while_expr(p, None), - T![try] => try_block_expr(p, None), + T![try] => try_expr(p, None), LIFETIME if la == T![:] => { let m = p.start(); label(p); @@ -134,7 +134,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar } }; let blocklike = match done.kind() { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_EXPR => { BlockLike::Block } _ => BlockLike::NotBlock, @@ -532,7 +532,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // fn foo() { // let _ = try {}; // } -fn try_block_expr(p: &mut Parser, m: Option) -> CompletedMarker { +fn try_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T![try])); let m = m.unwrap_or_else(|| p.start()); // Special-case `try!` as macro. diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index 524e7d784..ab727ed7e 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -191,7 +191,6 @@ pub enum SyntaxKind { RECORD_LIT, RECORD_FIELD_LIST, RECORD_FIELD, - TRY_BLOCK_EXPR, BOX_EXPR, CALL_EXPR, INDEX_EXPR, diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index ecf74fd36..6aed7b4bb 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -16,7 +16,7 @@ impl ast::Expr { | ast::Expr::WhileExpr(_) | ast::Expr::BlockExpr(_) | ast::Expr::MatchExpr(_) - | ast::Expr::TryBlockExpr(_) => true, + | ast::Expr::TryExpr(_) => true, _ => false, } } @@ -371,12 +371,12 @@ impl ast::BlockExpr { if self.unsafe_token().is_some() || self.async_token().is_some() { return false; } - let kind = match self.syntax().parent() { + let parent = match self.syntax().parent() { + Some(it) => it, None => return true, - Some(it) => it.kind(), }; - match kind { - FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, + match parent.kind() { + FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR => false, _ => true, } } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index d2253d4af..81260680f 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -475,16 +475,6 @@ impl LoopExpr { pub fn loop_token(&self) -> Option { support::token(&self.syntax, T![loop]) } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct TryBlockExpr { - pub(crate) syntax: SyntaxNode, -} -impl ast::AttrsOwner for TryBlockExpr {} -impl TryBlockExpr { - pub fn try_token(&self) -> Option { support::token(&self.syntax, T![try]) } - pub fn body(&self) -> Option { support::child(&self.syntax) } -} - #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ForExpr { pub(crate) syntax: SyntaxNode, @@ -1475,7 +1465,6 @@ pub enum Expr { FieldExpr(FieldExpr), AwaitExpr(AwaitExpr), TryExpr(TryExpr), - TryBlockExpr(TryBlockExpr), CastExpr(CastExpr), RefExpr(RefExpr), PrefixExpr(PrefixExpr), @@ -1958,17 +1947,6 @@ impl AstNode for LoopExpr { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl AstNode for TryBlockExpr { - fn can_cast(kind: SyntaxKind) -> bool { kind == TRY_BLOCK_EXPR } - fn cast(syntax: SyntaxNode) -> Option { - if Self::can_cast(syntax.kind()) { - Some(Self { syntax }) - } else { - None - } - } - fn syntax(&self) -> &SyntaxNode { &self.syntax } -} impl AstNode for ForExpr { fn can_cast(kind: SyntaxKind) -> bool { kind == FOR_EXPR } fn cast(syntax: SyntaxNode) -> Option { @@ -3310,9 +3288,6 @@ impl From for Expr { impl From for Expr { fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) } } -impl From for Expr { - fn from(node: TryBlockExpr) -> Expr { Expr::TryBlockExpr(node) } -} impl From for Expr { fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) } } @@ -3343,9 +3318,8 @@ impl AstNode for Expr { TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | RECORD_LIT | CALL_EXPR | INDEX_EXPR - | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR - | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL - | BOX_EXPR => true, + | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | CAST_EXPR | REF_EXPR + | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL | BOX_EXPR => true, _ => false, } } @@ -3373,7 +3347,6 @@ impl AstNode for Expr { FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }), AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }), TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), - TRY_BLOCK_EXPR => Expr::TryBlockExpr(TryBlockExpr { syntax }), CAST_EXPR => Expr::CastExpr(CastExpr { syntax }), REF_EXPR => Expr::RefExpr(RefExpr { syntax }), PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }), @@ -3410,7 +3383,6 @@ impl AstNode for Expr { Expr::FieldExpr(it) => &it.syntax, Expr::AwaitExpr(it) => &it.syntax, Expr::TryExpr(it) => &it.syntax, - Expr::TryBlockExpr(it) => &it.syntax, Expr::CastExpr(it) => &it.syntax, Expr::RefExpr(it) => &it.syntax, Expr::PrefixExpr(it) => &it.syntax, @@ -3891,11 +3863,6 @@ impl std::fmt::Display for LoopExpr { std::fmt::Display::fmt(self.syntax(), f) } } -impl std::fmt::Display for TryBlockExpr { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} impl std::fmt::Display for ForExpr { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index c14804aad..bdd42cb76 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs @@ -162,7 +162,6 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc { "RECORD_LIT", "RECORD_FIELD_LIST", "RECORD_FIELD", - "TRY_BLOCK_EXPR", "BOX_EXPR", // postfix "CALL_EXPR", @@ -440,7 +439,6 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { } struct IfExpr: AttrsOwner { T![if], Condition } struct LoopExpr: AttrsOwner, LoopBodyOwner { T![loop] } - struct TryBlockExpr: AttrsOwner { T![try], body: BlockExpr } struct ForExpr: AttrsOwner, LoopBodyOwner { T![for], Pat, @@ -451,7 +449,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] } struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr } struct Label { T![lifetime] } - struct BlockExpr: AttrsOwner { Label, T![unsafe], T![async], Block } + struct BlockExpr: AttrsOwner { Label, T![unsafe], T![async], Block } struct ReturnExpr: AttrsOwner { Expr } struct CallExpr: ArgListOwner { Expr } struct MethodCallExpr: AttrsOwner, ArgListOwner { @@ -722,7 +720,6 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { FieldExpr, AwaitExpr, TryExpr, - TryBlockExpr, CastExpr, RefExpr, PrefixExpr, -- cgit v1.2.3 From 1865dedadf02c0ecc71a039645ad832d33881d30 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 22:43:06 +0200 Subject: Introduce BlockModifier --- crates/ra_syntax/src/ast.rs | 4 +++- crates/ra_syntax/src/ast/expr_extensions.rs | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index a716e525b..521ca8ab8 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -16,7 +16,9 @@ use crate::{ }; pub use self::{ - expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, + expr_extensions::{ + ArrayExprKind, BinOp, BlockModifier, ElseBranch, LiteralKind, PrefixOp, RangeOp, + }, extensions::{ AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 6aed7b4bb..352c0d2c5 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -359,7 +359,22 @@ impl ast::Literal { } } +pub enum BlockModifier { + Async(SyntaxToken), + Unsafe(SyntaxToken), +} + impl ast::BlockExpr { + pub fn modifier(&self) -> Option { + if let Some(token) = self.async_token() { + return Some(BlockModifier::Async(token)); + } + if let Some(token) = self.unsafe_token() { + return Some(BlockModifier::Unsafe(token)); + } + None + } + /// false if the block is an intrinsic part of the syntax and can't be /// replaced with arbitrary expression. /// @@ -368,7 +383,7 @@ impl ast::BlockExpr { /// const FOO: () = { stand_alone }; /// ``` pub fn is_standalone(&self) -> bool { - if self.unsafe_token().is_some() || self.async_token().is_some() { + if self.modifier().is_some() { return false; } let parent = match self.syntax().parent() { -- cgit v1.2.3 From 14126349be47ee28e7feb1ebb2dd1f0392537e56 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 22:44:42 +0200 Subject: Kill more zombies --- crates/ra_hir_def/src/expr.rs | 4 ---- crates/ra_hir_ty/src/infer/expr.rs | 5 ----- 2 files changed, 9 deletions(-) diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index a0cdad529..aad12e123 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -101,9 +101,6 @@ pub enum Expr { Try { expr: ExprId, }, - TryBlock { - body: ExprId, - }, Cast { expr: ExprId, type_ref: TypeRef, @@ -239,7 +236,6 @@ impl Expr { f(*expr); } } - Expr::TryBlock { body } => f(*body), Expr::Loop { body } => f(*body), Expr::While { condition, body } => { f(*condition); diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 83f946eee..efc60986b 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -73,11 +73,6 @@ impl<'a> InferenceContext<'a> { self.coerce_merge_branch(&then_ty, &else_ty) } Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected), - Expr::TryBlock { body } => { - let _inner = self.infer_expr(*body, expected); - // FIXME should be std::result::Result<{inner}, _> - Ty::Unknown - } Expr::Loop { body } => { self.infer_expr(*body, &Expectation::has_type(Ty::unit())); // FIXME handle break with value -- cgit v1.2.3