Skip to content

Commit 4f2134c

Browse files
committed
Introduce EffectExpr
1 parent 3c96de5 commit 4f2134c

File tree

25 files changed

+242
-261
lines changed

25 files changed

+242
-261
lines changed

crates/ra_assists/src/handlers/early_return.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{iter::once, ops::RangeInclusive};
22

33
use ra_syntax::{
44
algo::replace_children,
5-
ast::{self, edit::IndentLevel, make, Block, Pat::TupleStructPat},
5+
ast::{self, edit::IndentLevel, make},
66
AstNode,
77
SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE},
88
SyntaxNode,
@@ -47,7 +47,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
4747
// Check if there is an IfLet that we can handle.
4848
let if_let_pat = match cond.pat() {
4949
None => None, // No IfLet, supported.
50-
Some(TupleStructPat(pat)) if pat.args().count() == 1 => {
50+
Some(ast::Pat::TupleStructPat(pat)) if pat.args().count() == 1 => {
5151
let path = pat.path()?;
5252
match path.qualifier() {
5353
None => {
@@ -61,9 +61,9 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
6161
};
6262

6363
let cond_expr = cond.expr()?;
64-
let then_block = if_expr.then_branch()?.block()?;
64+
let then_block = if_expr.then_branch()?;
6565

66-
let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?;
66+
let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::BlockExpr::cast)?;
6767

6868
if parent_block.expr()? != if_expr.clone().into() {
6969
return None;
@@ -80,7 +80,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
8080
return None;
8181
}
8282

83-
let parent_container = parent_block.syntax().parent()?.parent()?;
83+
let parent_container = parent_block.syntax().parent()?;
8484

8585
let early_expression: ast::Expr = match parent_container.kind() {
8686
WHILE_EXPR | LOOP_EXPR => make::expr_continue(),
@@ -144,13 +144,13 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
144144
}
145145
};
146146
edit.target(if_expr.syntax().text_range());
147-
edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap());
147+
edit.replace_ast(parent_block, ast::BlockExpr::cast(new_block).unwrap());
148148
edit.set_cursor(cursor_position);
149149

150150
fn replace(
151151
new_expr: &SyntaxNode,
152-
then_block: &Block,
153-
parent_block: &Block,
152+
then_block: &ast::BlockExpr,
153+
parent_block: &ast::BlockExpr,
154154
if_expr: &ast::IfExpr,
155155
) -> SyntaxNode {
156156
let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone());

crates/ra_assists/src/handlers/inline_local_variable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
8989
| (ast::Expr::ParenExpr(_), _)
9090
| (ast::Expr::PathExpr(_), _)
9191
| (ast::Expr::BlockExpr(_), _)
92+
| (ast::Expr::EffectExpr(_), _)
9293
| (_, ast::Expr::CallExpr(_))
9394
| (_, ast::Expr::TupleExpr(_))
9495
| (_, ast::Expr::ArrayExpr(_))

crates/ra_assists/src/handlers/introduce_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
111111
/// expression like a lambda or match arm.
112112
fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
113113
expr.syntax().ancestors().find_map(|node| {
114-
if let Some(expr) = node.parent().and_then(ast::Block::cast).and_then(|it| it.expr()) {
114+
if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) {
115115
if expr.syntax() == &node {
116116
tested_by!(test_introduce_var_last_expr);
117117
return Some((node, false));

crates/ra_assists/src/handlers/move_guard.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option<Assist> {
113113
"Move condition to match guard",
114114
|edit| {
115115
edit.target(if_expr.syntax().text_range());
116-
let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none();
116+
let then_only_expr = then_block.statements().next().is_none();
117117

118-
match &then_block.block().and_then(|it| it.expr()) {
118+
match &then_block.expr() {
119119
Some(then_expr) if then_only_expr => {
120120
edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text())
121121
}

crates/ra_fmt/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
4242
}
4343

4444
pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
45-
let block = block.block()?;
4645
let has_anything_else = |thing: &SyntaxNode| -> bool {
4746
let mut non_trivial_children =
4847
block.syntax().children_with_tokens().filter(|it| match it.kind() {

crates/ra_hir_def/src/body/lower.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ impl ExprCollector<'_> {
203203

204204
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
205205
}
206-
ast::Expr::TryBlockExpr(e) => {
207-
let body = self.collect_block_opt(e.body());
208-
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
209-
}
206+
ast::Expr::EffectExpr(e) => match e.effect() {
207+
ast::Effect::Try(_) => {
208+
let body = self.collect_block_opt(e.block_expr());
209+
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
210+
}
211+
// FIXME: we need to record these effects somewhere...
212+
ast::Effect::Async(_) | ast::Effect::Label(_) | ast::Effect::Unsafe(_) => {
213+
self.collect_block_opt(e.block_expr())
214+
}
215+
},
210216
ast::Expr::BlockExpr(e) => self.collect_block(e),
211217
ast::Expr::LoopExpr(e) => {
212218
let body = self.collect_block_opt(e.loop_body());
@@ -494,12 +500,8 @@ impl ExprCollector<'_> {
494500
}
495501
}
496502

497-
fn collect_block(&mut self, expr: ast::BlockExpr) -> ExprId {
498-
let syntax_node_ptr = AstPtr::new(&expr.clone().into());
499-
let block = match expr.block() {
500-
Some(block) => block,
501-
None => return self.alloc_expr(Expr::Missing, syntax_node_ptr),
502-
};
503+
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
504+
let syntax_node_ptr = AstPtr::new(&block.clone().into());
503505
self.collect_block_items(&block);
504506
let statements = block
505507
.statements()
@@ -517,7 +519,7 @@ impl ExprCollector<'_> {
517519
self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr)
518520
}
519521

520-
fn collect_block_items(&mut self, block: &ast::Block) {
522+
fn collect_block_items(&mut self, block: &ast::BlockExpr) {
521523
let container = ContainerId::DefWithBodyId(self.def);
522524
for item in block.items() {
523525
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {

crates/ra_hir_expand/src/db.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
330330
FragmentKind::Expr
331331
}
332332
// FIXME: Expand to statements in appropriate positions; HIR lowering needs to handle that
333-
EXPR_STMT | BLOCK => FragmentKind::Expr,
333+
EXPR_STMT | BLOCK_EXPR => FragmentKind::Expr,
334334
ARG_LIST => FragmentKind::Expr,
335335
TRY_EXPR => FragmentKind::Expr,
336336
TUPLE_EXPR => FragmentKind::Expr,
@@ -342,7 +342,6 @@ fn to_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind {
342342
CONDITION => FragmentKind::Expr,
343343
BREAK_EXPR => FragmentKind::Expr,
344344
RETURN_EXPR => FragmentKind::Expr,
345-
BLOCK_EXPR => FragmentKind::Expr,
346345
MATCH_EXPR => FragmentKind::Expr,
347346
MATCH_ARM => FragmentKind::Expr,
348347
MATCH_GUARD => FragmentKind::Expr,

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a> CompletionContext<'a> {
344344
stmt.syntax().text_range() == name_ref.syntax().text_range(),
345345
);
346346
}
347-
if let Some(block) = ast::Block::cast(node) {
347+
if let Some(block) = ast::BlockExpr::cast(node) {
348348
return Some(
349349
block.expr().map(|e| e.syntax().text_range())
350350
== Some(name_ref.syntax().text_range()),

crates/ra_ide/src/folding_ranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
8888
| ITEM_LIST
8989
| EXTERN_ITEM_LIST
9090
| USE_TREE_LIST
91-
| BLOCK
91+
| BLOCK_EXPR
9292
| MATCH_ARM_LIST
9393
| ENUM_VARIANT_LIST
9494
| TOKEN_TREE => Some(FoldKind::Block),

crates/ra_ide/src/join_lines.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ fn has_comma_after(node: &SyntaxNode) -> bool {
129129
}
130130

131131
fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
132-
let block = ast::Block::cast(token.parent())?;
133-
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
132+
let block_expr = ast::BlockExpr::cast(token.parent())?;
134133
if !block_expr.is_standalone() {
135134
return None;
136135
}

0 commit comments

Comments
 (0)