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