]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/block.rs
Auto merge of #47203 - varkor:output-filename-conflicts-with-directory, r=estebank
[rust.git] / src / librustc_mir / build / block.rs
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.
4 //
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.
10
11 use build::{BlockAnd, BlockAndExtension, Builder};
12 use hair::*;
13 use rustc::mir::*;
14 use rustc::hir;
15 use syntax_pos::Span;
16
17 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
18     pub fn ast_block(&mut self,
19                      destination: &Place<'tcx>,
20                      block: BasicBlock,
21                      ast_block: &'tcx hir::Block,
22                      source_info: SourceInfo)
23                      -> BlockAnd<()> {
24         let Block {
25             region_scope,
26             opt_destruction_scope,
27             span,
28             stmts,
29             expr,
30             targeted_by_break,
31             safety_mode
32         } =
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,
42                                                  safety_mode)
43                         });
44                     this.cfg.terminate(unpack!(block_exit), source_info,
45                                        TerminatorKind::Goto { target: exit_block });
46                     exit_block.unit()
47                 } else {
48                     this.ast_block_stmts(destination, block, span, stmts, expr,
49                                          safety_mode)
50                 }
51             })
52         })
53     }
54
55     fn ast_block_stmts(&mut self,
56                        destination: &Place<'tcx>,
57                        mut block: BasicBlock,
58                        span: Span,
59                        stmts: Vec<StmtRef<'tcx>>,
60                        expr: Option<ExprRef<'tcx>>,
61                        safety_mode: BlockSafety)
62                        -> BlockAnd<()> {
63         let this = self;
64
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:
67         //
68         //    let x = <init> in {
69         //       expr1;
70         //       let y = <init> in {
71         //           expr2;
72         //           expr3;
73         //           ...
74         //       }
75         //    }
76         //
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.
79         //
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);
86
87         let source_info = this.source_info(span);
88         for stmt in stmts {
89             let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
90             match kind {
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)
98                             })
99                         }));
100                 }
101                 StmtKind::Let {
102                     remainder_scope,
103                     init_scope,
104                     pattern,
105                     initializer,
106                     lint_level
107                 } => {
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);
111
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);
116
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| {
123                                     // FIXME #30046                             ^~~~
124                                     this.expr_into_pattern(block, pattern, init)
125                                 })
126                             }));
127                     } else {
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);
131                         })
132                     }
133
134                     // Enter the visibility scope, after evaluating the initializer.
135                     if let Some(visibility_scope) = scope {
136                         this.visibility_scope = visibility_scope;
137                     }
138                 }
139             }
140         }
141         // Then, the block may have an optional trailing expression which is a “return” value
142         // of the block.
143         if let Some(expr) = expr {
144             unpack!(block = this.into(destination, block, expr));
145         } else {
146             // If a block has no trailing expression, then it is given an implicit return type.
147             // This return type is usually `()`, unless the block is diverging, in which case the
148             // return type is `!`. For the unit type, we need to actually return the unit, but in
149             // the case of `!`, no return value is required, as the block will never return.
150             let tcx = this.hir.tcx();
151             let ty = destination.ty(&this.local_decls, tcx).to_ty(tcx);
152             if ty.is_nil() {
153                 // We only want to assign an implicit `()` as the return value of the block if the
154                 // block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
155                 this.cfg.push_assign_unit(block, source_info, destination);
156             }
157         }
158         // Finally, we pop all the let scopes before exiting out from the scope of block
159         // itself.
160         for scope in let_scope_stack.into_iter().rev() {
161             unpack!(block = this.pop_scope((scope, source_info), block));
162         }
163         // Restore the original visibility scope.
164         this.visibility_scope = outer_visibility_scope;
165         this.push_unsafe_count = outer_push_unsafe_count;
166         this.unpushed_unsafe = outer_unpushed_unsafe;
167         block.unit()
168     }
169
170     /// If we are changing the safety mode, create a new visibility scope
171     fn update_visibility_scope_for_safety_mode(&mut self,
172                                                span: Span,
173                                                safety_mode: BlockSafety)
174     {
175         debug!("update_visibility_scope_for({:?}, {:?})", span, safety_mode);
176         let new_unsafety = match safety_mode {
177             BlockSafety::Safe => None,
178             BlockSafety::ExplicitUnsafe(node_id) => {
179                 assert_eq!(self.push_unsafe_count, 0);
180                 match self.unpushed_unsafe {
181                     Safety::Safe => {}
182                     _ => return
183                 }
184                 self.unpushed_unsafe = Safety::ExplicitUnsafe(node_id);
185                 Some(Safety::ExplicitUnsafe(node_id))
186             }
187             BlockSafety::PushUnsafe => {
188                 self.push_unsafe_count += 1;
189                 Some(Safety::BuiltinUnsafe)
190             }
191             BlockSafety::PopUnsafe => {
192                 self.push_unsafe_count =
193                     self.push_unsafe_count.checked_sub(1).unwrap_or_else(|| {
194                         span_bug!(span, "unsafe count underflow")
195                     });
196                 if self.push_unsafe_count == 0 {
197                     Some(self.unpushed_unsafe)
198                 } else {
199                     None
200                 }
201             }
202         };
203
204         if let Some(unsafety) = new_unsafety {
205             self.visibility_scope = self.new_visibility_scope(
206                 span, LintLevel::Inherited, Some(unsafety));
207         }
208     }
209 }