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