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 mut block
: BasicBlock
,
20 ast_block
: &'tcx hir
::Block
)
22 let Block { extent, span, stmts, expr }
= self.hir
.mirror(ast_block
);
23 self.in_scope(extent
, block
, move |this
| {
24 // This convoluted structure is to avoid using recursion as we walk down a list
25 // of statements. Basically, the structure we get back is something like:
27 // let x = <init> in {
29 // let y = <init> in {
36 // The let bindings are valid till the end of block so all we have to do is to pop all
37 // the let-scopes at the end.
39 // First we build all the statements in the block.
40 let mut let_extent_stack
= Vec
::with_capacity(8);
41 let outer_visibility_scope
= this
.visibility_scope
;
43 let Stmt { span: _, kind }
= this
.hir
.mirror(stmt
);
45 StmtKind
::Expr { scope, expr }
=> {
46 unpack
!(block
= this
.in_scope(scope
, block
, |this
| {
47 let expr
= this
.hir
.mirror(expr
);
48 this
.stmt_expr(block
, expr
)
51 StmtKind
::Let { remainder_scope, init_scope, pattern, initializer }
=> {
52 let tcx
= this
.hir
.tcx();
54 // Enter the remainder scope, i.e. the bindings' destruction scope.
55 this
.push_scope(remainder_scope
);
56 let_extent_stack
.push(remainder_scope
);
58 // Declare the bindings, which may create a visibility scope.
59 let remainder_span
= remainder_scope
.span(&tcx
.region_maps
, &tcx
.hir
);
60 let remainder_span
= remainder_span
.unwrap_or(span
);
61 let scope
= this
.declare_bindings(None
, remainder_span
, &pattern
);
63 // Evaluate the initializer, if present.
64 if let Some(init
) = initializer
{
65 unpack
!(block
= this
.in_scope(init_scope
, block
, move |this
| {
67 this
.expr_into_pattern(block
, pattern
, init
)
70 this
.visit_bindings(&pattern
, &mut |this
, _
, _
, node
, span
, _
| {
71 this
.storage_live_binding(block
, node
, span
);
72 this
.schedule_drop_for_binding(node
, span
);
76 // Enter the visibility scope, after evaluating the initializer.
77 if let Some(visibility_scope
) = scope
{
78 this
.visibility_scope
= visibility_scope
;
83 // Then, the block may have an optional trailing expression which is a “return” value
85 if let Some(expr
) = expr
{
86 unpack
!(block
= this
.into(destination
, block
, expr
));
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
;