From 0a11e80fa83cd3ace4ca306531d84a6c8138d549 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 17 Sep 2019 04:39:21 -0400 Subject: [PATCH] introduce `lower_block_expr` convenience function, and use it --- src/librustc/hir/lowering.rs | 7 +++++++ src/librustc/hir/lowering/expr.rs | 11 +++-------- src/librustc/hir/lowering/item.rs | 8 ++------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 492029eed05..48f7fc44465 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -2708,6 +2708,13 @@ fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P { }) } + /// Lowers a block directly to an expression, presuming that it + /// has no attributes and is not targeted by a `break`. + fn lower_block_expr(&mut self, b: &Block) -> hir::Expr { + let block = self.lower_block(b, false); + self.expr_block(block, ThinVec::new()) + } + fn lower_pat(&mut self, p: &Pat) -> P { let node = match p.node { PatKind::Wild => hir::PatKind::Wild, diff --git a/src/librustc/hir/lowering/expr.rs b/src/librustc/hir/lowering/expr.rs index 87462e94f43..ef0bef9d569 100644 --- a/src/librustc/hir/lowering/expr.rs +++ b/src/librustc/hir/lowering/expr.rs @@ -90,10 +90,7 @@ pub(super) fn lower_expr(&mut self, e: &Expr) -> hir::Expr { ), ExprKind::Async(capture_clause, closure_node_id, ref block) => { self.make_async_expr(capture_clause, closure_node_id, None, block.span, |this| { - this.with_new_scopes(|this| { - let block = this.lower_block(block, false); - this.expr_block(block, ThinVec::new()) - }) + this.with_new_scopes(|this| this.lower_block_expr(block)) }) } ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr), @@ -284,8 +281,7 @@ fn lower_expr_if( let else_arm = self.arm(hir_vec![else_pat], P(else_expr)); // Handle then + scrutinee: - let then_blk = self.lower_block(then, false); - let then_expr = self.expr_block(then_blk, ThinVec::new()); + let then_expr = self.lower_block_expr(then); let (then_pat, scrutinee, desugar) = match cond.node { // ` => `: ExprKind::Let(ref pat, ref scrutinee) => { @@ -335,8 +331,7 @@ fn lower_expr_while_in_loop_scope( }; // Handle then + scrutinee: - let then_blk = self.lower_block(body, false); - let then_expr = self.expr_block(then_blk, ThinVec::new()); + let then_expr = self.lower_block_expr(body); let (then_pat, scrutinee, desugar, source) = match cond.node { ExprKind::Let(ref pat, ref scrutinee) => { // to: diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index 1e621f7fdae..9ac3a6bdc87 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -1071,10 +1071,7 @@ pub(super) fn lower_fn_body( } fn lower_fn_body_block(&mut self, decl: &FnDecl, body: &Block) -> hir::BodyId { - self.lower_fn_body(decl, |this| { - let body = this.lower_block(body, false); - this.expr_block(body, ThinVec::new()) - }) + self.lower_fn_body(decl, |this| this.lower_block_expr(body)) } pub(super) fn lower_const_body(&mut self, expr: &Expr) -> hir::BodyId { @@ -1220,8 +1217,7 @@ fn lower_maybe_async_body( CaptureBy::Value, closure_id, None, body.span, |this| { // Create a block from the user's function body: - let user_body = this.lower_block(body, false); - let user_body = this.expr_block(user_body, ThinVec::new()); + let user_body = this.lower_block_expr(body); // Transform into `drop-temps { }`, an expression: let desugared_span = this.mark_span_with_reason( -- 2.44.0