]> git.lizzy.rs Git - rust.git/commitdiff
introduce `lower_block_expr` convenience function, and use it
authorNiko Matsakis <niko@alum.mit.edu>
Tue, 17 Sep 2019 08:39:21 +0000 (04:39 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 17 Sep 2019 09:10:22 +0000 (05:10 -0400)
src/librustc/hir/lowering.rs
src/librustc/hir/lowering/expr.rs
src/librustc/hir/lowering/item.rs

index 492029eed05706adbcb1ff8f872bca13a5e0b300..48f7fc4446505369371b1964685155d742a2e981 100644 (file)
@@ -2708,6 +2708,13 @@ fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P<hir::Block> {
         })
     }
 
+    /// 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<hir::Pat> {
         let node = match p.node {
             PatKind::Wild => hir::PatKind::Wild,
index 87462e94f430d566e6d5e1213287e351367e0f58..ef0bef9d569a4446fe736657cb63c75ed0eb3b02 100644 (file)
@@ -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 {
             // `<pat> => <then>`:
             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:
index 1e621f7fdaea61c5783625e4612dbb0efff4dec5..9ac3a6bdc87af9a4e896283eee9c8641524c4a5c 100644 (file)
@@ -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 { <user-body> }`, an expression:
                     let desugared_span = this.mark_span_with_reason(