rustc_mir_build/builder/matches/
mod.rs

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