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};
17 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
18 pub fn ast_block(&mut self,
19 destination: &Place<'tcx>,
21 ast_block: &'tcx hir::Block,
22 source_info: SourceInfo)
26 opt_destruction_scope,
33 self.hir.mirror(ast_block);
34 self.in_opt_scope(opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
35 this.in_scope((region_scope, source_info), LintLevel::Inherited, block, move |this| {
36 if targeted_by_break {
37 // This is a `break`-able block (currently only `catch { ... }`)
38 let exit_block = this.cfg.start_new_block();
39 let block_exit = this.in_breakable_scope(
40 None, exit_block, destination.clone(), |this| {
41 this.ast_block_stmts(destination, block, span, stmts, expr,
44 this.cfg.terminate(unpack!(block_exit), source_info,
45 TerminatorKind::Goto { target: exit_block });
48 this.ast_block_stmts(destination, block, span, stmts, expr,
55 fn ast_block_stmts(&mut self,
56 destination: &Place<'tcx>,
57 mut block: BasicBlock,
59 stmts: Vec<StmtRef<'tcx>>,
60 expr: Option<ExprRef<'tcx>>,
61 safety_mode: BlockSafety)
65 // This convoluted structure is to avoid using recursion as we walk down a list
66 // of statements. Basically, the structure we get back is something like:
68 // let x = <init> in {
70 // let y = <init> in {
77 // The let bindings are valid till the end of block so all we have to do is to pop all
78 // the let-scopes at the end.
80 // First we build all the statements in the block.
81 let mut let_scope_stack = Vec::with_capacity(8);
82 let outer_visibility_scope = this.visibility_scope;
83 let outer_push_unsafe_count = this.push_unsafe_count;
84 let outer_unpushed_unsafe = this.unpushed_unsafe;
85 this.update_visibility_scope_for_safety_mode(span, safety_mode);
87 let source_info = this.source_info(span);
89 let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
91 StmtKind::Expr { scope, expr } => {
92 unpack!(block = this.in_opt_scope(
93 opt_destruction_scope.map(|de|(de, source_info)), block, |this| {
94 let si = (scope, source_info);
95 this.in_scope(si, LintLevel::Inherited, block, |this| {
96 let expr = this.hir.mirror(expr);
97 this.stmt_expr(block, expr)
108 // Enter the remainder scope, i.e. the bindings' destruction scope.
109 this.push_scope((remainder_scope, source_info));
110 let_scope_stack.push(remainder_scope);
112 // Declare the bindings, which may create a visibility scope.
113 let remainder_span = remainder_scope.span(this.hir.tcx(),
114 &this.hir.region_scope_tree);
115 let scope = this.declare_bindings(None, remainder_span, lint_level, &pattern);
117 // Evaluate the initializer, if present.
118 if let Some(init) = initializer {
119 unpack!(block = this.in_opt_scope(
120 opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
121 let scope = (init_scope, source_info);
122 this.in_scope(scope, lint_level, block, move |this| {
124 this.expr_into_pattern(block, pattern, init)
128 this.visit_bindings(&pattern, &mut |this, _, _, node, span, _| {
129 this.storage_live_binding(block, node, span);
130 this.schedule_drop_for_binding(node, span);
134 // Enter the visibility scope, after evaluating the initializer.
135 if let Some(visibility_scope) = scope {
136 this.visibility_scope = visibility_scope;
141 // Then, the block may have an optional trailing expression which is a “return” value
143 if let Some(expr) = expr {
144 unpack!(block = this.into(destination, block, expr));
146 this.cfg.push_assign_unit(block, source_info, destination);
148 // Finally, we pop all the let scopes before exiting out from the scope of block
150 for scope in let_scope_stack.into_iter().rev() {
151 unpack!(block = this.pop_scope((scope, source_info), block));
153 // Restore the original visibility scope.
154 this.visibility_scope = outer_visibility_scope;
155 this.push_unsafe_count = outer_push_unsafe_count;
156 this.unpushed_unsafe = outer_unpushed_unsafe;
160 /// If we are changing the safety mode, create a new visibility scope
161 fn update_visibility_scope_for_safety_mode(&mut self,
163 safety_mode: BlockSafety)
165 debug!("update_visibility_scope_for({:?}, {:?})", span, safety_mode);
166 let new_unsafety = match safety_mode {
167 BlockSafety::Safe => None,
168 BlockSafety::ExplicitUnsafe(node_id) => {
169 assert_eq!(self.push_unsafe_count, 0);
170 match self.unpushed_unsafe {
174 self.unpushed_unsafe = Safety::ExplicitUnsafe(node_id);
175 Some(Safety::ExplicitUnsafe(node_id))
177 BlockSafety::PushUnsafe => {
178 self.push_unsafe_count += 1;
179 Some(Safety::BuiltinUnsafe)
181 BlockSafety::PopUnsafe => {
182 self.push_unsafe_count =
183 self.push_unsafe_count.checked_sub(1).unwrap_or_else(|| {
184 span_bug!(span, "unsafe count underflow")
186 if self.push_unsafe_count == 0 {
187 Some(self.unpushed_unsafe)
194 if let Some(unsafety) = new_unsafety {
195 self.visibility_scope = self.new_visibility_scope(
196 span, LintLevel::Inherited, Some(unsafety));