1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use build::{BlockAnd, BlockAndExtension, Builder};
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17 pub fn ast_block(&mut self,
18 destination: &Lvalue<'tcx>,
19 // FIXME(#32959): temporary measure for the issue
21 mut block: BasicBlock,
22 ast_block: &'tcx hir::Block)
24 let Block { extent, span, stmts, expr } = self.hir.mirror(ast_block);
25 self.in_scope(extent, block, move |this| {
26 // This convoluted structure is to avoid using recursion as we walk down a list
27 // of statements. Basically, the structure we get back is something like:
29 // let x = <init> in {
31 // let y = <init> in {
38 // The let bindings are valid till the end of block so all we have to do is to pop all
39 // the let-scopes at the end.
41 // First we build all the statements in the block.
42 let mut let_extent_stack = Vec::with_capacity(8);
43 let outer_visibility_scope = this.visibility_scope;
45 let Stmt { span: _, kind } = this.hir.mirror(stmt);
47 StmtKind::Expr { scope, expr } => {
48 unpack!(block = this.in_scope(scope, block, |this| {
49 let expr = this.hir.mirror(expr);
50 this.stmt_expr(block, expr)
53 StmtKind::Let { remainder_scope, init_scope, pattern, initializer } => {
54 let tcx = this.hir.tcx();
56 // Enter the remainder scope, i.e. the bindings' destruction scope.
57 this.push_scope(remainder_scope);
58 let_extent_stack.push(remainder_scope);
60 // Declare the bindings, which may create a visibility scope.
61 let remainder_span = remainder_scope.span(&tcx.region_maps, &tcx.map);
62 let remainder_span = remainder_span.unwrap_or(span);
63 let scope = this.declare_bindings(None, remainder_span, &pattern);
65 // Evaluate the initializer, if present.
66 if let Some(init) = initializer {
67 unpack!(block = this.in_scope(init_scope, block, move |this| {
69 this.expr_into_pattern(block, pattern, init)
72 this.storage_live_for_bindings(block, &pattern);
75 // Enter the visibility scope, after evaluating the initializer.
76 if let Some(visibility_scope) = scope {
77 this.visibility_scope = visibility_scope;
82 // Then, the block may have an optional trailing expression which is a “return” value
84 if let Some(expr) = expr {
85 unpack!(block = this.into(destination, block, expr));
86 } else if dest_is_unit {
88 let source_info = this.source_info(span);
89 this.cfg.push_assign_unit(block, source_info, destination);
91 // Finally, we pop all the let scopes before exiting out from the scope of block
93 for extent in let_extent_stack.into_iter().rev() {
94 unpack!(block = this.pop_scope(extent, block));
96 // Restore the original visibility scope.
97 this.visibility_scope = outer_visibility_scope;