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