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