rustc_mir_build/builder/matches/mod.rs
1//! Code related to match expressions. These are sufficiently complex to
2//! warrant their own module and submodules. :) This main module includes the
3//! high-level algorithm, the submodules contain the details.
4//!
5//! This also includes code for pattern bindings in `let` statements and
6//! function parameters.
7
8use std::assert_matches::assert_matches;
9use std::borrow::Borrow;
10use std::mem;
11use std::sync::Arc;
12
13use rustc_abi::VariantIdx;
14use rustc_data_structures::fx::FxIndexMap;
15use rustc_data_structures::stack::ensure_sufficient_stack;
16use rustc_hir::{BindingMode, ByRef, LetStmt, LocalSource, Node};
17use rustc_middle::bug;
18use rustc_middle::middle::region;
19use rustc_middle::mir::{self, *};
20use rustc_middle::thir::{self, *};
21use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
22use rustc_span::{BytePos, Pos, Span, Symbol, sym};
23use tracing::{debug, instrument};
24
25use crate::builder::ForGuard::{self, OutsideGuard, RefWithinGuard};
26use crate::builder::expr::as_place::PlaceBuilder;
27use crate::builder::matches::user_ty::ProjectedUserTypesNode;
28use crate::builder::scope::DropKind;
29use crate::builder::{
30 BlockAnd, BlockAndExtension, Builder, GuardFrame, GuardFrameLocal, LocalsForNode,
31};
32
33// helper functions, broken out by category:
34mod match_pair;
35mod test;
36mod user_ty;
37mod util;
38
39/// Arguments to [`Builder::then_else_break_inner`] that are usually forwarded
40/// to recursive invocations.
41#[derive(Clone, Copy)]
42struct ThenElseArgs {
43 /// Used as the temp scope for lowering `expr`. If absent (for match guards),
44 /// `self.local_scope()` is used.
45 temp_scope_override: Option<region::Scope>,
46 variable_source_info: SourceInfo,
47 /// Determines how bindings should be handled when lowering `let` expressions.
48 ///
49 /// Forwarded to [`Builder::lower_let_expr`] when lowering [`ExprKind::Let`].
50 declare_let_bindings: DeclareLetBindings,
51}
52
53/// Should lowering a `let` expression also declare its bindings?
54///
55/// Used by [`Builder::lower_let_expr`] when lowering [`ExprKind::Let`].
56#[derive(Clone, Copy)]
57pub(crate) enum DeclareLetBindings {
58 /// Yes, declare `let` bindings as normal for `if` conditions.
59 Yes,
60 /// No, don't declare `let` bindings, because the caller declares them
61 /// separately due to special requirements.
62 ///
63 /// Used for match guards and let-else.
64 No,
65 /// Let expressions are not permitted in this context, so it is a bug to
66 /// try to lower one (e.g inside lazy-boolean-or or boolean-not).
67 LetNotPermitted,
68}
69
70/// Used by [`Builder::bind_matched_candidate_for_arm_body`] to determine
71/// whether or not to call [`Builder::storage_live_binding`] to emit
72/// [`StatementKind::StorageLive`].
73#[derive(Clone, Copy)]
74pub(crate) enum EmitStorageLive {
75 /// Yes, emit `StorageLive` as normal.
76 Yes,
77 /// No, don't emit `StorageLive`. The caller has taken responsibility for
78 /// emitting `StorageLive` as appropriate.
79 No,
80}
81
82/// Used by [`Builder::storage_live_binding`] and [`Builder::bind_matched_candidate_for_arm_body`]
83/// to decide whether to schedule drops.
84#[derive(Clone, Copy, Debug)]
85pub(crate) enum ScheduleDrops {
86 /// Yes, the relevant functions should also schedule drops as appropriate.
87 Yes,
88 /// No, don't schedule drops. The caller has taken responsibility for any
89 /// appropriate drops.
90 No,
91}
92
93impl<'a, 'tcx> Builder<'a, 'tcx> {
94 /// Lowers a condition in a way that ensures that variables bound in any let
95 /// expressions are definitely initialized in the if body.
96 ///
97 /// If `declare_let_bindings` is false then variables created in `let`
98 /// expressions will not be declared. This is for if let guards on arms with
99 /// an or pattern, where the guard is lowered multiple times.
100 pub(crate) fn then_else_break(
101 &mut self,
102 block: BasicBlock,
103 expr_id: ExprId,
104 temp_scope_override: Option<region::Scope>,
105 variable_source_info: SourceInfo,
106 declare_let_bindings: DeclareLetBindings,
107 ) -> BlockAnd<()> {
108 self.then_else_break_inner(
109 block,
110 expr_id,
111 ThenElseArgs { temp_scope_override, variable_source_info, declare_let_bindings },
112 )
113 }
114
115 fn then_else_break_inner(
116 &mut self,
117 block: BasicBlock, // Block that the condition and branch will be lowered into
118 expr_id: ExprId, // Condition expression to lower
119 args: ThenElseArgs,
120 ) -> BlockAnd<()> {
121 let this = self;
122 let expr = &this.thir[expr_id];
123 let expr_span = expr.span;
124
125 match expr.kind {
126 ExprKind::LogicalOp { op: op @ LogicalOp::And, lhs, rhs } => {
127 this.visit_coverage_branch_operation(op, expr_span);
128 let lhs_then_block = this.then_else_break_inner(block, lhs, args).into_block();
129 let rhs_then_block =
130 this.then_else_break_inner(lhs_then_block, rhs, args).into_block();
131 rhs_then_block.unit()
132 }
133 ExprKind::LogicalOp { op: op @ LogicalOp::Or, lhs, rhs } => {
134 this.visit_coverage_branch_operation(op, expr_span);
135 let local_scope = this.local_scope();
136 let (lhs_success_block, failure_block) =
137 this.in_if_then_scope(local_scope, expr_span, |this| {
138 this.then_else_break_inner(
139 block,
140 lhs,
141 ThenElseArgs {
142 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
143 ..args
144 },
145 )
146 });
147 let rhs_success_block = this
148 .then_else_break_inner(
149 failure_block,
150 rhs,
151 ThenElseArgs {
152 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
153 ..args
154 },
155 )
156 .into_block();
157
158 // Make the LHS and RHS success arms converge to a common block.
159 // (We can't just make LHS goto RHS, because `rhs_success_block`
160 // might contain statements that we don't want on the LHS path.)
161 let success_block = this.cfg.start_new_block();
162 this.cfg.goto(lhs_success_block, args.variable_source_info, success_block);
163 this.cfg.goto(rhs_success_block, args.variable_source_info, success_block);
164 success_block.unit()
165 }
166 ExprKind::Unary { op: UnOp::Not, arg } => {
167 // Improve branch coverage instrumentation by noting conditions
168 // nested within one or more `!` expressions.
169 // (Skipped if branch coverage is not enabled.)
170 if let Some(coverage_info) = this.coverage_info.as_mut() {
171 coverage_info.visit_unary_not(this.thir, expr_id);
172 }
173
174 let local_scope = this.local_scope();
175 let (success_block, failure_block) =
176 this.in_if_then_scope(local_scope, expr_span, |this| {
177 // Help out coverage instrumentation by injecting a dummy statement with
178 // the original condition's span (including `!`). This fixes #115468.
179 if this.tcx.sess.instrument_coverage() {
180 this.cfg.push_coverage_span_marker(block, this.source_info(expr_span));
181 }
182 this.then_else_break_inner(
183 block,
184 arg,
185 ThenElseArgs {
186 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
187 ..args
188 },
189 )
190 });
191 this.break_for_else(success_block, args.variable_source_info);
192 failure_block.unit()
193 }
194 ExprKind::Scope { region_scope, lint_level, value } => {
195 let region_scope = (region_scope, this.source_info(expr_span));
196 this.in_scope(region_scope, lint_level, |this| {
197 this.then_else_break_inner(block, value, args)
198 })
199 }
200 ExprKind::Use { source } => this.then_else_break_inner(block, source, args),
201 ExprKind::Let { expr, ref pat } => this.lower_let_expr(
202 block,
203 expr,
204 pat,
205 Some(args.variable_source_info.scope),
206 args.variable_source_info.span,
207 args.declare_let_bindings,
208 EmitStorageLive::Yes,
209 ),
210 _ => {
211 let mut block = block;
212 let temp_scope = args.temp_scope_override.unwrap_or_else(|| this.local_scope());
213 let mutability = Mutability::Mut;
214
215 // Increment the decision depth, in case we encounter boolean expressions
216 // further down.
217 this.mcdc_increment_depth_if_enabled();
218 let place = unpack!(
219 block = this.as_temp(
220 block,
221 TempLifetime {
222 temp_lifetime: Some(temp_scope),
223 backwards_incompatible: None
224 },
225 expr_id,
226 mutability
227 )
228 );
229 this.mcdc_decrement_depth_if_enabled();
230
231 let operand = Operand::Move(Place::from(place));
232
233 let then_block = this.cfg.start_new_block();
234 let else_block = this.cfg.start_new_block();
235 let term = TerminatorKind::if_(operand, then_block, else_block);
236
237 // Record branch coverage info for this condition.
238 // (Does nothing if branch coverage is not enabled.)
239 this.visit_coverage_branch_condition(expr_id, then_block, else_block);
240
241 let source_info = this.source_info(expr_span);
242 this.cfg.terminate(block, source_info, term);
243 this.break_for_else(else_block, source_info);
244
245 then_block.unit()
246 }
247 }
248 }
249
250 /// Generates MIR for a `match` expression.
251 ///
252 /// The MIR that we generate for a match looks like this.
253 ///
254 /// ```text
255 /// [ 0. Pre-match ]
256 /// |
257 /// [ 1. Evaluate Scrutinee (expression being matched on) ]
258 /// [ (PlaceMention of scrutinee) ]
259 /// |
260 /// [ 2. Decision tree -- check discriminants ] <--------+
261 /// | |
262 /// | (once a specific arm is chosen) |
263 /// | |
264 /// [pre_binding_block] [otherwise_block]
265 /// | |
266 /// [ 3. Create "guard bindings" for arm ] |
267 /// [ (create fake borrows) ] |
268 /// | |
269 /// [ 4. Execute guard code ] |
270 /// [ (read fake borrows) ] --(guard is false)-----------+
271 /// |
272 /// | (guard results in true)
273 /// |
274 /// [ 5. Create real bindings and execute arm ]
275 /// |
276 /// [ Exit match ]
277 /// ```
278 ///
279 /// All of the different arms have been stacked on top of each other to
280 /// simplify the diagram. For an arm with no guard the blocks marked 3 and
281 /// 4 and the fake borrows are omitted.
282 ///
283 /// We generate MIR in the following steps:
284 ///
285 /// 1. Evaluate the scrutinee and add the PlaceMention of it ([Builder::lower_scrutinee]).
286 /// 2. Create the decision tree ([Builder::lower_match_tree]).
287 /// 3. Determine the fake borrows that are needed from the places that were
288 /// matched against and create the required temporaries for them
289 /// ([util::collect_fake_borrows]).
290 /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]).
291 ///
292 /// ## False edges
293 ///
294 /// We don't want to have the exact structure of the decision tree be visible through borrow
295 /// checking. Specifically we want borrowck to think that:
296 /// - at any point, any or none of the patterns and guards seen so far may have been tested;
297 /// - after the match, any of the patterns may have matched.
298 ///
299 /// For example, all of these would fail to error if borrowck could see the real CFG (examples
300 /// taken from `tests/ui/nll/match-cfg-fake-edges.rs`):
301 /// ```ignore (too many errors, this is already in the test suite)
302 /// let x = String::new();
303 /// let _ = match true {
304 /// _ => {},
305 /// _ => drop(x),
306 /// };
307 /// // Borrowck must not know the second arm is never run.
308 /// drop(x); //~ ERROR use of moved value
309 ///
310 /// let x;
311 /// # let y = true;
312 /// match y {
313 /// _ if { x = 2; true } => {},
314 /// // Borrowck must not know the guard is always run.
315 /// _ => drop(x), //~ ERROR used binding `x` is possibly-uninitialized
316 /// };
317 ///
318 /// let x = String::new();
319 /// # let y = true;
320 /// match y {
321 /// false if { drop(x); true } => {},
322 /// // Borrowck must not know the guard is not run in the `true` case.
323 /// true => drop(x), //~ ERROR use of moved value: `x`
324 /// false => {},
325 /// };
326 ///
327 /// # let mut y = (true, true);
328 /// let r = &mut y.1;
329 /// match y {
330 /// //~^ ERROR cannot use `y.1` because it was mutably borrowed
331 /// (false, true) => {}
332 /// // Borrowck must not know we don't test `y.1` when `y.0` is `true`.
333 /// (true, _) => drop(r),
334 /// (false, _) => {}
335 /// };
336 /// ```
337 ///
338 /// We add false edges to act as if we were naively matching each arm in order. What we need is
339 /// a (fake) path from each candidate to the next, specifically from candidate C's pre-binding
340 /// block to next candidate D's pre-binding block. For maximum precision (needed for deref
341 /// patterns), we choose the earliest node on D's success path that doesn't also lead to C (to
342 /// avoid loops).
343 ///
344 /// This turns out to be easy to compute: that block is the `start_block` of the first call to
345 /// `match_candidates` where D is the first candidate in the list.
346 ///
347 /// For example:
348 /// ```rust
349 /// # let (x, y) = (true, true);
350 /// match (x, y) {
351 /// (true, true) => 1,
352 /// (false, true) => 2,
353 /// (true, false) => 3,
354 /// _ => 4,
355 /// }
356 /// # ;
357 /// ```
358 /// In this example, the pre-binding block of arm 1 has a false edge to the block for result
359 /// `false` of the first test on `x`. The other arms have false edges to the pre-binding blocks
360 /// of the next arm.
361 ///
362 /// On top of this, we also add a false edge from the otherwise_block of each guard to the
363 /// aforementioned start block of the next candidate, to ensure borrock doesn't rely on which
364 /// guards may have run.
365 #[instrument(level = "debug", skip(self, arms))]
366 pub(crate) fn match_expr(
367 &mut self,
368 destination: Place<'tcx>,
369 mut block: BasicBlock,
370 scrutinee_id: ExprId,
371 arms: &[ArmId],
372 span: Span,
373 scrutinee_span: Span,
374 ) -> BlockAnd<()> {
375 let scrutinee_place =
376 unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span));
377
378 let match_start_span = span.shrink_to_lo().to(scrutinee_span);
379 let patterns = arms
380 .iter()
381 .map(|&arm| {
382 let arm = &self.thir[arm];
383 let has_match_guard =
384 if arm.guard.is_some() { HasMatchGuard::Yes } else { HasMatchGuard::No };
385 (&*arm.pattern, has_match_guard)
386 })
387 .collect();
388 let built_tree = self.lower_match_tree(
389 block,
390 scrutinee_span,
391 &scrutinee_place,
392 match_start_span,
393 patterns,
394 false,
395 );
396
397 self.lower_match_arms(
398 destination,
399 scrutinee_place,
400 scrutinee_span,
401 arms,
402 built_tree,
403 self.source_info(span),
404 )
405 }
406
407 /// Evaluate the scrutinee and add the PlaceMention for it.
408 fn lower_scrutinee(
409 &mut self,
410 mut block: BasicBlock,
411 scrutinee_id: ExprId,
412 scrutinee_span: Span,
413 ) -> BlockAnd<PlaceBuilder<'tcx>> {
414 let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee_id));
415 if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
416 let source_info = self.source_info(scrutinee_span);
417 self.cfg.push_place_mention(block, source_info, scrutinee_place);
418 }
419
420 block.and(scrutinee_place_builder)
421 }
422
423 /// Lower the bindings, guards and arm bodies of a `match` expression.
424 ///
425 /// The decision tree should have already been created
426 /// (by [Builder::lower_match_tree]).
427 ///
428 /// `outer_source_info` is the SourceInfo for the whole match.
429 fn lower_match_arms(
430 &mut self,
431 destination: Place<'tcx>,
432 scrutinee_place_builder: PlaceBuilder<'tcx>,
433 scrutinee_span: Span,
434 arms: &[ArmId],
435 built_match_tree: BuiltMatchTree<'tcx>,
436 outer_source_info: SourceInfo,
437 ) -> BlockAnd<()> {
438 let arm_end_blocks: Vec<BasicBlock> = arms
439 .iter()
440 .map(|&arm| &self.thir[arm])
441 .zip(built_match_tree.branches)
442 .map(|(arm, branch)| {
443 debug!("lowering arm {:?}\ncorresponding branch = {:?}", arm, branch);
444
445 let arm_source_info = self.source_info(arm.span);
446 let arm_scope = (arm.scope, arm_source_info);
447 let match_scope = self.local_scope();
448 self.in_scope(arm_scope, arm.lint_level, |this| {
449 let old_dedup_scope =
450 mem::replace(&mut this.fixed_temps_scope, Some(arm.scope));
451
452 // `try_to_place` may fail if it is unable to resolve the given
453 // `PlaceBuilder` inside a closure. In this case, we don't want to include
454 // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
455 // if the only match arm is a wildcard (`_`).
456 // Example:
457 // ```
458 // let foo = (0, 1);
459 // let c = || {
460 // match foo { _ => () };
461 // };
462 // ```
463 let scrutinee_place = scrutinee_place_builder.try_to_place(this);
464 let opt_scrutinee_place =
465 scrutinee_place.as_ref().map(|place| (Some(place), scrutinee_span));
466 let scope = this.declare_bindings(
467 None,
468 arm.span,
469 &arm.pattern,
470 arm.guard,
471 opt_scrutinee_place,
472 );
473
474 let arm_block = this.bind_pattern(
475 outer_source_info,
476 branch,
477 &built_match_tree.fake_borrow_temps,
478 scrutinee_span,
479 Some((arm, match_scope)),
480 EmitStorageLive::Yes,
481 );
482
483 this.fixed_temps_scope = old_dedup_scope;
484
485 if let Some(source_scope) = scope {
486 this.source_scope = source_scope;
487 }
488
489 this.expr_into_dest(destination, arm_block, arm.body)
490 })
491 .into_block()
492 })
493 .collect();
494
495 // all the arm blocks will rejoin here
496 let end_block = self.cfg.start_new_block();
497
498 let end_brace = self.source_info(
499 outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
500 );
501 for arm_block in arm_end_blocks {
502 let block = &self.cfg.basic_blocks[arm_block];
503 let last_location = block.statements.last().map(|s| s.source_info);
504
505 self.cfg.goto(arm_block, last_location.unwrap_or(end_brace), end_block);
506 }
507
508 self.source_scope = outer_source_info.scope;
509
510 end_block.unit()
511 }
512
513 /// For a top-level `match` arm or a `let` binding, binds the variables and
514 /// ascribes types, and also checks the match arm guard (if present).
515 ///
516 /// `arm_scope` should be `Some` if and only if this is called for a
517 /// `match` arm.
518 ///
519 /// In the presence of or-patterns, a match arm might have multiple
520 /// sub-branches representing different ways to match, with each sub-branch
521 /// requiring its own bindings and its own copy of the guard. This method
522 /// handles those sub-branches individually, and then has them jump together
523 /// to a common block.
524 ///
525 /// Returns a single block that the match arm can be lowered into.
526 /// (For `let` bindings, this is the code that can use the bindings.)
527 fn bind_pattern(
528 &mut self,
529 outer_source_info: SourceInfo,
530 branch: MatchTreeBranch<'tcx>,
531 fake_borrow_temps: &[(Place<'tcx>, Local, FakeBorrowKind)],
532 scrutinee_span: Span,
533 arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
534 emit_storage_live: EmitStorageLive,
535 ) -> BasicBlock {
536 if branch.sub_branches.len() == 1 {
537 let [sub_branch] = branch.sub_branches.try_into().unwrap();
538 // Avoid generating another `BasicBlock` when we only have one sub branch.
539 self.bind_and_guard_matched_candidate(
540 sub_branch,
541 fake_borrow_temps,
542 scrutinee_span,
543 arm_match_scope,
544 ScheduleDrops::Yes,
545 emit_storage_live,
546 )
547 } else {
548 // It's helpful to avoid scheduling drops multiple times to save
549 // drop elaboration from having to clean up the extra drops.
550 //
551 // If we are in a `let` then we only schedule drops for the first
552 // candidate.
553 //
554 // If we're in a `match` arm then we could have a case like so:
555 //
556 // Ok(x) | Err(x) if return => { /* ... */ }
557 //
558 // In this case we don't want a drop of `x` scheduled when we
559 // return: it isn't bound by move until right before enter the arm.
560 // To handle this we instead unschedule it's drop after each time
561 // we lower the guard.
562 let target_block = self.cfg.start_new_block();
563 let mut schedule_drops = ScheduleDrops::Yes;
564 let arm = arm_match_scope.unzip().0;
565 // We keep a stack of all of the bindings and type ascriptions
566 // from the parent candidates that we visit, that also need to
567 // be bound for each candidate.
568 for sub_branch in branch.sub_branches {
569 if let Some(arm) = arm {
570 self.clear_top_scope(arm.scope);
571 }
572 let binding_end = self.bind_and_guard_matched_candidate(
573 sub_branch,
574 fake_borrow_temps,
575 scrutinee_span,
576 arm_match_scope,
577 schedule_drops,
578 emit_storage_live,
579 );
580 if arm.is_none() {
581 schedule_drops = ScheduleDrops::No;
582 }
583 self.cfg.goto(binding_end, outer_source_info, target_block);
584 }
585
586 target_block
587 }
588 }
589
590 pub(super) fn expr_into_pattern(
591 &mut self,
592 mut block: BasicBlock,
593 irrefutable_pat: &Pat<'tcx>,
594 initializer_id: ExprId,
595 ) -> BlockAnd<()> {
596 match irrefutable_pat.kind {
597 // Optimize the case of `let x = ...` to write directly into `x`
598 PatKind::Binding { mode: BindingMode(ByRef::No, _), var, subpattern: None, .. } => {
599 let place = self.storage_live_binding(
600 block,
601 var,
602 irrefutable_pat.span,
603 OutsideGuard,
604 ScheduleDrops::Yes,
605 );
606 block = self.expr_into_dest(place, block, initializer_id).into_block();
607
608 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
609 let source_info = self.source_info(irrefutable_pat.span);
610 self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place);
611
612 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
613 block.unit()
614 }
615
616 // Optimize the case of `let x: T = ...` to write directly
617 // into `x` and then require that `T == typeof(x)`.
618 PatKind::AscribeUserType {
619 ref subpattern,
620 ascription: thir::Ascription { ref annotation, variance: _ },
621 } if let PatKind::Binding {
622 mode: BindingMode(ByRef::No, _),
623 var,
624 subpattern: None,
625 ..
626 } = subpattern.kind =>
627 {
628 let place = self.storage_live_binding(
629 block,
630 var,
631 irrefutable_pat.span,
632 OutsideGuard,
633 ScheduleDrops::Yes,
634 );
635 block = self.expr_into_dest(place, block, initializer_id).into_block();
636
637 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
638 let pattern_source_info = self.source_info(irrefutable_pat.span);
639 let cause_let = FakeReadCause::ForLet(None);
640 self.cfg.push_fake_read(block, pattern_source_info, cause_let, place);
641
642 let ty_source_info = self.source_info(annotation.span);
643
644 let base = self.canonical_user_type_annotations.push(annotation.clone());
645 self.cfg.push(
646 block,
647 Statement {
648 source_info: ty_source_info,
649 kind: StatementKind::AscribeUserType(
650 Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
651 // We always use invariant as the variance here. This is because the
652 // variance field from the ascription refers to the variance to use
653 // when applying the type to the value being matched, but this
654 // ascription applies rather to the type of the binding. e.g., in this
655 // example:
656 //
657 // ```
658 // let x: T = <expr>
659 // ```
660 //
661 // We are creating an ascription that defines the type of `x` to be
662 // exactly `T` (i.e., with invariance). The variance field, in
663 // contrast, is intended to be used to relate `T` to the type of
664 // `<expr>`.
665 ty::Invariant,
666 ),
667 },
668 );
669
670 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
671 block.unit()
672 }
673
674 _ => {
675 let initializer = &self.thir[initializer_id];
676 let place_builder =
677 unpack!(block = self.lower_scrutinee(block, initializer_id, initializer.span));
678 self.place_into_pattern(block, irrefutable_pat, place_builder, true)
679 }
680 }
681 }
682
683 pub(crate) fn place_into_pattern(
684 &mut self,
685 block: BasicBlock,
686 irrefutable_pat: &Pat<'tcx>,
687 initializer: PlaceBuilder<'tcx>,
688 set_match_place: bool,
689 ) -> BlockAnd<()> {
690 let built_tree = self.lower_match_tree(
691 block,
692 irrefutable_pat.span,
693 &initializer,
694 irrefutable_pat.span,
695 vec![(irrefutable_pat, HasMatchGuard::No)],
696 false,
697 );
698 let [branch] = built_tree.branches.try_into().unwrap();
699
700 // For matches and function arguments, the place that is being matched
701 // can be set when creating the variables. But the place for
702 // let PATTERN = ... might not even exist until we do the assignment.
703 // so we set it here instead.
704 if set_match_place {
705 // `try_to_place` may fail if it is unable to resolve the given `PlaceBuilder` inside a
706 // closure. In this case, we don't want to include a scrutinee place.
707 // `scrutinee_place_builder` will fail for destructured assignments. This is because a
708 // closure only captures the precise places that it will read and as a result a closure
709 // may not capture the entire tuple/struct and rather have individual places that will
710 // be read in the final MIR.
711 // Example:
712 // ```
713 // let foo = (0, 1);
714 // let c = || {
715 // let (v1, v2) = foo;
716 // };
717 // ```
718 if let Some(place) = initializer.try_to_place(self) {
719 // Because or-alternatives bind the same variables, we only explore the first one.
720 let first_sub_branch = branch.sub_branches.first().unwrap();
721 for binding in &first_sub_branch.bindings {
722 let local = self.var_local_id(binding.var_id, OutsideGuard);
723 if let LocalInfo::User(BindingForm::Var(VarBindingForm {
724 opt_match_place: Some((ref mut match_place, _)),
725 ..
726 })) = **self.local_decls[local].local_info.as_mut().unwrap_crate_local()
727 {
728 *match_place = Some(place);
729 } else {
730 bug!("Let binding to non-user variable.")
731 };
732 }
733 }
734 }
735
736 self.bind_pattern(
737 self.source_info(irrefutable_pat.span),
738 branch,
739 &[],
740 irrefutable_pat.span,
741 None,
742 EmitStorageLive::Yes,
743 )
744 .unit()
745 }
746
747 /// Declares the bindings of the given patterns and returns the visibility
748 /// scope for the bindings in these patterns, if such a scope had to be
749 /// created. NOTE: Declaring the bindings should always be done in their
750 /// drop scope.
751 #[instrument(skip(self), level = "debug")]
752 pub(crate) fn declare_bindings(
753 &mut self,
754 mut visibility_scope: Option<SourceScope>,
755 scope_span: Span,
756 pattern: &Pat<'tcx>,
757 guard: Option<ExprId>,
758 opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
759 ) -> Option<SourceScope> {
760 self.visit_primary_bindings_special(
761 pattern,
762 &ProjectedUserTypesNode::None,
763 &mut |this, name, mode, var, span, ty, user_tys| {
764 let vis_scope = *visibility_scope
765 .get_or_insert_with(|| this.new_source_scope(scope_span, LintLevel::Inherited));
766 let source_info = SourceInfo { span, scope: this.source_scope };
767 let user_tys = user_tys.build_user_type_projections();
768
769 this.declare_binding(
770 source_info,
771 vis_scope,
772 name,
773 mode,
774 var,
775 ty,
776 user_tys,
777 ArmHasGuard(guard.is_some()),
778 opt_match_place.map(|(x, y)| (x.cloned(), y)),
779 pattern.span,
780 );
781 },
782 );
783 if let Some(guard_expr) = guard {
784 self.declare_guard_bindings(guard_expr, scope_span, visibility_scope);
785 }
786 visibility_scope
787 }
788
789 /// Declare bindings in a guard. This has to be done when declaring bindings
790 /// for an arm to ensure that or patterns only have one version of each
791 /// variable.
792 pub(crate) fn declare_guard_bindings(
793 &mut self,
794 guard_expr: ExprId,
795 scope_span: Span,
796 visibility_scope: Option<SourceScope>,
797 ) {
798 match self.thir.exprs[guard_expr].kind {
799 ExprKind::Let { expr: _, pat: ref guard_pat } => {
800 // FIXME: pass a proper `opt_match_place`
801 self.declare_bindings(visibility_scope, scope_span, guard_pat, None, None);
802 }
803 ExprKind::Scope { value, .. } => {
804 self.declare_guard_bindings(value, scope_span, visibility_scope);
805 }
806 ExprKind::Use { source } => {
807 self.declare_guard_bindings(source, scope_span, visibility_scope);
808 }
809 ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
810 self.declare_guard_bindings(lhs, scope_span, visibility_scope);
811 self.declare_guard_bindings(rhs, scope_span, visibility_scope);
812 }
813 _ => {}
814 }
815 }
816
817 /// Emits a [`StatementKind::StorageLive`] for the given var, and also
818 /// schedules a drop if requested (and possible).
819 pub(crate) fn storage_live_binding(
820 &mut self,
821 block: BasicBlock,
822 var: LocalVarId,
823 span: Span,
824 for_guard: ForGuard,
825 schedule_drop: ScheduleDrops,
826 ) -> Place<'tcx> {
827 let local_id = self.var_local_id(var, for_guard);
828 let source_info = self.source_info(span);
829 self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
830 // Although there is almost always scope for given variable in corner cases
831 // like #92893 we might get variable with no scope.
832 if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id)
833 && matches!(schedule_drop, ScheduleDrops::Yes)
834 {
835 self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
836 }
837 Place::from(local_id)
838 }
839
840 pub(crate) fn schedule_drop_for_binding(
841 &mut self,
842 var: LocalVarId,
843 span: Span,
844 for_guard: ForGuard,
845 ) {
846 let local_id = self.var_local_id(var, for_guard);
847 if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
848 self.schedule_drop(span, region_scope, local_id, DropKind::Value);
849 }
850 }
851
852 /// Visits all of the "primary" bindings in a pattern, i.e. the leftmost
853 /// occurrence of each variable bound by the pattern.
854 /// See [`PatKind::Binding::is_primary`] for more context.
855 ///
856 /// This variant provides only the limited subset of binding data needed
857 /// by its callers, and should be a "pure" visit without side-effects.
858 pub(super) fn visit_primary_bindings(
859 &mut self,
860 pattern: &Pat<'tcx>,
861 f: &mut impl FnMut(&mut Self, LocalVarId, Span),
862 ) {
863 pattern.walk_always(|pat| {
864 if let PatKind::Binding { var, is_primary: true, .. } = pat.kind {
865 f(self, var, pat.span);
866 }
867 })
868 }
869
870 /// Visits all of the "primary" bindings in a pattern, while preparing
871 /// additional user-type-annotation data needed by `declare_bindings`.
872 ///
873 /// This also has the side-effect of pushing all user type annotations
874 /// onto `canonical_user_type_annotations`, so that they end up in MIR
875 /// even if they aren't associated with any bindings.
876 #[instrument(level = "debug", skip(self, f))]
877 fn visit_primary_bindings_special(
878 &mut self,
879 pattern: &Pat<'tcx>,
880 user_tys: &ProjectedUserTypesNode<'_>,
881 f: &mut impl FnMut(
882 &mut Self,
883 Symbol,
884 BindingMode,
885 LocalVarId,
886 Span,
887 Ty<'tcx>,
888 &ProjectedUserTypesNode<'_>,
889 ),
890 ) {
891 // Avoid having to write the full method name at each recursive call.
892 let visit_subpat = |this: &mut Self, subpat, user_tys: &_, f: &mut _| {
893 this.visit_primary_bindings_special(subpat, user_tys, f)
894 };
895
896 match pattern.kind {
897 PatKind::Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
898 if is_primary {
899 f(self, name, mode, var, pattern.span, ty, user_tys);
900 }
901 if let Some(subpattern) = subpattern.as_ref() {
902 visit_subpat(self, subpattern, user_tys, f);
903 }
904 }
905
906 PatKind::Array { ref prefix, ref slice, ref suffix }
907 | PatKind::Slice { ref prefix, ref slice, ref suffix } => {
908 let from = u64::try_from(prefix.len()).unwrap();
909 let to = u64::try_from(suffix.len()).unwrap();
910 for subpattern in prefix.iter() {
911 visit_subpat(self, subpattern, &user_tys.index(), f);
912 }
913 if let Some(subpattern) = slice {
914 visit_subpat(self, subpattern, &user_tys.subslice(from, to), f);
915 }
916 for subpattern in suffix.iter() {
917 visit_subpat(self, subpattern, &user_tys.index(), f);
918 }
919 }
920
921 PatKind::Constant { .. }
922 | PatKind::Range { .. }
923 | PatKind::Missing
924 | PatKind::Wild
925 | PatKind::Never
926 | PatKind::Error(_) => {}
927
928 PatKind::Deref { ref subpattern } => {
929 visit_subpat(self, subpattern, &user_tys.deref(), f);
930 }
931
932 PatKind::DerefPattern { ref subpattern, .. } => {
933 visit_subpat(self, subpattern, &ProjectedUserTypesNode::None, f);
934 }
935
936 PatKind::AscribeUserType {
937 ref subpattern,
938 ascription: thir::Ascription { ref annotation, variance: _ },
939 } => {
940 // This corresponds to something like
941 //
942 // ```
943 // let A::<'a>(_): A<'static> = ...;
944 // ```
945 //
946 // Note that the variance doesn't apply here, as we are tracking the effect
947 // of `user_ty` on any bindings contained with subpattern.
948
949 // Caution: Pushing this user type here is load-bearing even for
950 // patterns containing no bindings, to ensure that the type ends
951 // up represented in MIR _somewhere_.
952 let base_user_ty = self.canonical_user_type_annotations.push(annotation.clone());
953 let subpattern_user_tys = user_tys.push_user_type(base_user_ty);
954 visit_subpat(self, subpattern, &subpattern_user_tys, f)
955 }
956
957 PatKind::ExpandedConstant { ref subpattern, .. } => {
958 visit_subpat(self, subpattern, user_tys, f)
959 }
960
961 PatKind::Leaf { ref subpatterns } => {
962 for subpattern in subpatterns {
963 let subpattern_user_tys = user_tys.leaf(subpattern.field);
964 debug!("visit_primary_bindings: subpattern_user_tys={subpattern_user_tys:?}");
965 visit_subpat(self, &subpattern.pattern, &subpattern_user_tys, f);
966 }
967 }
968
969 PatKind::Variant { adt_def, args: _, variant_index, ref subpatterns } => {
970 for subpattern in subpatterns {
971 let subpattern_user_tys =
972 user_tys.variant(adt_def, variant_index, subpattern.field);
973 visit_subpat(self, &subpattern.pattern, &subpattern_user_tys, f);
974 }
975 }
976 PatKind::Or { ref pats } => {
977 // In cases where we recover from errors the primary bindings
978 // may not all be in the leftmost subpattern. For example in
979 // `let (x | y) = ...`, the primary binding of `y` occurs in
980 // the right subpattern
981 for subpattern in pats.iter() {
982 visit_subpat(self, subpattern, user_tys, f);
983 }
984 }
985 }
986 }
987}
988
989/// Data extracted from a pattern that doesn't affect which branch is taken. Collected during
990/// pattern simplification and not mutated later.
991#[derive(Debug, Clone)]
992struct PatternExtraData<'tcx> {
993 /// [`Span`] of the original pattern.
994 span: Span,
995
996 /// Bindings that must be established.
997 bindings: Vec<Binding<'tcx>>,
998
999 /// Types that must be asserted.
1000 ascriptions: Vec<Ascription<'tcx>>,
1001
1002 /// Whether this corresponds to a never pattern.
1003 is_never: bool,
1004}
1005
1006impl<'tcx> PatternExtraData<'tcx> {
1007 fn is_empty(&self) -> bool {
1008 self.bindings.is_empty() && self.ascriptions.is_empty()
1009 }
1010}
1011
1012/// A pattern in a form suitable for lowering the match tree, with all irrefutable
1013/// patterns simplified away.
1014///
1015/// Here, "flat" indicates that irrefutable nodes in the pattern tree have been
1016/// recursively replaced with their refutable subpatterns. They are not
1017/// necessarily flat in an absolute sense.
1018///
1019/// Will typically be incorporated into a [`Candidate`].
1020#[derive(Debug, Clone)]
1021struct FlatPat<'tcx> {
1022 /// To match the pattern, all of these must be satisfied...
1023 match_pairs: Vec<MatchPairTree<'tcx>>,
1024
1025 extra_data: PatternExtraData<'tcx>,
1026}
1027
1028impl<'tcx> FlatPat<'tcx> {
1029 /// Creates a `FlatPat` containing a simplified [`MatchPairTree`] list/forest
1030 /// for the given pattern.
1031 fn new(place: PlaceBuilder<'tcx>, pattern: &Pat<'tcx>, cx: &mut Builder<'_, 'tcx>) -> Self {
1032 // Recursively build a tree of match pairs for the given pattern.
1033 let mut match_pairs = vec![];
1034 let mut extra_data = PatternExtraData {
1035 span: pattern.span,
1036 bindings: Vec::new(),
1037 ascriptions: Vec::new(),
1038 is_never: pattern.is_never_pattern(),
1039 };
1040 MatchPairTree::for_pattern(place, pattern, cx, &mut match_pairs, &mut extra_data);
1041
1042 Self { match_pairs, extra_data }
1043 }
1044}
1045
1046/// Candidates are a generalization of (a) top-level match arms, and
1047/// (b) sub-branches of or-patterns, allowing the match-lowering process to handle
1048/// them both in a mostly-uniform way. For example, the list of candidates passed
1049/// to [`Builder::match_candidates`] will often contain a mixture of top-level
1050/// candidates and or-pattern subcandidates.
1051///
1052/// At the start of match lowering, there is one candidate for each match arm.
1053/// During match lowering, arms with or-patterns will be expanded into a tree
1054/// of candidates, where each "leaf" candidate represents one of the ways for
1055/// the arm pattern to successfully match.
1056#[derive(Debug)]
1057struct Candidate<'tcx> {
1058 /// For the candidate to match, all of these must be satisfied...
1059 ///
1060 /// ---
1061 /// Initially contains a list of match pairs created by [`FlatPat`], but is
1062 /// subsequently mutated (in a queue-like way) while lowering the match tree.
1063 /// When this list becomes empty, the candidate is fully matched and becomes
1064 /// a leaf (see [`Builder::select_matched_candidate`]).
1065 ///
1066 /// Key mutations include:
1067 ///
1068 /// - When a match pair is fully satisfied by a test, it is removed from the
1069 /// list, and its subpairs are added instead (see [`Builder::sort_candidate`]).
1070 /// - During or-pattern expansion, any leading or-pattern is removed, and is
1071 /// converted into subcandidates (see [`Builder::expand_and_match_or_candidates`]).
1072 /// - After a candidate's subcandidates have been lowered, a copy of any remaining
1073 /// or-patterns is added to each leaf subcandidate
1074 /// (see [`Builder::test_remaining_match_pairs_after_or`]).
1075 ///
1076 /// Invariants:
1077 /// - All or-patterns ([`TestCase::Or`]) have been sorted to the end.
1078 match_pairs: Vec<MatchPairTree<'tcx>>,
1079
1080 /// ...and if this is non-empty, one of these subcandidates also has to match...
1081 ///
1082 /// ---
1083 /// Initially a candidate has no subcandidates; they are added (and then immediately
1084 /// lowered) during or-pattern expansion. Their main function is to serve as _output_
1085 /// of match tree lowering, allowing later steps to see the leaf candidates that
1086 /// represent a match of the entire match arm.
1087 ///
1088 /// A candidate no subcandidates is either incomplete (if it has match pairs left),
1089 /// or is a leaf in the match tree. A candidate with one or more subcandidates is
1090 /// an internal node in the match tree.
1091 ///
1092 /// Invariant: at the end of match tree lowering, this must not contain an
1093 /// `is_never` candidate, because that would break binding consistency.
1094 /// - See [`Builder::remove_never_subcandidates`].
1095 subcandidates: Vec<Candidate<'tcx>>,
1096
1097 /// ...and if there is a guard it must be evaluated; if it's `false` then branch to `otherwise_block`.
1098 ///
1099 /// ---
1100 /// For subcandidates, this is copied from the parent candidate, so it indicates
1101 /// whether the enclosing match arm has a guard.
1102 has_guard: bool,
1103
1104 /// Holds extra pattern data that was prepared by [`FlatPat`], including bindings and
1105 /// ascriptions that must be established if this candidate succeeds.
1106 extra_data: PatternExtraData<'tcx>,
1107
1108 /// When setting `self.subcandidates`, we store here the span of the or-pattern they came from.
1109 ///
1110 /// ---
1111 /// Invariant: it is `None` iff `subcandidates.is_empty()`.
1112 /// - FIXME: We sometimes don't unset this when clearing `subcandidates`.
1113 or_span: Option<Span>,
1114
1115 /// The block before the `bindings` have been established.
1116 ///
1117 /// After the match tree has been lowered, [`Builder::lower_match_arms`]
1118 /// will use this as the start point for lowering bindings and guards, and
1119 /// then jump to a shared block containing the arm body.
1120 pre_binding_block: Option<BasicBlock>,
1121
1122 /// The block to branch to if the guard or a nested candidate fails to match.
1123 otherwise_block: Option<BasicBlock>,
1124
1125 /// The earliest block that has only candidates >= this one as descendents. Used for false
1126 /// edges, see the doc for [`Builder::match_expr`].
1127 false_edge_start_block: Option<BasicBlock>,
1128}
1129
1130impl<'tcx> Candidate<'tcx> {
1131 fn new(
1132 place: PlaceBuilder<'tcx>,
1133 pattern: &Pat<'tcx>,
1134 has_guard: HasMatchGuard,
1135 cx: &mut Builder<'_, 'tcx>,
1136 ) -> Self {
1137 // Use `FlatPat` to build simplified match pairs, then immediately
1138 // incorporate them into a new candidate.
1139 Self::from_flat_pat(
1140 FlatPat::new(place, pattern, cx),
1141 matches!(has_guard, HasMatchGuard::Yes),
1142 )
1143 }
1144
1145 /// Incorporates an already-simplified [`FlatPat`] into a new candidate.
1146 fn from_flat_pat(flat_pat: FlatPat<'tcx>, has_guard: bool) -> Self {
1147 let mut this = Candidate {
1148 match_pairs: flat_pat.match_pairs,
1149 extra_data: flat_pat.extra_data,
1150 has_guard,
1151 subcandidates: Vec::new(),
1152 or_span: None,
1153 otherwise_block: None,
1154 pre_binding_block: None,
1155 false_edge_start_block: None,
1156 };
1157 this.sort_match_pairs();
1158 this
1159 }
1160
1161 /// Restores the invariant that or-patterns must be sorted to the end.
1162 fn sort_match_pairs(&mut self) {
1163 self.match_pairs.sort_by_key(|pair| matches!(pair.test_case, TestCase::Or { .. }));
1164 }
1165
1166 /// Returns whether the first match pair of this candidate is an or-pattern.
1167 fn starts_with_or_pattern(&self) -> bool {
1168 matches!(&*self.match_pairs, [MatchPairTree { test_case: TestCase::Or { .. }, .. }, ..])
1169 }
1170
1171 /// Visit the leaf candidates (those with no subcandidates) contained in
1172 /// this candidate.
1173 fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1174 traverse_candidate(
1175 self,
1176 &mut (),
1177 &mut move |c, _| visit_leaf(c),
1178 move |c, _| c.subcandidates.iter_mut(),
1179 |_| {},
1180 );
1181 }
1182
1183 /// Visit the leaf candidates in reverse order.
1184 fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1185 traverse_candidate(
1186 self,
1187 &mut (),
1188 &mut move |c, _| visit_leaf(c),
1189 move |c, _| c.subcandidates.iter_mut().rev(),
1190 |_| {},
1191 );
1192 }
1193}
1194
1195/// A depth-first traversal of the `Candidate` and all of its recursive
1196/// subcandidates.
1197///
1198/// This signature is very generic, to support traversing candidate trees by
1199/// reference or by value, and to allow a mutable "context" to be shared by the
1200/// traversal callbacks. Most traversals can use the simpler
1201/// [`Candidate::visit_leaves`] wrapper instead.
1202fn traverse_candidate<'tcx, C, T, I>(
1203 candidate: C,
1204 context: &mut T,
1205 // Called when visiting a "leaf" candidate (with no subcandidates).
1206 visit_leaf: &mut impl FnMut(C, &mut T),
1207 // Called when visiting a "node" candidate (with one or more subcandidates).
1208 // Returns an iterator over the candidate's children (by value or reference).
1209 // Can perform setup before visiting the node's children.
1210 get_children: impl Copy + Fn(C, &mut T) -> I,
1211 // Called after visiting a "node" candidate's children.
1212 complete_children: impl Copy + Fn(&mut T),
1213) where
1214 C: Borrow<Candidate<'tcx>>, // Typically `Candidate` or `&mut Candidate`
1215 I: Iterator<Item = C>,
1216{
1217 if candidate.borrow().subcandidates.is_empty() {
1218 visit_leaf(candidate, context)
1219 } else {
1220 for child in get_children(candidate, context) {
1221 traverse_candidate(child, context, visit_leaf, get_children, complete_children);
1222 }
1223 complete_children(context)
1224 }
1225}
1226
1227#[derive(Clone, Debug)]
1228struct Binding<'tcx> {
1229 span: Span,
1230 source: Place<'tcx>,
1231 var_id: LocalVarId,
1232 binding_mode: BindingMode,
1233}
1234
1235/// Indicates that the type of `source` must be a subtype of the
1236/// user-given type `user_ty`; this is basically a no-op but can
1237/// influence region inference.
1238#[derive(Clone, Debug)]
1239struct Ascription<'tcx> {
1240 source: Place<'tcx>,
1241 annotation: CanonicalUserTypeAnnotation<'tcx>,
1242 variance: ty::Variance,
1243}
1244
1245/// Partial summary of a [`thir::Pat`], indicating what sort of test should be
1246/// performed to match/reject the pattern, and what the desired test outcome is.
1247/// This avoids having to perform a full match on [`thir::PatKind`] in some places,
1248/// and helps [`TestKind::Switch`] and [`TestKind::SwitchInt`] know what target
1249/// values to use.
1250///
1251/// Created by [`MatchPairTree::for_pattern`], and then inspected primarily by:
1252/// - [`Builder::pick_test_for_match_pair`] (to choose a test)
1253/// - [`Builder::sort_candidate`] (to see how the test interacts with a match pair)
1254///
1255/// Note that or-patterns are not tested directly like the other variants.
1256/// Instead they participate in or-pattern expansion, where they are transformed into
1257/// subcandidates. See [`Builder::expand_and_match_or_candidates`].
1258#[derive(Debug, Clone)]
1259enum TestCase<'tcx> {
1260 Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx },
1261 Constant { value: mir::Const<'tcx> },
1262 Range(Arc<PatRange<'tcx>>),
1263 Slice { len: usize, variable_length: bool },
1264 Deref { temp: Place<'tcx>, mutability: Mutability },
1265 Never,
1266 Or { pats: Box<[FlatPat<'tcx>]> },
1267}
1268
1269impl<'tcx> TestCase<'tcx> {
1270 fn as_range(&self) -> Option<&PatRange<'tcx>> {
1271 if let Self::Range(v) = self { Some(v.as_ref()) } else { None }
1272 }
1273}
1274
1275/// Node in a tree of "match pairs", where each pair consists of a place to be
1276/// tested, and a test to perform on that place.
1277///
1278/// Each node also has a list of subpairs (possibly empty) that must also match,
1279/// and a reference to the THIR pattern it represents.
1280#[derive(Debug, Clone)]
1281pub(crate) struct MatchPairTree<'tcx> {
1282 /// This place...
1283 ///
1284 /// ---
1285 /// This can be `None` if it referred to a non-captured place in a closure.
1286 ///
1287 /// Invariant: Can only be `None` when `test_case` is `Or`.
1288 /// Therefore this must be `Some(_)` after or-pattern expansion.
1289 place: Option<Place<'tcx>>,
1290
1291 /// ... must pass this test...
1292 test_case: TestCase<'tcx>,
1293
1294 /// ... and these subpairs must match.
1295 ///
1296 /// ---
1297 /// Subpairs typically represent tests that can only be performed after their
1298 /// parent has succeeded. For example, the pattern `Some(3)` might have an
1299 /// outer match pair that tests for the variant `Some`, and then a subpair
1300 /// that tests its field for the value `3`.
1301 subpairs: Vec<Self>,
1302
1303 /// Type field of the pattern this node was created from.
1304 pattern_ty: Ty<'tcx>,
1305 /// Span field of the pattern this node was created from.
1306 pattern_span: Span,
1307}
1308
1309/// See [`Test`] for more.
1310#[derive(Clone, Debug, PartialEq)]
1311enum TestKind<'tcx> {
1312 /// Test what enum variant a value is.
1313 ///
1314 /// The subset of expected variants is not stored here; instead they are
1315 /// extracted from the [`TestCase`]s of the candidates participating in the
1316 /// test.
1317 Switch {
1318 /// The enum type being tested.
1319 adt_def: ty::AdtDef<'tcx>,
1320 },
1321
1322 /// Test what value an integer or `char` has.
1323 ///
1324 /// The test's target values are not stored here; instead they are extracted
1325 /// from the [`TestCase`]s of the candidates participating in the test.
1326 SwitchInt,
1327
1328 /// Test whether a `bool` is `true` or `false`.
1329 If,
1330
1331 /// Test for equality with value, possibly after an unsizing coercion to
1332 /// `ty`,
1333 Eq {
1334 value: Const<'tcx>,
1335 // Integer types are handled by `SwitchInt`, and constants with ADT
1336 // types and `&[T]` types are converted back into patterns, so this can
1337 // only be `&str`, `f32` or `f64`.
1338 ty: Ty<'tcx>,
1339 },
1340
1341 /// Test whether the value falls within an inclusive or exclusive range.
1342 Range(Arc<PatRange<'tcx>>),
1343
1344 /// Test that the length of the slice is `== len` or `>= len`.
1345 Len { len: u64, op: BinOp },
1346
1347 /// Call `Deref::deref[_mut]` on the value.
1348 Deref {
1349 /// Temporary to store the result of `deref()`/`deref_mut()`.
1350 temp: Place<'tcx>,
1351 mutability: Mutability,
1352 },
1353
1354 /// Assert unreachability of never patterns.
1355 Never,
1356}
1357
1358/// A test to perform to determine which [`Candidate`] matches a value.
1359///
1360/// [`Test`] is just the test to perform; it does not include the value
1361/// to be tested.
1362#[derive(Debug)]
1363pub(crate) struct Test<'tcx> {
1364 span: Span,
1365 kind: TestKind<'tcx>,
1366}
1367
1368/// The branch to be taken after a test.
1369#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1370enum TestBranch<'tcx> {
1371 /// Success branch, used for tests with two possible outcomes.
1372 Success,
1373 /// Branch corresponding to this constant.
1374 Constant(Const<'tcx>, u128),
1375 /// Branch corresponding to this variant.
1376 Variant(VariantIdx),
1377 /// Failure branch for tests with two possible outcomes, and "otherwise" branch for other tests.
1378 Failure,
1379}
1380
1381impl<'tcx> TestBranch<'tcx> {
1382 fn as_constant(&self) -> Option<&Const<'tcx>> {
1383 if let Self::Constant(v, _) = self { Some(v) } else { None }
1384 }
1385}
1386
1387/// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
1388/// a match arm has a guard expression attached to it.
1389#[derive(Copy, Clone, Debug)]
1390pub(crate) struct ArmHasGuard(pub(crate) bool);
1391
1392///////////////////////////////////////////////////////////////////////////
1393// Main matching algorithm
1394
1395/// A sub-branch in the output of match lowering. Match lowering has generated MIR code that will
1396/// branch to `success_block` when the matched value matches the corresponding pattern. If there is
1397/// a guard, its failure must continue to `otherwise_block`, which will resume testing patterns.
1398#[derive(Debug)]
1399struct MatchTreeSubBranch<'tcx> {
1400 span: Span,
1401 /// The block that is branched to if the corresponding subpattern matches.
1402 success_block: BasicBlock,
1403 /// The block to branch to if this arm had a guard and the guard fails.
1404 otherwise_block: BasicBlock,
1405 /// The bindings to set up in this sub-branch.
1406 bindings: Vec<Binding<'tcx>>,
1407 /// The ascriptions to set up in this sub-branch.
1408 ascriptions: Vec<Ascription<'tcx>>,
1409 /// Whether the sub-branch corresponds to a never pattern.
1410 is_never: bool,
1411}
1412
1413/// A branch in the output of match lowering.
1414#[derive(Debug)]
1415struct MatchTreeBranch<'tcx> {
1416 sub_branches: Vec<MatchTreeSubBranch<'tcx>>,
1417}
1418
1419/// The result of generating MIR for a pattern-matching expression. Each input branch/arm/pattern
1420/// gives rise to an output `MatchTreeBranch`. If one of the patterns matches, we branch to the
1421/// corresponding `success_block`. If none of the patterns matches, we branch to `otherwise_block`.
1422///
1423/// Each branch is made of one of more sub-branches, corresponding to or-patterns. E.g.
1424/// ```ignore(illustrative)
1425/// match foo {
1426/// (x, false) | (false, x) => {}
1427/// (true, true) => {}
1428/// }
1429/// ```
1430/// Here the first arm gives the first `MatchTreeBranch`, which has two sub-branches, one for each
1431/// alternative of the or-pattern. They are kept separate because each needs to bind `x` to a
1432/// different place.
1433#[derive(Debug)]
1434struct BuiltMatchTree<'tcx> {
1435 branches: Vec<MatchTreeBranch<'tcx>>,
1436 otherwise_block: BasicBlock,
1437 /// If any of the branches had a guard, we collect here the places and locals to fakely borrow
1438 /// to ensure match guards can't modify the values as we match them. For more details, see
1439 /// [`util::collect_fake_borrows`].
1440 fake_borrow_temps: Vec<(Place<'tcx>, Local, FakeBorrowKind)>,
1441}
1442
1443impl<'tcx> MatchTreeSubBranch<'tcx> {
1444 fn from_sub_candidate(
1445 candidate: Candidate<'tcx>,
1446 parent_data: &Vec<PatternExtraData<'tcx>>,
1447 ) -> Self {
1448 debug_assert!(candidate.match_pairs.is_empty());
1449 MatchTreeSubBranch {
1450 span: candidate.extra_data.span,
1451 success_block: candidate.pre_binding_block.unwrap(),
1452 otherwise_block: candidate.otherwise_block.unwrap(),
1453 bindings: parent_data
1454 .iter()
1455 .flat_map(|d| &d.bindings)
1456 .chain(&candidate.extra_data.bindings)
1457 .cloned()
1458 .collect(),
1459 ascriptions: parent_data
1460 .iter()
1461 .flat_map(|d| &d.ascriptions)
1462 .cloned()
1463 .chain(candidate.extra_data.ascriptions)
1464 .collect(),
1465 is_never: candidate.extra_data.is_never,
1466 }
1467 }
1468}
1469
1470impl<'tcx> MatchTreeBranch<'tcx> {
1471 fn from_candidate(candidate: Candidate<'tcx>) -> Self {
1472 let mut sub_branches = Vec::new();
1473 traverse_candidate(
1474 candidate,
1475 &mut Vec::new(),
1476 &mut |candidate: Candidate<'_>, parent_data: &mut Vec<PatternExtraData<'_>>| {
1477 sub_branches.push(MatchTreeSubBranch::from_sub_candidate(candidate, parent_data));
1478 },
1479 |inner_candidate, parent_data| {
1480 parent_data.push(inner_candidate.extra_data);
1481 inner_candidate.subcandidates.into_iter()
1482 },
1483 |parent_data| {
1484 parent_data.pop();
1485 },
1486 );
1487 MatchTreeBranch { sub_branches }
1488 }
1489}
1490
1491#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1492enum HasMatchGuard {
1493 Yes,
1494 No,
1495}
1496
1497impl<'a, 'tcx> Builder<'a, 'tcx> {
1498 /// The entrypoint of the matching algorithm. Create the decision tree for the match expression,
1499 /// starting from `block`.
1500 ///
1501 /// `patterns` is a list of patterns, one for each arm. The associated boolean indicates whether
1502 /// the arm has a guard.
1503 ///
1504 /// `refutable` indicates whether the candidate list is refutable (for `if let` and `let else`)
1505 /// or not (for `let` and `match`). In the refutable case we return the block to which we branch
1506 /// on failure.
1507 fn lower_match_tree(
1508 &mut self,
1509 block: BasicBlock,
1510 scrutinee_span: Span,
1511 scrutinee_place_builder: &PlaceBuilder<'tcx>,
1512 match_start_span: Span,
1513 patterns: Vec<(&Pat<'tcx>, HasMatchGuard)>,
1514 refutable: bool,
1515 ) -> BuiltMatchTree<'tcx> {
1516 // Assemble the initial list of candidates. These top-level candidates are 1:1 with the
1517 // input patterns, but other parts of match lowering also introduce subcandidates (for
1518 // sub-or-patterns). So inside the algorithm, the candidates list may not correspond to
1519 // match arms directly.
1520 let mut candidates: Vec<Candidate<'_>> = patterns
1521 .into_iter()
1522 .map(|(pat, has_guard)| {
1523 Candidate::new(scrutinee_place_builder.clone(), pat, has_guard, self)
1524 })
1525 .collect();
1526
1527 let fake_borrow_temps = util::collect_fake_borrows(
1528 self,
1529 &candidates,
1530 scrutinee_span,
1531 scrutinee_place_builder.base(),
1532 );
1533
1534 // This will generate code to test scrutinee_place and branch to the appropriate arm block.
1535 // If none of the arms match, we branch to `otherwise_block`. When lowering a `match`
1536 // expression, exhaustiveness checking ensures that this block is unreachable.
1537 let mut candidate_refs = candidates.iter_mut().collect::<Vec<_>>();
1538 let otherwise_block =
1539 self.match_candidates(match_start_span, scrutinee_span, block, &mut candidate_refs);
1540
1541 // Set up false edges so that the borrow-checker cannot make use of the specific CFG we
1542 // generated. We falsely branch from each candidate to the one below it to make it as if we
1543 // were testing match branches one by one in order. In the refutable case we also want a
1544 // false edge to the final failure block.
1545 let mut next_candidate_start_block = if refutable { Some(otherwise_block) } else { None };
1546 for candidate in candidates.iter_mut().rev() {
1547 let has_guard = candidate.has_guard;
1548 candidate.visit_leaves_rev(|leaf_candidate| {
1549 if let Some(next_candidate_start_block) = next_candidate_start_block {
1550 let source_info = self.source_info(leaf_candidate.extra_data.span);
1551 // Falsely branch to `next_candidate_start_block` before reaching pre_binding.
1552 let old_pre_binding = leaf_candidate.pre_binding_block.unwrap();
1553 let new_pre_binding = self.cfg.start_new_block();
1554 self.false_edges(
1555 old_pre_binding,
1556 new_pre_binding,
1557 next_candidate_start_block,
1558 source_info,
1559 );
1560 leaf_candidate.pre_binding_block = Some(new_pre_binding);
1561 if has_guard {
1562 // Falsely branch to `next_candidate_start_block` also if the guard fails.
1563 let new_otherwise = self.cfg.start_new_block();
1564 let old_otherwise = leaf_candidate.otherwise_block.unwrap();
1565 self.false_edges(
1566 new_otherwise,
1567 old_otherwise,
1568 next_candidate_start_block,
1569 source_info,
1570 );
1571 leaf_candidate.otherwise_block = Some(new_otherwise);
1572 }
1573 }
1574 assert!(leaf_candidate.false_edge_start_block.is_some());
1575 next_candidate_start_block = leaf_candidate.false_edge_start_block;
1576 });
1577 }
1578
1579 if !refutable {
1580 // Match checking ensures `otherwise_block` is actually unreachable in irrefutable
1581 // cases.
1582 let source_info = self.source_info(scrutinee_span);
1583
1584 // Matching on a scrutinee place of an uninhabited type doesn't generate any memory
1585 // reads by itself, and so if the place is uninitialized we wouldn't know. In order to
1586 // disallow the following:
1587 // ```rust
1588 // let x: !;
1589 // match x {}
1590 // ```
1591 // we add a dummy read on the place.
1592 //
1593 // NOTE: If we require never patterns for empty matches, those will check that the place
1594 // is initialized, and so this read would no longer be needed.
1595 let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
1596
1597 if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
1598 self.cfg.push_fake_read(
1599 otherwise_block,
1600 source_info,
1601 cause_matched_place,
1602 scrutinee_place,
1603 );
1604 }
1605
1606 self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
1607 }
1608
1609 BuiltMatchTree {
1610 branches: candidates.into_iter().map(MatchTreeBranch::from_candidate).collect(),
1611 otherwise_block,
1612 fake_borrow_temps,
1613 }
1614 }
1615
1616 /// The main match algorithm. It begins with a set of candidates `candidates` and has the job of
1617 /// generating code that branches to an appropriate block if the scrutinee matches one of these
1618 /// candidates. The
1619 /// candidates are ordered such that the first item in the list
1620 /// has the highest priority. When a candidate is found to match
1621 /// the value, we will set and generate a branch to the appropriate
1622 /// pre-binding block.
1623 ///
1624 /// If none of the candidates apply, we continue to the returned `otherwise_block`.
1625 ///
1626 /// Note that while `match` expressions in the Rust language are exhaustive,
1627 /// candidate lists passed to this method are often _non-exhaustive_.
1628 /// For example, the match lowering process will frequently divide up the
1629 /// list of candidates, and recursively call this method with a non-exhaustive
1630 /// subset of candidates.
1631 /// See [`Builder::test_candidates`] for more details on this
1632 /// "backtracking automata" approach.
1633 ///
1634 /// For an example of how we use `otherwise_block`, consider:
1635 /// ```
1636 /// # fn foo((x, y): (bool, bool)) -> u32 {
1637 /// match (x, y) {
1638 /// (true, true) => 1,
1639 /// (_, false) => 2,
1640 /// (false, true) => 3,
1641 /// }
1642 /// # }
1643 /// ```
1644 /// For this match, we generate something like:
1645 /// ```
1646 /// # fn foo((x, y): (bool, bool)) -> u32 {
1647 /// if x {
1648 /// if y {
1649 /// return 1
1650 /// } else {
1651 /// // continue
1652 /// }
1653 /// } else {
1654 /// // continue
1655 /// }
1656 /// if y {
1657 /// if x {
1658 /// // This is actually unreachable because the `(true, true)` case was handled above,
1659 /// // but we don't know that from within the lowering algorithm.
1660 /// // continue
1661 /// } else {
1662 /// return 3
1663 /// }
1664 /// } else {
1665 /// return 2
1666 /// }
1667 /// // this is the final `otherwise_block`, which is unreachable because the match was exhaustive.
1668 /// unreachable!()
1669 /// # }
1670 /// ```
1671 ///
1672 /// Every `continue` is an instance of branching to some `otherwise_block` somewhere deep within
1673 /// the algorithm. For more details on why we lower like this, see [`Builder::test_candidates`].
1674 ///
1675 /// Note how we test `x` twice. This is the tradeoff of backtracking automata: we prefer smaller
1676 /// code size so we accept non-optimal code paths.
1677 #[instrument(skip(self), level = "debug")]
1678 fn match_candidates(
1679 &mut self,
1680 span: Span,
1681 scrutinee_span: Span,
1682 start_block: BasicBlock,
1683 candidates: &mut [&mut Candidate<'tcx>],
1684 ) -> BasicBlock {
1685 ensure_sufficient_stack(|| {
1686 self.match_candidates_inner(span, scrutinee_span, start_block, candidates)
1687 })
1688 }
1689
1690 /// Construct the decision tree for `candidates`. Don't call this, call `match_candidates`
1691 /// instead to reserve sufficient stack space.
1692 fn match_candidates_inner(
1693 &mut self,
1694 span: Span,
1695 scrutinee_span: Span,
1696 mut start_block: BasicBlock,
1697 candidates: &mut [&mut Candidate<'tcx>],
1698 ) -> BasicBlock {
1699 if let [first, ..] = candidates {
1700 if first.false_edge_start_block.is_none() {
1701 first.false_edge_start_block = Some(start_block);
1702 }
1703 }
1704
1705 // Process a prefix of the candidates.
1706 let rest = match candidates {
1707 [] => {
1708 // If there are no candidates that still need testing, we're done.
1709 return start_block;
1710 }
1711 [first, remaining @ ..] if first.match_pairs.is_empty() => {
1712 // The first candidate has satisfied all its match pairs.
1713 // We record the blocks that will be needed by match arm lowering,
1714 // and then continue with the remaining candidates.
1715 let remainder_start = self.select_matched_candidate(first, start_block);
1716 remainder_start.and(remaining)
1717 }
1718 candidates if candidates.iter().any(|candidate| candidate.starts_with_or_pattern()) => {
1719 // If any candidate starts with an or-pattern, we want to expand or-patterns
1720 // before we do any more tests.
1721 //
1722 // The only candidate we strictly _need_ to expand here is the first one.
1723 // But by expanding other candidates as early as possible, we unlock more
1724 // opportunities to include them in test outcomes, making the match tree
1725 // smaller and simpler.
1726 self.expand_and_match_or_candidates(span, scrutinee_span, start_block, candidates)
1727 }
1728 candidates => {
1729 // The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1730 self.test_candidates(span, scrutinee_span, candidates, start_block)
1731 }
1732 };
1733
1734 // Process any candidates that remain.
1735 let remaining_candidates = unpack!(start_block = rest);
1736 self.match_candidates(span, scrutinee_span, start_block, remaining_candidates)
1737 }
1738
1739 /// Link up matched candidates.
1740 ///
1741 /// For example, if we have something like this:
1742 ///
1743 /// ```ignore (illustrative)
1744 /// ...
1745 /// Some(x) if cond1 => ...
1746 /// Some(x) => ...
1747 /// Some(x) if cond2 => ...
1748 /// ...
1749 /// ```
1750 ///
1751 /// We generate real edges from:
1752 ///
1753 /// * `start_block` to the [pre-binding block] of the first pattern,
1754 /// * the [otherwise block] of the first pattern to the second pattern,
1755 /// * the [otherwise block] of the third pattern to a block with an
1756 /// [`Unreachable` terminator](TerminatorKind::Unreachable).
1757 ///
1758 /// In addition, we later add fake edges from the otherwise blocks to the
1759 /// pre-binding block of the next candidate in the original set of
1760 /// candidates.
1761 ///
1762 /// [pre-binding block]: Candidate::pre_binding_block
1763 /// [otherwise block]: Candidate::otherwise_block
1764 fn select_matched_candidate(
1765 &mut self,
1766 candidate: &mut Candidate<'tcx>,
1767 start_block: BasicBlock,
1768 ) -> BasicBlock {
1769 assert!(candidate.otherwise_block.is_none());
1770 assert!(candidate.pre_binding_block.is_none());
1771 assert!(candidate.subcandidates.is_empty());
1772
1773 candidate.pre_binding_block = Some(start_block);
1774 let otherwise_block = self.cfg.start_new_block();
1775 // Create the otherwise block for this candidate, which is the
1776 // pre-binding block for the next candidate.
1777 candidate.otherwise_block = Some(otherwise_block);
1778 otherwise_block
1779 }
1780
1781 /// Takes a list of candidates such that some of the candidates' first match pairs are
1782 /// or-patterns. This expands as many or-patterns as possible and processes the resulting
1783 /// candidates. Returns the unprocessed candidates if any.
1784 fn expand_and_match_or_candidates<'b, 'c>(
1785 &mut self,
1786 span: Span,
1787 scrutinee_span: Span,
1788 start_block: BasicBlock,
1789 candidates: &'b mut [&'c mut Candidate<'tcx>],
1790 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
1791 // We can't expand or-patterns freely. The rule is:
1792 // - If a candidate doesn't start with an or-pattern, we include it in
1793 // the expansion list as-is (i.e. it "expands" to itself).
1794 // - If a candidate has an or-pattern as its only remaining match pair,
1795 // we can expand it.
1796 // - If it starts with an or-pattern but also has other match pairs,
1797 // we can expand it, but we can't process more candidates after it.
1798 //
1799 // If we didn't stop, the `otherwise` cases could get mixed up. E.g. in the
1800 // following, or-pattern simplification (in `merge_trivial_subcandidates`) makes it
1801 // so the `1` and `2` cases branch to a same block (which then tests `false`). If we
1802 // took `(2, _)` in the same set of candidates, when we reach the block that tests
1803 // `false` we don't know whether we came from `1` or `2`, hence we can't know where
1804 // to branch on failure.
1805 //
1806 // ```ignore(illustrative)
1807 // match (1, true) {
1808 // (1 | 2, false) => {},
1809 // (2, _) => {},
1810 // _ => {}
1811 // }
1812 // ```
1813 //
1814 // We therefore split the `candidates` slice in two, expand or-patterns in the first part,
1815 // and process the rest separately.
1816 let expand_until = candidates
1817 .iter()
1818 .position(|candidate| {
1819 // If a candidate starts with an or-pattern and has more match pairs,
1820 // we can expand it, but we must stop expanding _after_ it.
1821 candidate.match_pairs.len() > 1 && candidate.starts_with_or_pattern()
1822 })
1823 .map(|pos| pos + 1) // Stop _after_ the found candidate
1824 .unwrap_or(candidates.len()); // Otherwise, include all candidates
1825 let (candidates_to_expand, remaining_candidates) = candidates.split_at_mut(expand_until);
1826
1827 // Expand one level of or-patterns for each candidate in `candidates_to_expand`.
1828 // We take care to preserve the relative ordering of candidates, so that
1829 // or-patterns are expanded in their parent's relative position.
1830 let mut expanded_candidates = Vec::new();
1831 for candidate in candidates_to_expand.iter_mut() {
1832 if candidate.starts_with_or_pattern() {
1833 let or_match_pair = candidate.match_pairs.remove(0);
1834 // Expand the or-pattern into subcandidates.
1835 self.create_or_subcandidates(candidate, or_match_pair);
1836 // Collect the newly created subcandidates.
1837 for subcandidate in candidate.subcandidates.iter_mut() {
1838 expanded_candidates.push(subcandidate);
1839 }
1840 // Note that the subcandidates have been added to `expanded_candidates`,
1841 // but `candidate` itself has not. If the last candidate has more match pairs,
1842 // they are handled separately by `test_remaining_match_pairs_after_or`.
1843 } else {
1844 // A candidate that doesn't start with an or-pattern has nothing to
1845 // expand, so it is included in the post-expansion list as-is.
1846 expanded_candidates.push(candidate);
1847 }
1848 }
1849
1850 // Recursively lower the part of the match tree represented by the
1851 // expanded candidates. This is where subcandidates actually get lowered!
1852 let remainder_start = self.match_candidates(
1853 span,
1854 scrutinee_span,
1855 start_block,
1856 expanded_candidates.as_mut_slice(),
1857 );
1858
1859 // Postprocess subcandidates, and process any leftover match pairs.
1860 // (Only the last candidate can possibly have more match pairs.)
1861 debug_assert!({
1862 let mut all_except_last = candidates_to_expand.iter().rev().skip(1);
1863 all_except_last.all(|candidate| candidate.match_pairs.is_empty())
1864 });
1865 for candidate in candidates_to_expand.iter_mut() {
1866 if !candidate.subcandidates.is_empty() {
1867 self.merge_trivial_subcandidates(candidate);
1868 self.remove_never_subcandidates(candidate);
1869 }
1870 }
1871 // It's important to perform the above simplifications _before_ dealing
1872 // with remaining match pairs, to avoid exponential blowup if possible
1873 // (for trivial or-patterns), and avoid useless work (for never patterns).
1874 if let Some(last_candidate) = candidates_to_expand.last_mut() {
1875 self.test_remaining_match_pairs_after_or(span, scrutinee_span, last_candidate);
1876 }
1877
1878 remainder_start.and(remaining_candidates)
1879 }
1880
1881 /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1882 /// subcandidate. Any candidate that has been expanded this way should also be postprocessed
1883 /// at the end of [`Self::expand_and_match_or_candidates`].
1884 fn create_or_subcandidates(
1885 &mut self,
1886 candidate: &mut Candidate<'tcx>,
1887 match_pair: MatchPairTree<'tcx>,
1888 ) {
1889 let TestCase::Or { pats } = match_pair.test_case else { bug!() };
1890 debug!("expanding or-pattern: candidate={:#?}\npats={:#?}", candidate, pats);
1891 candidate.or_span = Some(match_pair.pattern_span);
1892 candidate.subcandidates = pats
1893 .into_vec()
1894 .into_iter()
1895 .map(|flat_pat| Candidate::from_flat_pat(flat_pat, candidate.has_guard))
1896 .collect();
1897 candidate.subcandidates[0].false_edge_start_block = candidate.false_edge_start_block;
1898 }
1899
1900 /// Try to merge all of the subcandidates of the given candidate into one. This avoids
1901 /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The candidate should have been
1902 /// expanded with `create_or_subcandidates`.
1903 ///
1904 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1905 /// so:
1906 ///
1907 /// ```text
1908 /// [ start ]
1909 /// |
1910 /// [ match P, Q ]
1911 /// |
1912 /// +----------------------------------------+------------------------------------+
1913 /// | | |
1914 /// V V V
1915 /// [ P matches ] [ Q matches ] [ otherwise ]
1916 /// | | |
1917 /// V V |
1918 /// [ match R, S ] [ match R, S ] |
1919 /// | | |
1920 /// +--------------+------------+ +--------------+------------+ |
1921 /// | | | | | | |
1922 /// V V V V V V |
1923 /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] |
1924 /// | | | | | | |
1925 /// +--------------+------------|------------+--------------+ | |
1926 /// | | | |
1927 /// | +----------------------------------------+--------+
1928 /// | |
1929 /// V V
1930 /// [ Success ] [ Failure ]
1931 /// ```
1932 ///
1933 /// In practice there are some complications:
1934 ///
1935 /// * If there's a guard, then the otherwise branch of the first match on
1936 /// `R | S` goes to a test for whether `Q` matches, and the control flow
1937 /// doesn't merge into a single success block until after the guard is
1938 /// tested.
1939 /// * If neither `P` or `Q` has any bindings or type ascriptions and there
1940 /// isn't a match guard, then we create a smaller CFG like:
1941 ///
1942 /// ```text
1943 /// ...
1944 /// +---------------+------------+
1945 /// | | |
1946 /// [ P matches ] [ Q matches ] [ otherwise ]
1947 /// | | |
1948 /// +---------------+ |
1949 /// | ...
1950 /// [ match R, S ]
1951 /// |
1952 /// ...
1953 /// ```
1954 ///
1955 /// Note that this takes place _after_ the subcandidates have participated
1956 /// in match tree lowering.
1957 fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
1958 assert!(!candidate.subcandidates.is_empty());
1959 if candidate.has_guard {
1960 // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
1961 return;
1962 }
1963
1964 // FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
1965 let can_merge = candidate.subcandidates.iter().all(|subcandidate| {
1966 subcandidate.subcandidates.is_empty() && subcandidate.extra_data.is_empty()
1967 });
1968 if !can_merge {
1969 return;
1970 }
1971
1972 let mut last_otherwise = None;
1973 let shared_pre_binding_block = self.cfg.start_new_block();
1974 // This candidate is about to become a leaf, so unset `or_span`.
1975 let or_span = candidate.or_span.take().unwrap();
1976 let source_info = self.source_info(or_span);
1977
1978 if candidate.false_edge_start_block.is_none() {
1979 candidate.false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
1980 }
1981
1982 // Remove the (known-trivial) subcandidates from the candidate tree,
1983 // so that they aren't visible after match tree lowering, and wire them
1984 // all to join up at a single shared pre-binding block.
1985 // (Note that the subcandidates have already had their part of the match
1986 // tree lowered by this point, which is why we can add a goto to them.)
1987 for subcandidate in mem::take(&mut candidate.subcandidates) {
1988 let subcandidate_block = subcandidate.pre_binding_block.unwrap();
1989 self.cfg.goto(subcandidate_block, source_info, shared_pre_binding_block);
1990 last_otherwise = subcandidate.otherwise_block;
1991 }
1992 candidate.pre_binding_block = Some(shared_pre_binding_block);
1993 assert!(last_otherwise.is_some());
1994 candidate.otherwise_block = last_otherwise;
1995 }
1996
1997 /// Never subcandidates may have a set of bindings inconsistent with their siblings,
1998 /// which would break later code. So we filter them out. Note that we can't filter out
1999 /// top-level candidates this way.
2000 fn remove_never_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
2001 if candidate.subcandidates.is_empty() {
2002 return;
2003 }
2004
2005 let false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
2006 candidate.subcandidates.retain_mut(|candidate| {
2007 if candidate.extra_data.is_never {
2008 candidate.visit_leaves(|subcandidate| {
2009 let block = subcandidate.pre_binding_block.unwrap();
2010 // That block is already unreachable but needs a terminator to make the MIR well-formed.
2011 let source_info = self.source_info(subcandidate.extra_data.span);
2012 self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2013 });
2014 false
2015 } else {
2016 true
2017 }
2018 });
2019 if candidate.subcandidates.is_empty() {
2020 // If `candidate` has become a leaf candidate, ensure it has a `pre_binding_block` and `otherwise_block`.
2021 let next_block = self.cfg.start_new_block();
2022 candidate.pre_binding_block = Some(next_block);
2023 candidate.otherwise_block = Some(next_block);
2024 // In addition, if `candidate` doesn't have `false_edge_start_block`, it should be assigned here.
2025 if candidate.false_edge_start_block.is_none() {
2026 candidate.false_edge_start_block = false_edge_start_block;
2027 }
2028 }
2029 }
2030
2031 /// If more match pairs remain, test them after each subcandidate.
2032 /// We could have added them to the or-candidates during or-pattern expansion, but that
2033 /// would make it impossible to detect simplifiable or-patterns. That would guarantee
2034 /// exponentially large CFGs for cases like `(1 | 2, 3 | 4, ...)`.
2035 fn test_remaining_match_pairs_after_or(
2036 &mut self,
2037 span: Span,
2038 scrutinee_span: Span,
2039 candidate: &mut Candidate<'tcx>,
2040 ) {
2041 if candidate.match_pairs.is_empty() {
2042 return;
2043 }
2044
2045 let or_span = candidate.or_span.unwrap_or(candidate.extra_data.span);
2046 let source_info = self.source_info(or_span);
2047 let mut last_otherwise = None;
2048 candidate.visit_leaves(|leaf_candidate| {
2049 last_otherwise = leaf_candidate.otherwise_block;
2050 });
2051
2052 let remaining_match_pairs = mem::take(&mut candidate.match_pairs);
2053 // We're testing match pairs that remained after an `Or`, so the remaining
2054 // pairs should all be `Or` too, due to the sorting invariant.
2055 debug_assert!(
2056 remaining_match_pairs
2057 .iter()
2058 .all(|match_pair| matches!(match_pair.test_case, TestCase::Or { .. }))
2059 );
2060
2061 // Visit each leaf candidate within this subtree, add a copy of the remaining
2062 // match pairs to it, and then recursively lower the rest of the match tree
2063 // from that point.
2064 candidate.visit_leaves(|leaf_candidate| {
2065 // At this point the leaf's own match pairs have all been lowered
2066 // and removed, so `extend` and assignment are equivalent,
2067 // but extending can also recycle any existing vector capacity.
2068 assert!(leaf_candidate.match_pairs.is_empty());
2069 leaf_candidate.match_pairs.extend(remaining_match_pairs.iter().cloned());
2070
2071 let or_start = leaf_candidate.pre_binding_block.unwrap();
2072 let otherwise =
2073 self.match_candidates(span, scrutinee_span, or_start, &mut [leaf_candidate]);
2074 // In a case like `(P | Q, R | S)`, if `P` succeeds and `R | S` fails, we know `(Q,
2075 // R | S)` will fail too. If there is no guard, we skip testing of `Q` by branching
2076 // directly to `last_otherwise`. If there is a guard,
2077 // `leaf_candidate.otherwise_block` can be reached by guard failure as well, so we
2078 // can't skip `Q`.
2079 let or_otherwise = if leaf_candidate.has_guard {
2080 leaf_candidate.otherwise_block.unwrap()
2081 } else {
2082 last_otherwise.unwrap()
2083 };
2084 self.cfg.goto(otherwise, source_info, or_otherwise);
2085 });
2086 }
2087
2088 /// Pick a test to run. Which test doesn't matter as long as it is guaranteed to fully match at
2089 /// least one match pair. We currently simply pick the test corresponding to the first match
2090 /// pair of the first candidate in the list.
2091 ///
2092 /// *Note:* taking the first match pair is somewhat arbitrary, and we might do better here by
2093 /// choosing more carefully what to test.
2094 ///
2095 /// For example, consider the following possible match-pairs:
2096 ///
2097 /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
2098 /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
2099 /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
2100 /// 4. etc.
2101 ///
2102 /// [`Switch`]: TestKind::Switch
2103 /// [`SwitchInt`]: TestKind::SwitchInt
2104 /// [`Range`]: TestKind::Range
2105 fn pick_test(&mut self, candidates: &[&mut Candidate<'tcx>]) -> (Place<'tcx>, Test<'tcx>) {
2106 // Extract the match-pair from the highest priority candidate
2107 let match_pair = &candidates[0].match_pairs[0];
2108 let test = self.pick_test_for_match_pair(match_pair);
2109 // Unwrap is ok after simplification.
2110 let match_place = match_pair.place.unwrap();
2111 debug!(?test, ?match_pair);
2112
2113 (match_place, test)
2114 }
2115
2116 /// Given a test, we partition the input candidates into several buckets.
2117 /// If a candidate matches in exactly one of the branches of `test`
2118 /// (and no other branches), we put it into the corresponding bucket.
2119 /// If it could match in more than one of the branches of `test`, the test
2120 /// doesn't usefully apply to it, and we stop partitioning candidates.
2121 ///
2122 /// Importantly, we also **mutate** the branched candidates to remove match pairs
2123 /// that are entailed by the outcome of the test, and add any sub-pairs of the
2124 /// removed pairs.
2125 ///
2126 /// This returns a pair of
2127 /// - the candidates that weren't sorted;
2128 /// - for each possible outcome of the test, the candidates that match in that outcome.
2129 ///
2130 /// For example:
2131 /// ```
2132 /// # let (x, y, z) = (true, true, true);
2133 /// match (x, y, z) {
2134 /// (true , _ , true ) => true, // (0)
2135 /// (false, false, _ ) => false, // (1)
2136 /// (_ , true , _ ) => true, // (2)
2137 /// (true , _ , false) => false, // (3)
2138 /// }
2139 /// # ;
2140 /// ```
2141 ///
2142 /// Assume we are testing on `x`. Conceptually, there are 2 overlapping candidate sets:
2143 /// - If the outcome is that `x` is true, candidates {0, 2, 3} are possible
2144 /// - If the outcome is that `x` is false, candidates {1, 2} are possible
2145 ///
2146 /// Following our algorithm:
2147 /// - Candidate 0 is sorted into outcome `x == true`
2148 /// - Candidate 1 is sorted into outcome `x == false`
2149 /// - Candidate 2 remains unsorted, because testing `x` has no effect on it
2150 /// - Candidate 3 remains unsorted, because a previous candidate (2) was unsorted
2151 /// - This helps preserve the illusion that candidates are tested "in order"
2152 ///
2153 /// The sorted candidates are mutated to remove entailed match pairs:
2154 /// - candidate 0 becomes `[z @ true]` since we know that `x` was `true`;
2155 /// - candidate 1 becomes `[y @ false]` since we know that `x` was `false`.
2156 fn sort_candidates<'b, 'c>(
2157 &mut self,
2158 match_place: Place<'tcx>,
2159 test: &Test<'tcx>,
2160 mut candidates: &'b mut [&'c mut Candidate<'tcx>],
2161 ) -> (
2162 &'b mut [&'c mut Candidate<'tcx>],
2163 FxIndexMap<TestBranch<'tcx>, Vec<&'b mut Candidate<'tcx>>>,
2164 ) {
2165 // For each of the possible outcomes, collect vector of candidates that apply if the test
2166 // has that particular outcome.
2167 let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'_>>> = Default::default();
2168
2169 let total_candidate_count = candidates.len();
2170
2171 // Sort the candidates into the appropriate vector in `target_candidates`. Note that at some
2172 // point we may encounter a candidate where the test is not relevant; at that point, we stop
2173 // sorting.
2174 while let Some(candidate) = candidates.first_mut() {
2175 let Some(branch) =
2176 self.sort_candidate(match_place, test, candidate, &target_candidates)
2177 else {
2178 break;
2179 };
2180 let (candidate, rest) = candidates.split_first_mut().unwrap();
2181 target_candidates.entry(branch).or_insert_with(Vec::new).push(candidate);
2182 candidates = rest;
2183 }
2184
2185 // At least the first candidate ought to be tested
2186 assert!(
2187 total_candidate_count > candidates.len(),
2188 "{total_candidate_count}, {candidates:#?}"
2189 );
2190 debug!("tested_candidates: {}", total_candidate_count - candidates.len());
2191 debug!("untested_candidates: {}", candidates.len());
2192
2193 (candidates, target_candidates)
2194 }
2195
2196 /// This is the most subtle part of the match lowering algorithm. At this point, there are
2197 /// no fully-satisfied candidates, and no or-patterns to expand, so we actually need to
2198 /// perform some sort of test to make progress.
2199 ///
2200 /// Once we pick what sort of test we are going to perform, this test will help us winnow down
2201 /// our candidates. So we walk over the candidates (from high to low priority) and check. We
2202 /// compute, for each outcome of the test, a list of (modified) candidates. If a candidate
2203 /// matches in exactly one branch of our test, we add it to the corresponding outcome. We also
2204 /// **mutate its list of match pairs** if appropriate, to reflect the fact that we know which
2205 /// outcome occurred.
2206 ///
2207 /// For example, if we are testing `x.0`'s variant, and we have a candidate `(x.0 @ Some(v), x.1
2208 /// @ 22)`, then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)` in the
2209 /// branch corresponding to `Some`. To ensure we make progress, we always pick a test that
2210 /// results in simplifying the first candidate.
2211 ///
2212 /// But there may also be candidates that the test doesn't
2213 /// apply to. The classical example is wildcards:
2214 ///
2215 /// ```
2216 /// # let (x, y, z) = (true, true, true);
2217 /// match (x, y, z) {
2218 /// (true , _ , true ) => true, // (0)
2219 /// (false, false, _ ) => false, // (1)
2220 /// (_ , true , _ ) => true, // (2)
2221 /// (true , _ , false) => false, // (3)
2222 /// }
2223 /// # ;
2224 /// ```
2225 ///
2226 /// Here, the traditional "decision tree" method would generate 2 separate code-paths for the 2
2227 /// possible values of `x`. This would however duplicate some candidates, which would need to be
2228 /// lowered several times.
2229 ///
2230 /// In some cases, this duplication can create an exponential amount of
2231 /// code. This is most easily seen by noticing that this method terminates
2232 /// with precisely the reachable arms being reachable - but that problem
2233 /// is trivially NP-complete:
2234 ///
2235 /// ```ignore (illustrative)
2236 /// match (var0, var1, var2, var3, ...) {
2237 /// (true , _ , _ , false, true, ...) => false,
2238 /// (_ , true, true , false, _ , ...) => false,
2239 /// (false, _ , false, false, _ , ...) => false,
2240 /// ...
2241 /// _ => true
2242 /// }
2243 /// ```
2244 ///
2245 /// Here the last arm is reachable only if there is an assignment to
2246 /// the variables that does not match any of the literals. Therefore,
2247 /// compilation would take an exponential amount of time in some cases.
2248 ///
2249 /// In rustc, we opt instead for the "backtracking automaton" approach. This guarantees we never
2250 /// duplicate a candidate (except in the presence of or-patterns). In fact this guarantee is
2251 /// ensured by the fact that we carry around `&mut Candidate`s which can't be duplicated.
2252 ///
2253 /// To make this work, whenever we decide to perform a test, if we encounter a candidate that
2254 /// could match in more than one branch of the test, we stop. We generate code for the test and
2255 /// for the candidates in its branches; the remaining candidates will be tested if the
2256 /// candidates in the branches fail to match.
2257 ///
2258 /// For example, if we test on `x` in the following:
2259 /// ```
2260 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2261 /// match (x, y, z) {
2262 /// (true , _ , true ) => 0,
2263 /// (false, false, _ ) => 1,
2264 /// (_ , true , _ ) => 2,
2265 /// (true , _ , false) => 3,
2266 /// }
2267 /// # }
2268 /// ```
2269 /// this function generates code that looks more of less like:
2270 /// ```
2271 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2272 /// if x {
2273 /// match (y, z) {
2274 /// (_, true) => return 0,
2275 /// _ => {} // continue matching
2276 /// }
2277 /// } else {
2278 /// match (y, z) {
2279 /// (false, _) => return 1,
2280 /// _ => {} // continue matching
2281 /// }
2282 /// }
2283 /// // the block here is `remainder_start`
2284 /// match (x, y, z) {
2285 /// (_ , true , _ ) => 2,
2286 /// (true , _ , false) => 3,
2287 /// _ => unreachable!(),
2288 /// }
2289 /// # }
2290 /// ```
2291 ///
2292 /// We return the unprocessed candidates.
2293 fn test_candidates<'b, 'c>(
2294 &mut self,
2295 span: Span,
2296 scrutinee_span: Span,
2297 candidates: &'b mut [&'c mut Candidate<'tcx>],
2298 start_block: BasicBlock,
2299 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
2300 // Choose a match pair from the first candidate, and use it to determine a
2301 // test to perform that will confirm or refute that match pair.
2302 let (match_place, test) = self.pick_test(candidates);
2303
2304 // For each of the N possible test outcomes, build the vector of candidates that applies if
2305 // the test has that particular outcome. This also mutates the candidates to remove match
2306 // pairs that are fully satisfied by the relevant outcome.
2307 let (remaining_candidates, target_candidates) =
2308 self.sort_candidates(match_place, &test, candidates);
2309
2310 // The block that we should branch to if none of the `target_candidates` match.
2311 let remainder_start = self.cfg.start_new_block();
2312
2313 // For each outcome of the test, recursively lower the rest of the match tree
2314 // from that point. (Note that we haven't lowered the actual test yet!)
2315 let target_blocks: FxIndexMap<_, _> = target_candidates
2316 .into_iter()
2317 .map(|(branch, mut candidates)| {
2318 let branch_start = self.cfg.start_new_block();
2319 // Recursively lower the rest of the match tree after the relevant outcome.
2320 let branch_otherwise =
2321 self.match_candidates(span, scrutinee_span, branch_start, &mut *candidates);
2322
2323 // Link up the `otherwise` block of the subtree to `remainder_start`.
2324 let source_info = self.source_info(span);
2325 self.cfg.goto(branch_otherwise, source_info, remainder_start);
2326 (branch, branch_start)
2327 })
2328 .collect();
2329
2330 // Perform the chosen test, branching to one of the N subtrees prepared above
2331 // (or to `remainder_start` if no outcome was satisfied).
2332 self.perform_test(
2333 span,
2334 scrutinee_span,
2335 start_block,
2336 remainder_start,
2337 match_place,
2338 &test,
2339 target_blocks,
2340 );
2341
2342 remainder_start.and(remaining_candidates)
2343 }
2344}
2345
2346///////////////////////////////////////////////////////////////////////////
2347// Pat binding - used for `let` and function parameters as well.
2348
2349impl<'a, 'tcx> Builder<'a, 'tcx> {
2350 /// Lowers a `let` expression that appears in a suitable context
2351 /// (e.g. an `if` condition or match guard).
2352 ///
2353 /// Also used for lowering let-else statements, since they have similar
2354 /// needs despite not actually using `let` expressions.
2355 ///
2356 /// Use [`DeclareLetBindings`] to control whether the `let` bindings are
2357 /// declared or not.
2358 pub(crate) fn lower_let_expr(
2359 &mut self,
2360 mut block: BasicBlock,
2361 expr_id: ExprId,
2362 pat: &Pat<'tcx>,
2363 source_scope: Option<SourceScope>,
2364 scope_span: Span,
2365 declare_let_bindings: DeclareLetBindings,
2366 emit_storage_live: EmitStorageLive,
2367 ) -> BlockAnd<()> {
2368 let expr_span = self.thir[expr_id].span;
2369 let scrutinee = unpack!(block = self.lower_scrutinee(block, expr_id, expr_span));
2370 let built_tree = self.lower_match_tree(
2371 block,
2372 expr_span,
2373 &scrutinee,
2374 pat.span,
2375 vec![(pat, HasMatchGuard::No)],
2376 true,
2377 );
2378 let [branch] = built_tree.branches.try_into().unwrap();
2379
2380 self.break_for_else(built_tree.otherwise_block, self.source_info(expr_span));
2381
2382 match declare_let_bindings {
2383 DeclareLetBindings::Yes => {
2384 let expr_place = scrutinee.try_to_place(self);
2385 let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
2386 self.declare_bindings(
2387 source_scope,
2388 pat.span.to(scope_span),
2389 pat,
2390 None,
2391 opt_expr_place,
2392 );
2393 }
2394 DeclareLetBindings::No => {} // Caller is responsible for bindings.
2395 DeclareLetBindings::LetNotPermitted => {
2396 self.tcx.dcx().span_bug(expr_span, "let expression not expected in this context")
2397 }
2398 }
2399
2400 let success = self.bind_pattern(
2401 self.source_info(pat.span),
2402 branch,
2403 &[],
2404 expr_span,
2405 None,
2406 emit_storage_live,
2407 );
2408
2409 // If branch coverage is enabled, record this branch.
2410 self.visit_coverage_conditional_let(pat, success, built_tree.otherwise_block);
2411
2412 success.unit()
2413 }
2414
2415 /// Initializes each of the bindings from the candidate by
2416 /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
2417 /// any, and then branches to the arm. Returns the block for the case where
2418 /// the guard succeeds.
2419 ///
2420 /// Note: we do not check earlier that if there is a guard,
2421 /// there cannot be move bindings. We avoid a use-after-move by only
2422 /// moving the binding once the guard has evaluated to true (see below).
2423 fn bind_and_guard_matched_candidate(
2424 &mut self,
2425 sub_branch: MatchTreeSubBranch<'tcx>,
2426 fake_borrows: &[(Place<'tcx>, Local, FakeBorrowKind)],
2427 scrutinee_span: Span,
2428 arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
2429 schedule_drops: ScheduleDrops,
2430 emit_storage_live: EmitStorageLive,
2431 ) -> BasicBlock {
2432 debug!("bind_and_guard_matched_candidate(subbranch={:?})", sub_branch);
2433
2434 let block = sub_branch.success_block;
2435
2436 if sub_branch.is_never {
2437 // This arm has a dummy body, we don't need to generate code for it. `block` is already
2438 // unreachable (except via false edge).
2439 let source_info = self.source_info(sub_branch.span);
2440 self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2441 return self.cfg.start_new_block();
2442 }
2443
2444 self.ascribe_types(block, sub_branch.ascriptions);
2445
2446 // Lower an instance of the arm guard (if present) for this candidate,
2447 // and then perform bindings for the arm body.
2448 if let Some((arm, match_scope)) = arm_match_scope
2449 && let Some(guard) = arm.guard
2450 {
2451 let tcx = self.tcx;
2452
2453 // Bindings for guards require some extra handling to automatically
2454 // insert implicit references/dereferences.
2455 self.bind_matched_candidate_for_guard(
2456 block,
2457 schedule_drops,
2458 sub_branch.bindings.iter(),
2459 );
2460 let guard_frame = GuardFrame {
2461 locals: sub_branch
2462 .bindings
2463 .iter()
2464 .map(|b| GuardFrameLocal::new(b.var_id))
2465 .collect(),
2466 };
2467 debug!("entering guard building context: {:?}", guard_frame);
2468 self.guard_context.push(guard_frame);
2469
2470 let re_erased = tcx.lifetimes.re_erased;
2471 let scrutinee_source_info = self.source_info(scrutinee_span);
2472 for &(place, temp, kind) in fake_borrows {
2473 let borrow = Rvalue::Ref(re_erased, BorrowKind::Fake(kind), place);
2474 self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
2475 }
2476
2477 let mut guard_span = rustc_span::DUMMY_SP;
2478
2479 let (post_guard_block, otherwise_post_guard_block) =
2480 self.in_if_then_scope(match_scope, guard_span, |this| {
2481 guard_span = this.thir[guard].span;
2482 this.then_else_break(
2483 block,
2484 guard,
2485 None, // Use `self.local_scope()` as the temp scope
2486 this.source_info(arm.span),
2487 DeclareLetBindings::No, // For guards, `let` bindings are declared separately
2488 )
2489 });
2490
2491 let source_info = self.source_info(guard_span);
2492 let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
2493 let guard_frame = self.guard_context.pop().unwrap();
2494 debug!("Exiting guard building context with locals: {:?}", guard_frame);
2495
2496 for &(_, temp, _) in fake_borrows {
2497 let cause = FakeReadCause::ForMatchGuard;
2498 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
2499 }
2500
2501 self.cfg.goto(otherwise_post_guard_block, source_info, sub_branch.otherwise_block);
2502
2503 // We want to ensure that the matched candidates are bound
2504 // after we have confirmed this candidate *and* any
2505 // associated guard; Binding them on `block` is too soon,
2506 // because that would be before we've checked the result
2507 // from the guard.
2508 //
2509 // But binding them on the arm is *too late*, because
2510 // then all of the candidates for a single arm would be
2511 // bound in the same place, that would cause a case like:
2512 //
2513 // ```rust
2514 // match (30, 2) {
2515 // (mut x, 1) | (2, mut x) if { true } => { ... }
2516 // ... // ^^^^^^^ (this is `arm_block`)
2517 // }
2518 // ```
2519 //
2520 // would yield an `arm_block` something like:
2521 //
2522 // ```
2523 // StorageLive(_4); // _4 is `x`
2524 // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case
2525 // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case
2526 // ```
2527 //
2528 // and that is clearly not correct.
2529 let by_value_bindings = sub_branch
2530 .bindings
2531 .iter()
2532 .filter(|binding| matches!(binding.binding_mode.0, ByRef::No));
2533 // Read all of the by reference bindings to ensure that the
2534 // place they refer to can't be modified by the guard.
2535 for binding in by_value_bindings.clone() {
2536 let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
2537 let cause = FakeReadCause::ForGuardBinding;
2538 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
2539 }
2540 assert_matches!(
2541 schedule_drops,
2542 ScheduleDrops::Yes,
2543 "patterns with guards must schedule drops"
2544 );
2545 self.bind_matched_candidate_for_arm_body(
2546 post_guard_block,
2547 ScheduleDrops::Yes,
2548 by_value_bindings,
2549 emit_storage_live,
2550 );
2551
2552 post_guard_block
2553 } else {
2554 // (Here, it is not too early to bind the matched
2555 // candidate on `block`, because there is no guard result
2556 // that we have to inspect before we bind them.)
2557 self.bind_matched_candidate_for_arm_body(
2558 block,
2559 schedule_drops,
2560 sub_branch.bindings.iter(),
2561 emit_storage_live,
2562 );
2563 block
2564 }
2565 }
2566
2567 /// Append `AscribeUserType` statements onto the end of `block`
2568 /// for each ascription
2569 fn ascribe_types(
2570 &mut self,
2571 block: BasicBlock,
2572 ascriptions: impl IntoIterator<Item = Ascription<'tcx>>,
2573 ) {
2574 for ascription in ascriptions {
2575 let source_info = self.source_info(ascription.annotation.span);
2576
2577 let base = self.canonical_user_type_annotations.push(ascription.annotation);
2578 self.cfg.push(
2579 block,
2580 Statement {
2581 source_info,
2582 kind: StatementKind::AscribeUserType(
2583 Box::new((
2584 ascription.source,
2585 UserTypeProjection { base, projs: Vec::new() },
2586 )),
2587 ascription.variance,
2588 ),
2589 },
2590 );
2591 }
2592 }
2593
2594 /// Binding for guards is a bit different from binding for the arm body,
2595 /// because an extra layer of implicit reference/dereference is added.
2596 ///
2597 /// The idea is that any pattern bindings of type T will map to a `&T` within
2598 /// the context of the guard expression, but will continue to map to a `T`
2599 /// in the context of the arm body. To avoid surfacing this distinction in
2600 /// the user source code (which would be a severe change to the language and
2601 /// require far more revision to the compiler), any occurrence of the
2602 /// identifier in the guard expression will automatically get a deref op
2603 /// applied to it. (See the caller of [`Self::is_bound_var_in_guard`].)
2604 ///
2605 /// So an input like:
2606 ///
2607 /// ```ignore (illustrative)
2608 /// let place = Foo::new();
2609 /// match place { foo if inspect(foo)
2610 /// => feed(foo), ... }
2611 /// ```
2612 ///
2613 /// will be treated as if it were really something like:
2614 ///
2615 /// ```ignore (illustrative)
2616 /// let place = Foo::new();
2617 /// match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
2618 /// => { let tmp2 = place; feed(tmp2) }, ... }
2619 /// ```
2620 ///
2621 /// And an input like:
2622 ///
2623 /// ```ignore (illustrative)
2624 /// let place = Foo::new();
2625 /// match place { ref mut foo if inspect(foo)
2626 /// => feed(foo), ... }
2627 /// ```
2628 ///
2629 /// will be treated as if it were really something like:
2630 ///
2631 /// ```ignore (illustrative)
2632 /// let place = Foo::new();
2633 /// match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
2634 /// => { let tmp2 = &mut place; feed(tmp2) }, ... }
2635 /// ```
2636 /// ---
2637 ///
2638 /// ## Implementation notes
2639 ///
2640 /// To encode the distinction above, we must inject the
2641 /// temporaries `tmp1` and `tmp2`.
2642 ///
2643 /// There are two cases of interest: binding by-value, and binding by-ref.
2644 ///
2645 /// 1. Binding by-value: Things are simple.
2646 ///
2647 /// * Establishing `tmp1` creates a reference into the
2648 /// matched place. This code is emitted by
2649 /// [`Self::bind_matched_candidate_for_guard`].
2650 ///
2651 /// * `tmp2` is only initialized "lazily", after we have
2652 /// checked the guard. Thus, the code that can trigger
2653 /// moves out of the candidate can only fire after the
2654 /// guard evaluated to true. This initialization code is
2655 /// emitted by [`Self::bind_matched_candidate_for_arm_body`].
2656 ///
2657 /// 2. Binding by-reference: Things are tricky.
2658 ///
2659 /// * Here, the guard expression wants a `&&` or `&&mut`
2660 /// into the original input. This means we need to borrow
2661 /// the reference that we create for the arm.
2662 /// * So we eagerly create the reference for the arm and then take a
2663 /// reference to that.
2664 ///
2665 /// ---
2666 ///
2667 /// See these PRs for some historical context:
2668 /// - <https://github.com/rust-lang/rust/pull/49870> (introduction of autoref)
2669 /// - <https://github.com/rust-lang/rust/pull/59114> (always use autoref)
2670 fn bind_matched_candidate_for_guard<'b>(
2671 &mut self,
2672 block: BasicBlock,
2673 schedule_drops: ScheduleDrops,
2674 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2675 ) where
2676 'tcx: 'b,
2677 {
2678 debug!("bind_matched_candidate_for_guard(block={:?})", block);
2679
2680 // Assign each of the bindings. Since we are binding for a
2681 // guard expression, this will never trigger moves out of the
2682 // candidate.
2683 let re_erased = self.tcx.lifetimes.re_erased;
2684 for binding in bindings {
2685 debug!("bind_matched_candidate_for_guard(binding={:?})", binding);
2686 let source_info = self.source_info(binding.span);
2687
2688 // For each pattern ident P of type T, `ref_for_guard` is
2689 // a reference R: &T pointing to the location matched by
2690 // the pattern, and every occurrence of P within a guard
2691 // denotes *R.
2692 let ref_for_guard = self.storage_live_binding(
2693 block,
2694 binding.var_id,
2695 binding.span,
2696 RefWithinGuard,
2697 schedule_drops,
2698 );
2699 match binding.binding_mode.0 {
2700 ByRef::No => {
2701 // The arm binding will be by value, so for the guard binding
2702 // just take a shared reference to the matched place.
2703 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
2704 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2705 }
2706 ByRef::Yes(mutbl) => {
2707 // The arm binding will be by reference, so eagerly create it now.
2708 let value_for_arm = self.storage_live_binding(
2709 block,
2710 binding.var_id,
2711 binding.span,
2712 OutsideGuard,
2713 schedule_drops,
2714 );
2715
2716 let rvalue =
2717 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source);
2718 self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
2719 // For the guard binding, take a shared reference to that reference.
2720 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
2721 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2722 }
2723 }
2724 }
2725 }
2726
2727 fn bind_matched_candidate_for_arm_body<'b>(
2728 &mut self,
2729 block: BasicBlock,
2730 schedule_drops: ScheduleDrops,
2731 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2732 emit_storage_live: EmitStorageLive,
2733 ) where
2734 'tcx: 'b,
2735 {
2736 debug!("bind_matched_candidate_for_arm_body(block={:?})", block);
2737
2738 let re_erased = self.tcx.lifetimes.re_erased;
2739 // Assign each of the bindings. This may trigger moves out of the candidate.
2740 for binding in bindings {
2741 let source_info = self.source_info(binding.span);
2742 let local = match emit_storage_live {
2743 // Here storages are already alive, probably because this is a binding
2744 // from let-else.
2745 // We just need to schedule drop for the value.
2746 EmitStorageLive::No => self.var_local_id(binding.var_id, OutsideGuard).into(),
2747 EmitStorageLive::Yes => self.storage_live_binding(
2748 block,
2749 binding.var_id,
2750 binding.span,
2751 OutsideGuard,
2752 schedule_drops,
2753 ),
2754 };
2755 if matches!(schedule_drops, ScheduleDrops::Yes) {
2756 self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2757 }
2758 let rvalue = match binding.binding_mode.0 {
2759 ByRef::No => Rvalue::Use(self.consume_by_copy_or_move(binding.source)),
2760 ByRef::Yes(mutbl) => {
2761 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source)
2762 }
2763 };
2764 self.cfg.push_assign(block, source_info, local, rvalue);
2765 }
2766 }
2767
2768 /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2769 /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2770 /// first local is a binding for occurrences of `var` in the guard, which
2771 /// will have type `&T`. The second local is a binding for occurrences of
2772 /// `var` in the arm body, which will have type `T`.
2773 #[instrument(skip(self), level = "debug")]
2774 fn declare_binding(
2775 &mut self,
2776 source_info: SourceInfo,
2777 visibility_scope: SourceScope,
2778 name: Symbol,
2779 mode: BindingMode,
2780 var_id: LocalVarId,
2781 var_ty: Ty<'tcx>,
2782 user_ty: Option<Box<UserTypeProjections>>,
2783 has_guard: ArmHasGuard,
2784 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2785 pat_span: Span,
2786 ) {
2787 let tcx = self.tcx;
2788 let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
2789 let local = LocalDecl {
2790 mutability: mode.1,
2791 ty: var_ty,
2792 user_ty,
2793 source_info,
2794 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(BindingForm::Var(
2795 VarBindingForm {
2796 binding_mode: mode,
2797 // hypothetically, `visit_primary_bindings` could try to unzip
2798 // an outermost hir::Ty as we descend, matching up
2799 // idents in pat; but complex w/ unclear UI payoff.
2800 // Instead, just abandon providing diagnostic info.
2801 opt_ty_info: None,
2802 opt_match_place,
2803 pat_span,
2804 },
2805 )))),
2806 };
2807 let for_arm_body = self.local_decls.push(local);
2808 if self.should_emit_debug_info_for_binding(name, var_id) {
2809 self.var_debug_info.push(VarDebugInfo {
2810 name,
2811 source_info: debug_source_info,
2812 value: VarDebugInfoContents::Place(for_arm_body.into()),
2813 composite: None,
2814 argument_index: None,
2815 });
2816 }
2817 let locals = if has_guard.0 {
2818 let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
2819 // This variable isn't mutated but has a name, so has to be
2820 // immutable to avoid the unused mut lint.
2821 mutability: Mutability::Not,
2822 ty: Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, var_ty),
2823 user_ty: None,
2824 source_info,
2825 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(
2826 BindingForm::RefForGuard,
2827 ))),
2828 });
2829 if self.should_emit_debug_info_for_binding(name, var_id) {
2830 self.var_debug_info.push(VarDebugInfo {
2831 name,
2832 source_info: debug_source_info,
2833 value: VarDebugInfoContents::Place(ref_for_guard.into()),
2834 composite: None,
2835 argument_index: None,
2836 });
2837 }
2838 LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
2839 } else {
2840 LocalsForNode::One(for_arm_body)
2841 };
2842 debug!(?locals);
2843 self.var_indices.insert(var_id, locals);
2844 }
2845
2846 /// Some bindings are introduced when producing HIR from the AST and don't
2847 /// actually exist in the source. Skip producing debug info for those when
2848 /// we can recognize them.
2849 fn should_emit_debug_info_for_binding(&self, name: Symbol, var_id: LocalVarId) -> bool {
2850 // For now we only recognize the output of desugaring assigns.
2851 if name != sym::lhs {
2852 return true;
2853 }
2854
2855 let tcx = self.tcx;
2856 for (_, node) in tcx.hir_parent_iter(var_id.0) {
2857 // FIXME(khuey) at what point is it safe to bail on the iterator?
2858 // Can we stop at the first non-Pat node?
2859 if matches!(node, Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar(_), .. }))
2860 {
2861 return false;
2862 }
2863 }
2864
2865 true
2866 }
2867}