rustc_mir_build/builder/matches/
mod.rs

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