]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | use crate::build::matches::ArmHasGuard; |
46de9a89 FG |
2 | use crate::build::ForGuard::OutsideGuard; |
3 | use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder}; | |
9fa01778 | 4 | use crate::hair::*; |
c30ab7b3 | 5 | use rustc::mir::*; |
46de9a89 FG |
6 | use rustc_hir as hir; |
7 | use rustc_span::Span; | |
e9174d1e | 8 | |
dc9dc135 | 9 | impl<'a, 'tcx> Builder<'a, 'tcx> { |
46de9a89 FG |
10 | crate fn ast_block( |
11 | &mut self, | |
12 | destination: &Place<'tcx>, | |
13 | block: BasicBlock, | |
14 | ast_block: &'tcx hir::Block<'tcx>, | |
15 | source_info: SourceInfo, | |
16 | ) -> BlockAnd<()> { | |
ea8adc8c XL |
17 | let Block { |
18 | region_scope, | |
19 | opt_destruction_scope, | |
20 | span, | |
21 | stmts, | |
22 | expr, | |
23 | targeted_by_break, | |
46de9a89 FG |
24 | safety_mode, |
25 | } = self.hir.mirror(ast_block); | |
26 | self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| { | |
dc9dc135 | 27 | this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| { |
041b39d2 | 28 | if targeted_by_break { |
94b46f34 | 29 | // This is a `break`-able block |
041b39d2 | 30 | let exit_block = this.cfg.start_new_block(); |
46de9a89 FG |
31 | let block_exit = |
32 | this.in_breakable_scope(None, exit_block, destination.clone(), |this| { | |
33 | this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode) | |
041b39d2 | 34 | }); |
46de9a89 | 35 | this.cfg.goto(unpack!(block_exit), source_info, exit_block); |
041b39d2 XL |
36 | exit_block.unit() |
37 | } else { | |
46de9a89 | 38 | this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode) |
041b39d2 XL |
39 | } |
40 | }) | |
cc61c64b XL |
41 | }) |
42 | } | |
3157f602 | 43 | |
46de9a89 FG |
44 | fn ast_block_stmts( |
45 | &mut self, | |
46 | destination: &Place<'tcx>, | |
47 | mut block: BasicBlock, | |
48 | span: Span, | |
49 | stmts: Vec<StmtRef<'tcx>>, | |
50 | expr: Option<ExprRef<'tcx>>, | |
51 | safety_mode: BlockSafety, | |
52 | ) -> BlockAnd<()> { | |
cc61c64b XL |
53 | let this = self; |
54 | ||
55 | // This convoluted structure is to avoid using recursion as we walk down a list | |
56 | // of statements. Basically, the structure we get back is something like: | |
57 | // | |
58 | // let x = <init> in { | |
59 | // expr1; | |
60 | // let y = <init> in { | |
61 | // expr2; | |
62 | // expr3; | |
63 | // ... | |
64 | // } | |
65 | // } | |
66 | // | |
67 | // The let bindings are valid till the end of block so all we have to do is to pop all | |
68 | // the let-scopes at the end. | |
69 | // | |
70 | // First we build all the statements in the block. | |
ea8adc8c | 71 | let mut let_scope_stack = Vec::with_capacity(8); |
94b46f34 | 72 | let outer_source_scope = this.source_scope; |
ea8adc8c XL |
73 | let outer_push_unsafe_count = this.push_unsafe_count; |
74 | let outer_unpushed_unsafe = this.unpushed_unsafe; | |
94b46f34 | 75 | this.update_source_scope_for_safety_mode(span, safety_mode); |
ea8adc8c | 76 | |
041b39d2 | 77 | let source_info = this.source_info(span); |
cc61c64b | 78 | for stmt in stmts { |
dc9dc135 | 79 | let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt); |
cc61c64b XL |
80 | match kind { |
81 | StmtKind::Expr { scope, expr } => { | |
0bf4aa26 | 82 | this.block_context.push(BlockFrame::Statement { ignores_expr_result: true }); |
46de9a89 FG |
83 | unpack!( |
84 | block = this.in_opt_scope( | |
85 | opt_destruction_scope.map(|de| (de, source_info)), | |
86 | |this| { | |
87 | let si = (scope, source_info); | |
88 | this.in_scope(si, LintLevel::Inherited, |this| { | |
89 | let expr = this.hir.mirror(expr); | |
90 | this.stmt_expr(block, expr, Some(scope)) | |
91 | }) | |
92 | } | |
93 | ) | |
94 | ); | |
cc61c64b | 95 | } |
46de9a89 FG |
96 | StmtKind::Let { remainder_scope, init_scope, pattern, initializer, lint_level } => { |
97 | let ignores_expr_result = | |
98 | if let PatKind::Wild = *pattern.kind { true } else { false }; | |
0bf4aa26 XL |
99 | this.block_context.push(BlockFrame::Statement { ignores_expr_result }); |
100 | ||
0731742a | 101 | // Enter the remainder scope, i.e., the bindings' destruction scope. |
3b2f2976 | 102 | this.push_scope((remainder_scope, source_info)); |
ea8adc8c | 103 | let_scope_stack.push(remainder_scope); |
3157f602 | 104 | |
94b46f34 | 105 | // Declare the bindings, which may create a source scope. |
46de9a89 FG |
106 | let remainder_span = |
107 | remainder_scope.span(this.hir.tcx(), &this.hir.region_scope_tree); | |
8faf50e0 | 108 | |
dc9dc135 XL |
109 | let visibility_scope = |
110 | Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None)); | |
3157f602 | 111 | |
cc61c64b XL |
112 | // Evaluate the initializer, if present. |
113 | if let Some(init) = initializer { | |
8faf50e0 XL |
114 | let initializer_span = init.span(); |
115 | ||
46de9a89 FG |
116 | unpack!( |
117 | block = this.in_opt_scope( | |
118 | opt_destruction_scope.map(|de| (de, source_info)), | |
119 | |this| { | |
120 | let scope = (init_scope, source_info); | |
121 | this.in_scope(scope, lint_level, |this| { | |
122 | this.declare_bindings( | |
123 | visibility_scope, | |
124 | remainder_span, | |
125 | &pattern, | |
126 | ArmHasGuard(false), | |
127 | Some((None, initializer_span)), | |
128 | ); | |
129 | this.expr_into_pattern(block, pattern, init) | |
130 | }) | |
131 | } | |
132 | ) | |
133 | ); | |
cc61c64b | 134 | } else { |
dc9dc135 XL |
135 | let scope = (init_scope, source_info); |
136 | unpack!(this.in_scope(scope, lint_level, |this| { | |
137 | this.declare_bindings( | |
138 | visibility_scope, | |
139 | remainder_span, | |
140 | &pattern, | |
141 | ArmHasGuard(false), | |
142 | None, | |
143 | ); | |
144 | block.unit() | |
145 | })); | |
8faf50e0 | 146 | |
0731742a | 147 | debug!("ast_block_stmts: pattern={:?}", pattern); |
0bf4aa26 XL |
148 | this.visit_bindings( |
149 | &pattern, | |
0731742a | 150 | UserTypeProjections::none(), |
0bf4aa26 XL |
151 | &mut |this, _, _, _, node, span, _, _| { |
152 | this.storage_live_binding(block, node, span, OutsideGuard); | |
153 | this.schedule_drop_for_binding(node, span, OutsideGuard); | |
46de9a89 FG |
154 | }, |
155 | ) | |
cc61c64b XL |
156 | } |
157 | ||
dc9dc135 XL |
158 | // Enter the visibility scope, after evaluating the initializer. |
159 | if let Some(source_scope) = visibility_scope { | |
94b46f34 | 160 | this.source_scope = source_scope; |
54a0048b | 161 | } |
9cc50fc6 SL |
162 | } |
163 | } | |
0bf4aa26 XL |
164 | |
165 | let popped = this.block_context.pop(); | |
46de9a89 | 166 | assert!(popped.map_or(false, |bf| bf.is_statement())); |
cc61c64b | 167 | } |
0bf4aa26 | 168 | |
cc61c64b | 169 | // Then, the block may have an optional trailing expression which is a “return” value |
0bf4aa26 XL |
170 | // of the block, which is stored into `destination`. |
171 | let tcx = this.hir.tcx(); | |
532ac7d7 | 172 | let destination_ty = destination.ty(&this.local_decls, tcx).ty; |
cc61c64b | 173 | if let Some(expr) = expr { |
46de9a89 FG |
174 | let tail_result_is_ignored = |
175 | destination_ty.is_unit() || this.block_context.currently_ignores_tail_results(); | |
0bf4aa26 XL |
176 | this.block_context.push(BlockFrame::TailExpr { tail_result_is_ignored }); |
177 | ||
cc61c64b | 178 | unpack!(block = this.into(destination, block, expr)); |
0bf4aa26 XL |
179 | let popped = this.block_context.pop(); |
180 | ||
46de9a89 | 181 | assert!(popped.map_or(false, |bf| bf.is_tail_expr())); |
cc61c64b | 182 | } else { |
2c00a5a8 XL |
183 | // If a block has no trailing expression, then it is given an implicit return type. |
184 | // This return type is usually `()`, unless the block is diverging, in which case the | |
185 | // return type is `!`. For the unit type, we need to actually return the unit, but in | |
186 | // the case of `!`, no return value is required, as the block will never return. | |
0bf4aa26 | 187 | if destination_ty.is_unit() { |
2c00a5a8 XL |
188 | // We only want to assign an implicit `()` as the return value of the block if the |
189 | // block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.) | |
190 | this.cfg.push_assign_unit(block, source_info, destination); | |
191 | } | |
cc61c64b XL |
192 | } |
193 | // Finally, we pop all the let scopes before exiting out from the scope of block | |
194 | // itself. | |
ea8adc8c XL |
195 | for scope in let_scope_stack.into_iter().rev() { |
196 | unpack!(block = this.pop_scope((scope, source_info), block)); | |
cc61c64b | 197 | } |
94b46f34 XL |
198 | // Restore the original source scope. |
199 | this.source_scope = outer_source_scope; | |
ea8adc8c XL |
200 | this.push_unsafe_count = outer_push_unsafe_count; |
201 | this.unpushed_unsafe = outer_unpushed_unsafe; | |
cc61c64b | 202 | block.unit() |
e9174d1e | 203 | } |
ea8adc8c | 204 | |
94b46f34 | 205 | /// If we are changing the safety mode, create a new source scope |
46de9a89 | 206 | fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) { |
94b46f34 | 207 | debug!("update_source_scope_for({:?}, {:?})", span, safety_mode); |
ea8adc8c XL |
208 | let new_unsafety = match safety_mode { |
209 | BlockSafety::Safe => None, | |
532ac7d7 | 210 | BlockSafety::ExplicitUnsafe(hir_id) => { |
ea8adc8c XL |
211 | assert_eq!(self.push_unsafe_count, 0); |
212 | match self.unpushed_unsafe { | |
213 | Safety::Safe => {} | |
46de9a89 | 214 | _ => return, |
ea8adc8c | 215 | } |
532ac7d7 XL |
216 | self.unpushed_unsafe = Safety::ExplicitUnsafe(hir_id); |
217 | Some(Safety::ExplicitUnsafe(hir_id)) | |
ea8adc8c XL |
218 | } |
219 | BlockSafety::PushUnsafe => { | |
220 | self.push_unsafe_count += 1; | |
221 | Some(Safety::BuiltinUnsafe) | |
222 | } | |
223 | BlockSafety::PopUnsafe => { | |
46de9a89 FG |
224 | self.push_unsafe_count = self |
225 | .push_unsafe_count | |
226 | .checked_sub(1) | |
227 | .unwrap_or_else(|| span_bug!(span, "unsafe count underflow")); | |
228 | if self.push_unsafe_count == 0 { Some(self.unpushed_unsafe) } else { None } | |
ea8adc8c XL |
229 | } |
230 | }; | |
231 | ||
232 | if let Some(unsafety) = new_unsafety { | |
46de9a89 | 233 | self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(unsafety)); |
ea8adc8c XL |
234 | } |
235 | } | |
e9174d1e | 236 | } |