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