rustc_mir_build/builder/matches/
mod.rs

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