rustc_mir_build/builder/block.rs
1use rustc_middle::middle::region::Scope;
2use rustc_middle::mir::*;
3use rustc_middle::thir::*;
4use rustc_middle::{span_bug, ty};
5use rustc_span::Span;
6use tracing::debug;
7
8use crate::builder::ForGuard::OutsideGuard;
9use crate::builder::matches::{DeclareLetBindings, ScheduleDrops};
10use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
11
12impl<'a, 'tcx> Builder<'a, 'tcx> {
13 pub(crate) fn ast_block(
14 &mut self,
15 destination: Place<'tcx>,
16 block: BasicBlock,
17 ast_block: BlockId,
18 source_info: SourceInfo,
19 ) -> BlockAnd<()> {
20 let Block { region_scope, span, ref stmts, expr, targeted_by_break, safety_mode: _ } =
21 self.thir[ast_block];
22 self.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
23 if targeted_by_break {
24 this.in_breakable_scope(None, destination, span, |this| {
25 Some(this.ast_block_stmts(destination, block, span, stmts, expr, region_scope))
26 })
27 } else {
28 this.ast_block_stmts(destination, block, span, stmts, expr, region_scope)
29 }
30 })
31 }
32
33 fn ast_block_stmts(
34 &mut self,
35 destination: Place<'tcx>,
36 mut block: BasicBlock,
37 span: Span,
38 stmts: &[StmtId],
39 expr: Option<ExprId>,
40 region_scope: Scope,
41 ) -> BlockAnd<()> {
42 let this = self;
43
44 // This convoluted structure is to avoid using recursion as we walk down a list
45 // of statements. Basically, the structure we get back is something like:
46 //
47 // let x = <init> in {
48 // expr1;
49 // let y = <init> in {
50 // expr2;
51 // expr3;
52 // ...
53 // }
54 // }
55 //
56 // The let bindings are valid till the end of block so all we have to do is to pop all
57 // the let-scopes at the end.
58 //
59 // First we build all the statements in the block.
60 let mut let_scope_stack = Vec::with_capacity(8);
61 let outer_source_scope = this.source_scope;
62 // This scope information is kept for breaking out of the parent remainder scope in case
63 // one let-else pattern matching fails.
64 // By doing so, we can be sure that even temporaries that receive extended lifetime
65 // assignments are dropped, too.
66 let mut last_remainder_scope = region_scope;
67
68 let source_info = this.source_info(span);
69 for stmt in stmts {
70 let Stmt { ref kind } = this.thir[*stmt];
71 match kind {
72 StmtKind::Expr { scope, expr } => {
73 this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
74 let si = (*scope, source_info);
75 block = this
76 .in_scope(si, LintLevel::Inherited, |this| {
77 this.stmt_expr(block, *expr, Some(*scope))
78 })
79 .into_block();
80 }
81 StmtKind::Let {
82 remainder_scope,
83 init_scope,
84 pattern,
85 initializer: Some(initializer),
86 lint_level,
87 else_block: Some(else_block),
88 span: _,
89 } => {
90 // When lowering the statement `let <pat> = <expr> else { <else> };`,
91 // the `<else>` block is nested in the parent scope enclosing this statement.
92 // That scope is usually either the enclosing block scope,
93 // or the remainder scope of the last statement.
94 // This is to make sure that temporaries instantiated in `<expr>` are dropped
95 // as well.
96 // In addition, even though bindings in `<pat>` only come into scope if
97 // the pattern matching passes, in the MIR building the storages for them
98 // are declared as live any way.
99 // This is similar to `let x;` statements without an initializer expression,
100 // where the value of `x` in this example may or may be assigned,
101 // because the storage for their values may not be live after all due to
102 // failure in pattern matching.
103 // For this reason, we declare those storages as live but we do not schedule
104 // any drop yet- they are scheduled later after the pattern matching.
105 // The generated MIR will have `StorageDead` whenever the control flow breaks out
106 // of the parent scope, regardless of the result of the pattern matching.
107 // However, the drops are inserted in MIR only when the control flow breaks out of
108 // the scope of the remainder scope associated with this `let .. else` statement.
109 // Pictorial explanation of the scope structure:
110 // ┌─────────────────────────────────┐
111 // │ Scope of the enclosing block, │
112 // │ or the last remainder scope │
113 // │ ┌───────────────────────────┐ │
114 // │ │ Scope for <else> block │ │
115 // │ └───────────────────────────┘ │
116 // │ ┌───────────────────────────┐ │
117 // │ │ Remainder scope of │ │
118 // │ │ this let-else statement │ │
119 // │ │ ┌─────────────────────┐ │ │
120 // │ │ │ <expr> scope │ │ │
121 // │ │ └─────────────────────┘ │ │
122 // │ │ extended temporaries in │ │
123 // │ │ <expr> lives in this │ │
124 // │ │ scope │ │
125 // │ │ ┌─────────────────────┐ │ │
126 // │ │ │ Scopes for the rest │ │ │
127 // │ │ └─────────────────────┘ │ │
128 // │ └───────────────────────────┘ │
129 // └─────────────────────────────────┘
130 // Generated control flow:
131 // │ let Some(x) = y() else { return; }
132 // │
133 // ┌────────▼───────┐
134 // │ evaluate y() │
135 // └────────┬───────┘
136 // │ ┌────────────────┐
137 // ┌────────▼───────┐ │Drop temporaries│
138 // │Test the pattern├──────►in y() │
139 // └────────┬───────┘ │because breaking│
140 // │ │out of <expr> │
141 // ┌────────▼───────┐ │scope │
142 // │Move value into │ └───────┬────────┘
143 // │binding x │ │
144 // └────────┬───────┘ ┌───────▼────────┐
145 // │ │Drop extended │
146 // ┌────────▼───────┐ │temporaries in │
147 // │Drop temporaries│ │<expr> because │
148 // │in y() │ │breaking out of │
149 // │because breaking│ │remainder scope │
150 // │out of <expr> │ └───────┬────────┘
151 // │scope │ │
152 // └────────┬───────┘ ┌───────▼────────┐
153 // │ │Enter <else> ├────────►
154 // ┌────────▼───────┐ │block │ return;
155 // │Continue... │ └────────────────┘
156 // └────────────────┘
157
158 let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
159 this.block_context.push(BlockFrame::Statement { ignores_expr_result });
160
161 // Lower the `else` block first because its parent scope is actually
162 // enclosing the rest of the `let .. else ..` parts.
163 let else_block_span = this.thir[*else_block].span;
164 // This place is not really used because this destination place
165 // should never be used to take values at the end of the failure
166 // block.
167 let dummy_place = this.temp(this.tcx.types.never, else_block_span);
168 let failure_entry = this.cfg.start_new_block();
169 let failure_block;
170 failure_block = this
171 .ast_block(
172 dummy_place,
173 failure_entry,
174 *else_block,
175 this.source_info(else_block_span),
176 )
177 .into_block();
178 this.cfg.terminate(
179 failure_block,
180 this.source_info(else_block_span),
181 TerminatorKind::Unreachable,
182 );
183
184 // Declare the bindings, which may create a source scope.
185 let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);
186 this.push_scope((*remainder_scope, source_info));
187 let_scope_stack.push(remainder_scope);
188
189 let visibility_scope =
190 Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
191
192 let initializer_span = this.thir[*initializer].span;
193 let scope = (*init_scope, source_info);
194 let failure_and_block = this.in_scope(scope, *lint_level, |this| {
195 this.declare_bindings(
196 visibility_scope,
197 remainder_span,
198 pattern,
199 None,
200 Some((Some(&destination), initializer_span)),
201 );
202 let else_block_span = this.thir[*else_block].span;
203 let (matching, failure) =
204 this.in_if_then_scope(last_remainder_scope, else_block_span, |this| {
205 this.lower_let_expr(
206 block,
207 *initializer,
208 pattern,
209 None,
210 initializer_span,
211 DeclareLetBindings::No,
212 )
213 });
214 matching.and(failure)
215 });
216 let failure = unpack!(block = failure_and_block);
217 this.cfg.goto(failure, source_info, failure_entry);
218
219 if let Some(source_scope) = visibility_scope {
220 this.source_scope = source_scope;
221 }
222 last_remainder_scope = *remainder_scope;
223 }
224 StmtKind::Let { init_scope, initializer: None, else_block: Some(_), .. } => {
225 span_bug!(
226 init_scope.span(this.tcx, this.region_scope_tree),
227 "initializer is missing, but else block is present in this let binding",
228 )
229 }
230 StmtKind::Let {
231 remainder_scope,
232 init_scope,
233 pattern,
234 initializer,
235 lint_level,
236 else_block: None,
237 span: _,
238 } => {
239 let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
240 this.block_context.push(BlockFrame::Statement { ignores_expr_result });
241
242 // Enter the remainder scope, i.e., the bindings' destruction scope.
243 this.push_scope((*remainder_scope, source_info));
244 let_scope_stack.push(remainder_scope);
245
246 // Declare the bindings, which may create a source scope.
247 let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);
248
249 let visibility_scope =
250 Some(this.new_source_scope(remainder_span, LintLevel::Inherited));
251
252 // Evaluate the initializer, if present.
253 if let Some(init) = *initializer {
254 let initializer_span = this.thir[init].span;
255 let scope = (*init_scope, source_info);
256
257 block = this
258 .in_scope(scope, *lint_level, |this| {
259 this.declare_bindings(
260 visibility_scope,
261 remainder_span,
262 pattern,
263 None,
264 Some((None, initializer_span)),
265 );
266 this.expr_into_pattern(block, &pattern, init)
267 // irrefutable pattern
268 })
269 .into_block();
270 } else {
271 let scope = (*init_scope, source_info);
272 let _: BlockAnd<()> = this.in_scope(scope, *lint_level, |this| {
273 this.declare_bindings(
274 visibility_scope,
275 remainder_span,
276 pattern,
277 None,
278 None,
279 );
280 block.unit()
281 });
282
283 debug!("ast_block_stmts: pattern={:?}", pattern);
284 this.visit_primary_bindings(pattern, &mut |this, node, span| {
285 this.storage_live_binding(
286 block,
287 node,
288 span,
289 OutsideGuard,
290 ScheduleDrops::Yes,
291 );
292 this.schedule_drop_for_binding(node, span, OutsideGuard);
293 })
294 }
295
296 // Enter the visibility scope, after evaluating the initializer.
297 if let Some(source_scope) = visibility_scope {
298 this.source_scope = source_scope;
299 }
300 last_remainder_scope = *remainder_scope;
301 }
302 }
303
304 let popped = this.block_context.pop();
305 assert!(popped.is_some_and(|bf| bf.is_statement()));
306 }
307
308 // Then, the block may have an optional trailing expression which is a “return” value
309 // of the block, which is stored into `destination`.
310 let tcx = this.tcx;
311 let destination_ty = destination.ty(&this.local_decls, tcx).ty;
312 if let Some(expr_id) = expr {
313 let expr = &this.thir[expr_id];
314 let tail_result_is_ignored =
315 destination_ty.is_unit() || this.block_context.currently_ignores_tail_results();
316 this.block_context.push(BlockFrame::TailExpr {
317 info: BlockTailInfo { tail_result_is_ignored, span: expr.span },
318 });
319
320 block = this.expr_into_dest(destination, block, expr_id).into_block();
321 let popped = this.block_context.pop();
322
323 assert!(popped.is_some_and(|bf| bf.is_tail_expr()));
324 } else {
325 // If a block has no trailing expression, then it is given an implicit return type.
326 // This return type is usually `()`, unless the block is diverging, in which case the
327 // return type is `!`. For the unit type, we need to actually return the unit, but in
328 // the case of `!`, no return value is required, as the block will never return.
329 // Opaque types of empty bodies also need this unit assignment, in order to infer that their
330 // type is actually unit. Otherwise there will be no defining use found in the MIR.
331 if destination_ty.is_unit()
332 || matches!(destination_ty.kind(), ty::Alias(ty::Opaque, ..))
333 {
334 // We only want to assign an implicit `()` as the return value of the block if the
335 // block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
336 this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
337 }
338 }
339 // Finally, we pop all the let scopes before exiting out from the scope of block
340 // itself.
341 for scope in let_scope_stack.into_iter().rev() {
342 block = this.pop_scope((*scope, source_info), block).into_block();
343 }
344 // Restore the original source scope.
345 this.source_scope = outer_source_scope;
346 block.unit()
347 }
348}