]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/matches/mod.rs
Preparing for merge from rustc
[rust.git] / compiler / rustc_mir_build / src / build / 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
8 use crate::build::expr::as_place::PlaceBuilder;
9 use crate::build::scope::DropKind;
10 use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
11 use crate::build::{BlockAnd, BlockAndExtension, Builder};
12 use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
13 use rustc_data_structures::{
14     fx::{FxHashSet, FxIndexMap, FxIndexSet},
15     stack::ensure_sufficient_stack,
16 };
17 use rustc_index::bit_set::BitSet;
18 use rustc_middle::middle::region;
19 use rustc_middle::mir::*;
20 use rustc_middle::thir::{self, *};
21 use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
22 use rustc_span::symbol::Symbol;
23 use rustc_span::{BytePos, Pos, Span};
24 use rustc_target::abi::VariantIdx;
25 use smallvec::{smallvec, SmallVec};
26
27 // helper functions, broken out by category:
28 mod simplify;
29 mod test;
30 mod util;
31
32 use std::borrow::Borrow;
33 use std::mem;
34
35 impl<'a, 'tcx> Builder<'a, 'tcx> {
36     pub(crate) fn then_else_break(
37         &mut self,
38         mut block: BasicBlock,
39         expr: &Expr<'tcx>,
40         temp_scope_override: Option<region::Scope>,
41         break_scope: region::Scope,
42         variable_source_info: SourceInfo,
43     ) -> BlockAnd<()> {
44         let this = self;
45         let expr_span = expr.span;
46
47         match expr.kind {
48             ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
49                 let lhs_then_block = unpack!(this.then_else_break(
50                     block,
51                     &this.thir[lhs],
52                     temp_scope_override,
53                     break_scope,
54                     variable_source_info,
55                 ));
56
57                 let rhs_then_block = unpack!(this.then_else_break(
58                     lhs_then_block,
59                     &this.thir[rhs],
60                     temp_scope_override,
61                     break_scope,
62                     variable_source_info,
63                 ));
64
65                 rhs_then_block.unit()
66             }
67             ExprKind::Scope { region_scope, lint_level, value } => {
68                 let region_scope = (region_scope, this.source_info(expr_span));
69                 this.in_scope(region_scope, lint_level, |this| {
70                     this.then_else_break(
71                         block,
72                         &this.thir[value],
73                         temp_scope_override,
74                         break_scope,
75                         variable_source_info,
76                     )
77                 })
78             }
79             ExprKind::Let { expr, ref pat } => this.lower_let_expr(
80                 block,
81                 &this.thir[expr],
82                 pat,
83                 break_scope,
84                 Some(variable_source_info.scope),
85                 variable_source_info.span,
86                 true,
87             ),
88             _ => {
89                 let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope());
90                 let mutability = Mutability::Mut;
91                 let place =
92                     unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability));
93                 let operand = Operand::Move(Place::from(place));
94
95                 let then_block = this.cfg.start_new_block();
96                 let else_block = this.cfg.start_new_block();
97                 let term = TerminatorKind::if_(operand, then_block, else_block);
98
99                 let source_info = this.source_info(expr_span);
100                 this.cfg.terminate(block, source_info, term);
101                 this.break_for_else(else_block, break_scope, source_info);
102
103                 then_block.unit()
104             }
105         }
106     }
107
108     /// Generates MIR for a `match` expression.
109     ///
110     /// The MIR that we generate for a match looks like this.
111     ///
112     /// ```text
113     /// [ 0. Pre-match ]
114     ///        |
115     /// [ 1. Evaluate Scrutinee (expression being matched on) ]
116     /// [ (fake read of scrutinee) ]
117     ///        |
118     /// [ 2. Decision tree -- check discriminants ] <--------+
119     ///        |                                             |
120     ///        | (once a specific arm is chosen)             |
121     ///        |                                             |
122     /// [pre_binding_block]                           [otherwise_block]
123     ///        |                                             |
124     /// [ 3. Create "guard bindings" for arm ]               |
125     /// [ (create fake borrows) ]                            |
126     ///        |                                             |
127     /// [ 4. Execute guard code ]                            |
128     /// [ (read fake borrows) ] --(guard is false)-----------+
129     ///        |
130     ///        | (guard results in true)
131     ///        |
132     /// [ 5. Create real bindings and execute arm ]
133     ///        |
134     /// [ Exit match ]
135     /// ```
136     ///
137     /// All of the different arms have been stacked on top of each other to
138     /// simplify the diagram. For an arm with no guard the blocks marked 3 and
139     /// 4 and the fake borrows are omitted.
140     ///
141     /// We generate MIR in the following steps:
142     ///
143     /// 1. Evaluate the scrutinee and add the fake read of it ([Builder::lower_scrutinee]).
144     /// 2. Create the decision tree ([Builder::lower_match_tree]).
145     /// 3. Determine the fake borrows that are needed from the places that were
146     ///    matched against and create the required temporaries for them
147     ///    ([Builder::calculate_fake_borrows]).
148     /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]).
149     ///
150     /// ## False edges
151     ///
152     /// We don't want to have the exact structure of the decision tree be
153     /// visible through borrow checking. False edges ensure that the CFG as
154     /// seen by borrow checking doesn't encode this. False edges are added:
155     ///
156     /// * From each pre-binding block to the next pre-binding block.
157     /// * From each otherwise block to the next pre-binding block.
158     #[instrument(level = "debug", skip(self, arms))]
159     pub(crate) fn match_expr(
160         &mut self,
161         destination: Place<'tcx>,
162         span: Span,
163         mut block: BasicBlock,
164         scrutinee: &Expr<'tcx>,
165         arms: &[ArmId],
166     ) -> BlockAnd<()> {
167         let scrutinee_span = scrutinee.span;
168         let scrutinee_place =
169             unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,));
170
171         let mut arm_candidates = self.create_match_candidates(&scrutinee_place, &arms);
172
173         let match_has_guard = arm_candidates.iter().any(|(_, candidate)| candidate.has_guard);
174         let mut candidates =
175             arm_candidates.iter_mut().map(|(_, candidate)| candidate).collect::<Vec<_>>();
176
177         let match_start_span = span.shrink_to_lo().to(scrutinee.span);
178
179         let fake_borrow_temps = self.lower_match_tree(
180             block,
181             scrutinee_span,
182             match_start_span,
183             match_has_guard,
184             &mut candidates,
185         );
186
187         self.lower_match_arms(
188             destination,
189             scrutinee_place,
190             scrutinee_span,
191             arm_candidates,
192             self.source_info(span),
193             fake_borrow_temps,
194         )
195     }
196
197     /// Evaluate the scrutinee and add the fake read of it.
198     fn lower_scrutinee(
199         &mut self,
200         mut block: BasicBlock,
201         scrutinee: &Expr<'tcx>,
202         scrutinee_span: Span,
203     ) -> BlockAnd<PlaceBuilder<'tcx>> {
204         let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee));
205         // Matching on a `scrutinee_place` with an uninhabited type doesn't
206         // generate any memory reads by itself, and so if the place "expression"
207         // contains unsafe operations like raw pointer dereferences or union
208         // field projections, we wouldn't know to require an `unsafe` block
209         // around a `match` equivalent to `std::intrinsics::unreachable()`.
210         // See issue #47412 for this hole being discovered in the wild.
211         //
212         // HACK(eddyb) Work around the above issue by adding a dummy inspection
213         // of `scrutinee_place`, specifically by applying `ReadForMatch`.
214         //
215         // NOTE: ReadForMatch also checks that the scrutinee is initialized.
216         // This is currently needed to not allow matching on an uninitialized,
217         // uninhabited value. If we get never patterns, those will check that
218         // the place is initialized, and so this read would only be used to
219         // check safety.
220         let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
221         let source_info = self.source_info(scrutinee_span);
222
223         if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
224             self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
225         }
226
227         block.and(scrutinee_place_builder)
228     }
229
230     /// Create the initial `Candidate`s for a `match` expression.
231     fn create_match_candidates<'pat>(
232         &mut self,
233         scrutinee: &PlaceBuilder<'tcx>,
234         arms: &'pat [ArmId],
235     ) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)>
236     where
237         'a: 'pat,
238     {
239         // Assemble a list of candidates: there is one candidate per pattern,
240         // which means there may be more than one candidate *per arm*.
241         arms.iter()
242             .copied()
243             .map(|arm| {
244                 let arm = &self.thir[arm];
245                 let arm_has_guard = arm.guard.is_some();
246                 let arm_candidate =
247                     Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard, self);
248                 (arm, arm_candidate)
249             })
250             .collect()
251     }
252
253     /// Create the decision tree for the match expression, starting from `block`.
254     ///
255     /// Modifies `candidates` to store the bindings and type ascriptions for
256     /// that candidate.
257     ///
258     /// Returns the places that need fake borrows because we bind or test them.
259     fn lower_match_tree<'pat>(
260         &mut self,
261         block: BasicBlock,
262         scrutinee_span: Span,
263         match_start_span: Span,
264         match_has_guard: bool,
265         candidates: &mut [&mut Candidate<'pat, 'tcx>],
266     ) -> Vec<(Place<'tcx>, Local)> {
267         // The set of places that we are creating fake borrows of. If there are
268         // no match guards then we don't need any fake borrows, so don't track
269         // them.
270         let mut fake_borrows = match_has_guard.then(FxIndexSet::default);
271
272         let mut otherwise = None;
273
274         // This will generate code to test scrutinee_place and
275         // branch to the appropriate arm block
276         self.match_candidates(
277             match_start_span,
278             scrutinee_span,
279             block,
280             &mut otherwise,
281             candidates,
282             &mut fake_borrows,
283         );
284
285         if let Some(otherwise_block) = otherwise {
286             // See the doc comment on `match_candidates` for why we may have an
287             // otherwise block. Match checking will ensure this is actually
288             // unreachable.
289             let source_info = self.source_info(scrutinee_span);
290             self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
291         }
292
293         // Link each leaf candidate to the `pre_binding_block` of the next one.
294         let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
295
296         for candidate in candidates {
297             candidate.visit_leaves(|leaf_candidate| {
298                 if let Some(ref mut prev) = previous_candidate {
299                     prev.next_candidate_pre_binding_block = leaf_candidate.pre_binding_block;
300                 }
301                 previous_candidate = Some(leaf_candidate);
302             });
303         }
304
305         if let Some(ref borrows) = fake_borrows {
306             self.calculate_fake_borrows(borrows, scrutinee_span)
307         } else {
308             Vec::new()
309         }
310     }
311
312     /// Lower the bindings, guards and arm bodies of a `match` expression.
313     ///
314     /// The decision tree should have already been created
315     /// (by [Builder::lower_match_tree]).
316     ///
317     /// `outer_source_info` is the SourceInfo for the whole match.
318     fn lower_match_arms(
319         &mut self,
320         destination: Place<'tcx>,
321         scrutinee_place_builder: PlaceBuilder<'tcx>,
322         scrutinee_span: Span,
323         arm_candidates: Vec<(&'_ Arm<'tcx>, Candidate<'_, 'tcx>)>,
324         outer_source_info: SourceInfo,
325         fake_borrow_temps: Vec<(Place<'tcx>, Local)>,
326     ) -> BlockAnd<()> {
327         let arm_end_blocks: Vec<_> = arm_candidates
328             .into_iter()
329             .map(|(arm, candidate)| {
330                 debug!("lowering arm {:?}\ncandidate = {:?}", arm, candidate);
331
332                 let arm_source_info = self.source_info(arm.span);
333                 let arm_scope = (arm.scope, arm_source_info);
334                 let match_scope = self.local_scope();
335                 self.in_scope(arm_scope, arm.lint_level, |this| {
336                     // `try_to_place` may fail if it is unable to resolve the given
337                     // `PlaceBuilder` inside a closure. In this case, we don't want to include
338                     // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
339                     // if the only match arm is a wildcard (`_`).
340                     // Example:
341                     // ```
342                     // let foo = (0, 1);
343                     // let c = || {
344                     //    match foo { _ => () };
345                     // };
346                     // ```
347                     let scrutinee_place = scrutinee_place_builder.try_to_place(this);
348                     let opt_scrutinee_place =
349                         scrutinee_place.as_ref().map(|place| (Some(place), scrutinee_span));
350                     let scope = this.declare_bindings(
351                         None,
352                         arm.span,
353                         &arm.pattern,
354                         arm.guard.as_ref(),
355                         opt_scrutinee_place,
356                     );
357
358                     let arm_block = this.bind_pattern(
359                         outer_source_info,
360                         candidate,
361                         &fake_borrow_temps,
362                         scrutinee_span,
363                         Some((arm, match_scope)),
364                         false,
365                     );
366
367                     if let Some(source_scope) = scope {
368                         this.source_scope = source_scope;
369                     }
370
371                     this.expr_into_dest(destination, arm_block, &&this.thir[arm.body])
372                 })
373             })
374             .collect();
375
376         // all the arm blocks will rejoin here
377         let end_block = self.cfg.start_new_block();
378
379         let end_brace = self.source_info(
380             outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
381         );
382         for arm_block in arm_end_blocks {
383             let block = &self.cfg.basic_blocks[arm_block.0];
384             let last_location = block.statements.last().map(|s| s.source_info);
385
386             self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block);
387         }
388
389         self.source_scope = outer_source_info.scope;
390
391         end_block.unit()
392     }
393
394     /// Binds the variables and ascribes types for a given `match` arm or
395     /// `let` binding.
396     ///
397     /// Also check if the guard matches, if it's provided.
398     /// `arm_scope` should be `Some` if and only if this is called for a
399     /// `match` arm.
400     fn bind_pattern(
401         &mut self,
402         outer_source_info: SourceInfo,
403         candidate: Candidate<'_, 'tcx>,
404         fake_borrow_temps: &[(Place<'tcx>, Local)],
405         scrutinee_span: Span,
406         arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
407         storages_alive: bool,
408     ) -> BasicBlock {
409         if candidate.subcandidates.is_empty() {
410             // Avoid generating another `BasicBlock` when we only have one
411             // candidate.
412             self.bind_and_guard_matched_candidate(
413                 candidate,
414                 &[],
415                 fake_borrow_temps,
416                 scrutinee_span,
417                 arm_match_scope,
418                 true,
419                 storages_alive,
420             )
421         } else {
422             // It's helpful to avoid scheduling drops multiple times to save
423             // drop elaboration from having to clean up the extra drops.
424             //
425             // If we are in a `let` then we only schedule drops for the first
426             // candidate.
427             //
428             // If we're in a `match` arm then we could have a case like so:
429             //
430             // Ok(x) | Err(x) if return => { /* ... */ }
431             //
432             // In this case we don't want a drop of `x` scheduled when we
433             // return: it isn't bound by move until right before enter the arm.
434             // To handle this we instead unschedule it's drop after each time
435             // we lower the guard.
436             let target_block = self.cfg.start_new_block();
437             let mut schedule_drops = true;
438             let arm = arm_match_scope.unzip().0;
439             // We keep a stack of all of the bindings and type ascriptions
440             // from the parent candidates that we visit, that also need to
441             // be bound for each candidate.
442             traverse_candidate(
443                 candidate,
444                 &mut Vec::new(),
445                 &mut |leaf_candidate, parent_bindings| {
446                     if let Some(arm) = arm {
447                         self.clear_top_scope(arm.scope);
448                     }
449                     let binding_end = self.bind_and_guard_matched_candidate(
450                         leaf_candidate,
451                         parent_bindings,
452                         &fake_borrow_temps,
453                         scrutinee_span,
454                         arm_match_scope,
455                         schedule_drops,
456                         storages_alive,
457                     );
458                     if arm.is_none() {
459                         schedule_drops = false;
460                     }
461                     self.cfg.goto(binding_end, outer_source_info, target_block);
462                 },
463                 |inner_candidate, parent_bindings| {
464                     parent_bindings.push((inner_candidate.bindings, inner_candidate.ascriptions));
465                     inner_candidate.subcandidates.into_iter()
466                 },
467                 |parent_bindings| {
468                     parent_bindings.pop();
469                 },
470             );
471
472             target_block
473         }
474     }
475
476     pub(super) fn expr_into_pattern(
477         &mut self,
478         mut block: BasicBlock,
479         irrefutable_pat: &Pat<'tcx>,
480         initializer: &Expr<'tcx>,
481     ) -> BlockAnd<()> {
482         match irrefutable_pat.kind {
483             // Optimize the case of `let x = ...` to write directly into `x`
484             PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => {
485                 let place =
486                     self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
487                 unpack!(block = self.expr_into_dest(place, block, initializer));
488
489                 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
490                 let source_info = self.source_info(irrefutable_pat.span);
491                 self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place);
492
493                 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
494                 block.unit()
495             }
496
497             // Optimize the case of `let x: T = ...` to write directly
498             // into `x` and then require that `T == typeof(x)`.
499             //
500             // Weirdly, this is needed to prevent the
501             // `intrinsic-move-val.rs` test case from crashing. That
502             // test works with uninitialized values in a rather
503             // dubious way, so it may be that the test is kind of
504             // broken.
505             PatKind::AscribeUserType {
506                 subpattern:
507                     box Pat {
508                         kind:
509                             PatKind::Binding {
510                                 mode: BindingMode::ByValue, var, subpattern: None, ..
511                             },
512                         ..
513                     },
514                 ascription: thir::Ascription { ref annotation, variance: _ },
515             } => {
516                 let place =
517                     self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
518                 unpack!(block = self.expr_into_dest(place, block, initializer));
519
520                 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
521                 let pattern_source_info = self.source_info(irrefutable_pat.span);
522                 let cause_let = FakeReadCause::ForLet(None);
523                 self.cfg.push_fake_read(block, pattern_source_info, cause_let, place);
524
525                 let ty_source_info = self.source_info(annotation.span);
526
527                 let base = self.canonical_user_type_annotations.push(annotation.clone());
528                 self.cfg.push(
529                     block,
530                     Statement {
531                         source_info: ty_source_info,
532                         kind: StatementKind::AscribeUserType(
533                             Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
534                             // We always use invariant as the variance here. This is because the
535                             // variance field from the ascription refers to the variance to use
536                             // when applying the type to the value being matched, but this
537                             // ascription applies rather to the type of the binding. e.g., in this
538                             // example:
539                             //
540                             // ```
541                             // let x: T = <expr>
542                             // ```
543                             //
544                             // We are creating an ascription that defines the type of `x` to be
545                             // exactly `T` (i.e., with invariance). The variance field, in
546                             // contrast, is intended to be used to relate `T` to the type of
547                             // `<expr>`.
548                             ty::Variance::Invariant,
549                         ),
550                     },
551                 );
552
553                 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
554                 block.unit()
555             }
556
557             _ => {
558                 let place_builder = unpack!(block = self.as_place_builder(block, initializer));
559                 self.place_into_pattern(block, &irrefutable_pat, place_builder, true)
560             }
561         }
562     }
563
564     pub(crate) fn place_into_pattern(
565         &mut self,
566         block: BasicBlock,
567         irrefutable_pat: &Pat<'tcx>,
568         initializer: PlaceBuilder<'tcx>,
569         set_match_place: bool,
570     ) -> BlockAnd<()> {
571         let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false, self);
572         let fake_borrow_temps = self.lower_match_tree(
573             block,
574             irrefutable_pat.span,
575             irrefutable_pat.span,
576             false,
577             &mut [&mut candidate],
578         );
579         // For matches and function arguments, the place that is being matched
580         // can be set when creating the variables. But the place for
581         // let PATTERN = ... might not even exist until we do the assignment.
582         // so we set it here instead.
583         if set_match_place {
584             let mut candidate_ref = &candidate;
585             while let Some(next) = {
586                 for binding in &candidate_ref.bindings {
587                     let local = self.var_local_id(binding.var_id, OutsideGuard);
588                     // `try_to_place` may fail if it is unable to resolve the given
589                     // `PlaceBuilder` inside a closure. In this case, we don't want to include
590                     // a scrutinee place. `scrutinee_place_builder` will fail for destructured
591                     // assignments. This is because a closure only captures the precise places
592                     // that it will read and as a result a closure may not capture the entire
593                     // tuple/struct and rather have individual places that will be read in the
594                     // final MIR.
595                     // Example:
596                     // ```
597                     // let foo = (0, 1);
598                     // let c = || {
599                     //    let (v1, v2) = foo;
600                     // };
601                     // ```
602                     if let Some(place) = initializer.try_to_place(self) {
603                         let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
604                             VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
605                         )))) = self.local_decls[local].local_info else {
606                             bug!("Let binding to non-user variable.")
607                         };
608                         *match_place = Some(place);
609                     }
610                 }
611                 // All of the subcandidates should bind the same locals, so we
612                 // only visit the first one.
613                 candidate_ref.subcandidates.get(0)
614             } {
615                 candidate_ref = next;
616             }
617         }
618
619         self.bind_pattern(
620             self.source_info(irrefutable_pat.span),
621             candidate,
622             &fake_borrow_temps,
623             irrefutable_pat.span,
624             None,
625             false,
626         )
627         .unit()
628     }
629
630     /// Declares the bindings of the given patterns and returns the visibility
631     /// scope for the bindings in these patterns, if such a scope had to be
632     /// created. NOTE: Declaring the bindings should always be done in their
633     /// drop scope.
634     #[instrument(skip(self), level = "debug")]
635     pub(crate) fn declare_bindings(
636         &mut self,
637         mut visibility_scope: Option<SourceScope>,
638         scope_span: Span,
639         pattern: &Pat<'tcx>,
640         guard: Option<&Guard<'tcx>>,
641         opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
642     ) -> Option<SourceScope> {
643         self.visit_primary_bindings(
644             &pattern,
645             UserTypeProjections::none(),
646             &mut |this, mutability, name, mode, var, span, ty, user_ty| {
647                 if visibility_scope.is_none() {
648                     visibility_scope =
649                         Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
650                 }
651                 let source_info = SourceInfo { span, scope: this.source_scope };
652                 let visibility_scope = visibility_scope.unwrap();
653                 this.declare_binding(
654                     source_info,
655                     visibility_scope,
656                     mutability,
657                     name,
658                     mode,
659                     var,
660                     ty,
661                     user_ty,
662                     ArmHasGuard(guard.is_some()),
663                     opt_match_place.map(|(x, y)| (x.cloned(), y)),
664                     pattern.span,
665                 );
666             },
667         );
668         if let Some(Guard::IfLet(guard_pat, _)) = guard {
669             // FIXME: pass a proper `opt_match_place`
670             self.declare_bindings(visibility_scope, scope_span, guard_pat, None, None);
671         }
672         visibility_scope
673     }
674
675     pub(crate) fn storage_live_binding(
676         &mut self,
677         block: BasicBlock,
678         var: LocalVarId,
679         span: Span,
680         for_guard: ForGuard,
681         schedule_drop: bool,
682     ) -> Place<'tcx> {
683         let local_id = self.var_local_id(var, for_guard);
684         let source_info = self.source_info(span);
685         self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
686         // Although there is almost always scope for given variable in corner cases
687         // like #92893 we might get variable with no scope.
688         if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop {
689             self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
690         }
691         Place::from(local_id)
692     }
693
694     pub(crate) fn schedule_drop_for_binding(
695         &mut self,
696         var: LocalVarId,
697         span: Span,
698         for_guard: ForGuard,
699     ) {
700         let local_id = self.var_local_id(var, for_guard);
701         if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
702             self.schedule_drop(span, region_scope, local_id, DropKind::Value);
703         }
704     }
705
706     /// Visit all of the primary bindings in a patterns, that is, visit the
707     /// leftmost occurrence of each variable bound in a pattern. A variable
708     /// will occur more than once in an or-pattern.
709     pub(super) fn visit_primary_bindings(
710         &mut self,
711         pattern: &Pat<'tcx>,
712         pattern_user_ty: UserTypeProjections,
713         f: &mut impl FnMut(
714             &mut Self,
715             Mutability,
716             Symbol,
717             BindingMode,
718             LocalVarId,
719             Span,
720             Ty<'tcx>,
721             UserTypeProjections,
722         ),
723     ) {
724         debug!(
725             "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}",
726             pattern, pattern_user_ty
727         );
728         match pattern.kind {
729             PatKind::Binding {
730                 mutability,
731                 name,
732                 mode,
733                 var,
734                 ty,
735                 ref subpattern,
736                 is_primary,
737                 ..
738             } => {
739                 if is_primary {
740                     f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty.clone());
741                 }
742                 if let Some(subpattern) = subpattern.as_ref() {
743                     self.visit_primary_bindings(subpattern, pattern_user_ty, f);
744                 }
745             }
746
747             PatKind::Array { ref prefix, ref slice, ref suffix }
748             | PatKind::Slice { ref prefix, ref slice, ref suffix } => {
749                 let from = u64::try_from(prefix.len()).unwrap();
750                 let to = u64::try_from(suffix.len()).unwrap();
751                 for subpattern in prefix.iter() {
752                     self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
753                 }
754                 for subpattern in slice {
755                     self.visit_primary_bindings(
756                         subpattern,
757                         pattern_user_ty.clone().subslice(from, to),
758                         f,
759                     );
760                 }
761                 for subpattern in suffix.iter() {
762                     self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
763                 }
764             }
765
766             PatKind::Constant { .. } | PatKind::Range { .. } | PatKind::Wild => {}
767
768             PatKind::Deref { ref subpattern } => {
769                 self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
770             }
771
772             PatKind::AscribeUserType {
773                 ref subpattern,
774                 ascription: thir::Ascription { ref annotation, variance: _ },
775             } => {
776                 // This corresponds to something like
777                 //
778                 // ```
779                 // let A::<'a>(_): A<'static> = ...;
780                 // ```
781                 //
782                 // Note that the variance doesn't apply here, as we are tracking the effect
783                 // of `user_ty` on any bindings contained with subpattern.
784
785                 let projection = UserTypeProjection {
786                     base: self.canonical_user_type_annotations.push(annotation.clone()),
787                     projs: Vec::new(),
788                 };
789                 let subpattern_user_ty =
790                     pattern_user_ty.push_projection(&projection, annotation.span);
791                 self.visit_primary_bindings(subpattern, subpattern_user_ty, f)
792             }
793
794             PatKind::Leaf { ref subpatterns } => {
795                 for subpattern in subpatterns {
796                     let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);
797                     debug!("visit_primary_bindings: subpattern_user_ty={:?}", subpattern_user_ty);
798                     self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
799                 }
800             }
801
802             PatKind::Variant { adt_def, substs: _, variant_index, ref subpatterns } => {
803                 for subpattern in subpatterns {
804                     let subpattern_user_ty =
805                         pattern_user_ty.clone().variant(adt_def, variant_index, subpattern.field);
806                     self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
807                 }
808             }
809             PatKind::Or { ref pats } => {
810                 // In cases where we recover from errors the primary bindings
811                 // may not all be in the leftmost subpattern. For example in
812                 // `let (x | y) = ...`, the primary binding of `y` occurs in
813                 // the right subpattern
814                 for subpattern in pats.iter() {
815                     self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f);
816                 }
817             }
818         }
819     }
820 }
821
822 #[derive(Debug)]
823 struct Candidate<'pat, 'tcx> {
824     /// [`Span`] of the original pattern that gave rise to this candidate.
825     span: Span,
826
827     /// Whether this `Candidate` has a guard.
828     has_guard: bool,
829
830     /// All of these must be satisfied...
831     match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
832
833     /// ...these bindings established...
834     bindings: Vec<Binding<'tcx>>,
835
836     /// ...and these types asserted...
837     ascriptions: Vec<Ascription<'tcx>>,
838
839     /// ...and if this is non-empty, one of these subcandidates also has to match...
840     subcandidates: Vec<Candidate<'pat, 'tcx>>,
841
842     /// ...and the guard must be evaluated; if it's `false` then branch to `otherwise_block`.
843     otherwise_block: Option<BasicBlock>,
844
845     /// The block before the `bindings` have been established.
846     pre_binding_block: Option<BasicBlock>,
847     /// The pre-binding block of the next candidate.
848     next_candidate_pre_binding_block: Option<BasicBlock>,
849 }
850
851 impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
852     fn new(
853         place: PlaceBuilder<'tcx>,
854         pattern: &'pat Pat<'tcx>,
855         has_guard: bool,
856         cx: &Builder<'_, 'tcx>,
857     ) -> Self {
858         Candidate {
859             span: pattern.span,
860             has_guard,
861             match_pairs: smallvec![MatchPair::new(place, pattern, cx)],
862             bindings: Vec::new(),
863             ascriptions: Vec::new(),
864             subcandidates: Vec::new(),
865             otherwise_block: None,
866             pre_binding_block: None,
867             next_candidate_pre_binding_block: None,
868         }
869     }
870
871     /// Visit the leaf candidates (those with no subcandidates) contained in
872     /// this candidate.
873     fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
874         traverse_candidate(
875             self,
876             &mut (),
877             &mut move |c, _| visit_leaf(c),
878             move |c, _| c.subcandidates.iter_mut(),
879             |_| {},
880         );
881     }
882 }
883
884 /// A depth-first traversal of the `Candidate` and all of its recursive
885 /// subcandidates.
886 fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>(
887     candidate: C,
888     context: &mut T,
889     visit_leaf: &mut impl FnMut(C, &mut T),
890     get_children: impl Copy + Fn(C, &mut T) -> I,
891     complete_children: impl Copy + Fn(&mut T),
892 ) where
893     C: Borrow<Candidate<'pat, 'tcx>>,
894     I: Iterator<Item = C>,
895 {
896     if candidate.borrow().subcandidates.is_empty() {
897         visit_leaf(candidate, context)
898     } else {
899         for child in get_children(candidate, context) {
900             traverse_candidate(child, context, visit_leaf, get_children, complete_children);
901         }
902         complete_children(context)
903     }
904 }
905
906 #[derive(Clone, Debug)]
907 struct Binding<'tcx> {
908     span: Span,
909     source: Place<'tcx>,
910     var_id: LocalVarId,
911     binding_mode: BindingMode,
912 }
913
914 /// Indicates that the type of `source` must be a subtype of the
915 /// user-given type `user_ty`; this is basically a no-op but can
916 /// influence region inference.
917 #[derive(Clone, Debug)]
918 struct Ascription<'tcx> {
919     source: Place<'tcx>,
920     annotation: CanonicalUserTypeAnnotation<'tcx>,
921     variance: ty::Variance,
922 }
923
924 #[derive(Clone, Debug)]
925 pub(crate) struct MatchPair<'pat, 'tcx> {
926     // this place...
927     place: PlaceBuilder<'tcx>,
928
929     // ... must match this pattern.
930     pattern: &'pat Pat<'tcx>,
931 }
932
933 /// See [`Test`] for more.
934 #[derive(Clone, Debug, PartialEq)]
935 enum TestKind<'tcx> {
936     /// Test what enum variant a value is.
937     Switch {
938         /// The enum type being tested.
939         adt_def: ty::AdtDef<'tcx>,
940         /// The set of variants that we should create a branch for. We also
941         /// create an additional "otherwise" case.
942         variants: BitSet<VariantIdx>,
943     },
944
945     /// Test what value an integer, `bool`, or `char` has.
946     SwitchInt {
947         /// The type of the value that we're testing.
948         switch_ty: Ty<'tcx>,
949         /// The (ordered) set of values that we test for.
950         ///
951         /// For integers and `char`s we create a branch to each of the values in
952         /// `options`, as well as an "otherwise" branch for all other values, even
953         /// in the (rare) case that `options` is exhaustive.
954         ///
955         /// For `bool` we always generate two edges, one for `true` and one for
956         /// `false`.
957         options: FxIndexMap<ConstantKind<'tcx>, u128>,
958     },
959
960     /// Test for equality with value, possibly after an unsizing coercion to
961     /// `ty`,
962     Eq {
963         value: ConstantKind<'tcx>,
964         // Integer types are handled by `SwitchInt`, and constants with ADT
965         // types are converted back into patterns, so this can only be `&str`,
966         // `&[T]`, `f32` or `f64`.
967         ty: Ty<'tcx>,
968     },
969
970     /// Test whether the value falls within an inclusive or exclusive range
971     Range(Box<PatRange<'tcx>>),
972
973     /// Test that the length of the slice is equal to `len`.
974     Len { len: u64, op: BinOp },
975 }
976
977 /// A test to perform to determine which [`Candidate`] matches a value.
978 ///
979 /// [`Test`] is just the test to perform; it does not include the value
980 /// to be tested.
981 #[derive(Debug)]
982 pub(crate) struct Test<'tcx> {
983     span: Span,
984     kind: TestKind<'tcx>,
985 }
986
987 /// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
988 /// a match arm has a guard expression attached to it.
989 #[derive(Copy, Clone, Debug)]
990 pub(crate) struct ArmHasGuard(pub(crate) bool);
991
992 ///////////////////////////////////////////////////////////////////////////
993 // Main matching algorithm
994
995 impl<'a, 'tcx> Builder<'a, 'tcx> {
996     /// The main match algorithm. It begins with a set of candidates
997     /// `candidates` and has the job of generating code to determine
998     /// which of these candidates, if any, is the correct one. The
999     /// candidates are sorted such that the first item in the list
1000     /// has the highest priority. When a candidate is found to match
1001     /// the value, we will set and generate a branch to the appropriate
1002     /// pre-binding block.
1003     ///
1004     /// If we find that *NONE* of the candidates apply, we branch to the
1005     /// `otherwise_block`, setting it to `Some` if required. In principle, this
1006     /// means that the input list was not exhaustive, though at present we
1007     /// sometimes are not smart enough to recognize all exhaustive inputs.
1008     ///
1009     /// It might be surprising that the input can be non-exhaustive.
1010     /// Indeed, initially, it is not, because all matches are
1011     /// exhaustive in Rust. But during processing we sometimes divide
1012     /// up the list of candidates and recurse with a non-exhaustive
1013     /// list. This is important to keep the size of the generated code
1014     /// under control. See [`Builder::test_candidates`] for more details.
1015     ///
1016     /// If `fake_borrows` is `Some`, then places which need fake borrows
1017     /// will be added to it.
1018     ///
1019     /// For an example of a case where we set `otherwise_block`, even for an
1020     /// exhaustive match, consider:
1021     ///
1022     /// ```
1023     /// # fn foo(x: (bool, bool)) {
1024     /// match x {
1025     ///     (true, true) => (),
1026     ///     (_, false) => (),
1027     ///     (false, true) => (),
1028     /// }
1029     /// # }
1030     /// ```
1031     ///
1032     /// For this match, we check if `x.0` matches `true` (for the first
1033     /// arm). If it doesn't match, we check `x.1`. If `x.1` is `true` we check
1034     /// if `x.0` matches `false` (for the third arm). In the (impossible at
1035     /// runtime) case when `x.0` is now `true`, we branch to
1036     /// `otherwise_block`.
1037     #[instrument(skip(self, fake_borrows), level = "debug")]
1038     fn match_candidates<'pat>(
1039         &mut self,
1040         span: Span,
1041         scrutinee_span: Span,
1042         start_block: BasicBlock,
1043         otherwise_block: &mut Option<BasicBlock>,
1044         candidates: &mut [&mut Candidate<'pat, 'tcx>],
1045         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1046     ) {
1047         // Start by simplifying candidates. Once this process is complete, all
1048         // the match pairs which remain require some form of test, whether it
1049         // be a switch or pattern comparison.
1050         let mut split_or_candidate = false;
1051         for candidate in &mut *candidates {
1052             split_or_candidate |= self.simplify_candidate(candidate);
1053         }
1054
1055         ensure_sufficient_stack(|| {
1056             if split_or_candidate {
1057                 // At least one of the candidates has been split into subcandidates.
1058                 // We need to change the candidate list to include those.
1059                 let mut new_candidates = Vec::new();
1060
1061                 for candidate in candidates {
1062                     candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
1063                 }
1064                 self.match_simplified_candidates(
1065                     span,
1066                     scrutinee_span,
1067                     start_block,
1068                     otherwise_block,
1069                     &mut *new_candidates,
1070                     fake_borrows,
1071                 );
1072             } else {
1073                 self.match_simplified_candidates(
1074                     span,
1075                     scrutinee_span,
1076                     start_block,
1077                     otherwise_block,
1078                     candidates,
1079                     fake_borrows,
1080                 );
1081             }
1082         });
1083     }
1084
1085     fn match_simplified_candidates(
1086         &mut self,
1087         span: Span,
1088         scrutinee_span: Span,
1089         start_block: BasicBlock,
1090         otherwise_block: &mut Option<BasicBlock>,
1091         candidates: &mut [&mut Candidate<'_, 'tcx>],
1092         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1093     ) {
1094         // The candidates are sorted by priority. Check to see whether the
1095         // higher priority candidates (and hence at the front of the slice)
1096         // have satisfied all their match pairs.
1097         let fully_matched = candidates.iter().take_while(|c| c.match_pairs.is_empty()).count();
1098         debug!("match_candidates: {:?} candidates fully matched", fully_matched);
1099         let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
1100
1101         let block = if !matched_candidates.is_empty() {
1102             let otherwise_block =
1103                 self.select_matched_candidates(matched_candidates, start_block, fake_borrows);
1104
1105             if let Some(last_otherwise_block) = otherwise_block {
1106                 last_otherwise_block
1107             } else {
1108                 // Any remaining candidates are unreachable.
1109                 if unmatched_candidates.is_empty() {
1110                     return;
1111                 }
1112                 self.cfg.start_new_block()
1113             }
1114         } else {
1115             start_block
1116         };
1117
1118         // If there are no candidates that still need testing, we're
1119         // done. Since all matches are exhaustive, execution should
1120         // never reach this point.
1121         if unmatched_candidates.is_empty() {
1122             let source_info = self.source_info(span);
1123             if let Some(otherwise) = *otherwise_block {
1124                 self.cfg.goto(block, source_info, otherwise);
1125             } else {
1126                 *otherwise_block = Some(block);
1127             }
1128             return;
1129         }
1130
1131         // Test for the remaining candidates.
1132         self.test_candidates_with_or(
1133             span,
1134             scrutinee_span,
1135             unmatched_candidates,
1136             block,
1137             otherwise_block,
1138             fake_borrows,
1139         );
1140     }
1141
1142     /// Link up matched candidates.
1143     ///
1144     /// For example, if we have something like this:
1145     ///
1146     /// ```ignore (illustrative)
1147     /// ...
1148     /// Some(x) if cond1 => ...
1149     /// Some(x) => ...
1150     /// Some(x) if cond2 => ...
1151     /// ...
1152     /// ```
1153     ///
1154     /// We generate real edges from:
1155     ///
1156     /// * `start_block` to the [pre-binding block] of the first pattern,
1157     /// * the [otherwise block] of the first pattern to the second pattern,
1158     /// * the [otherwise block] of the third pattern to a block with an
1159     ///   [`Unreachable` terminator](TerminatorKind::Unreachable).
1160     ///
1161     /// In addition, we add fake edges from the otherwise blocks to the
1162     /// pre-binding block of the next candidate in the original set of
1163     /// candidates.
1164     ///
1165     /// [pre-binding block]: Candidate::pre_binding_block
1166     /// [otherwise block]: Candidate::otherwise_block
1167     fn select_matched_candidates(
1168         &mut self,
1169         matched_candidates: &mut [&mut Candidate<'_, 'tcx>],
1170         start_block: BasicBlock,
1171         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1172     ) -> Option<BasicBlock> {
1173         debug_assert!(
1174             !matched_candidates.is_empty(),
1175             "select_matched_candidates called with no candidates",
1176         );
1177         debug_assert!(
1178             matched_candidates.iter().all(|c| c.subcandidates.is_empty()),
1179             "subcandidates should be empty in select_matched_candidates",
1180         );
1181
1182         // Insert a borrows of prefixes of places that are bound and are
1183         // behind a dereference projection.
1184         //
1185         // These borrows are taken to avoid situations like the following:
1186         //
1187         // match x[10] {
1188         //     _ if { x = &[0]; false } => (),
1189         //     y => (), // Out of bounds array access!
1190         // }
1191         //
1192         // match *x {
1193         //     // y is bound by reference in the guard and then by copy in the
1194         //     // arm, so y is 2 in the arm!
1195         //     y if { y == 1 && (x = &2) == () } => y,
1196         //     _ => 3,
1197         // }
1198         if let Some(fake_borrows) = fake_borrows {
1199             for Binding { source, .. } in
1200                 matched_candidates.iter().flat_map(|candidate| &candidate.bindings)
1201             {
1202                 if let Some(i) =
1203                     source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref)
1204                 {
1205                     let proj_base = &source.projection[..i];
1206
1207                     fake_borrows.insert(Place {
1208                         local: source.local,
1209                         projection: self.tcx.intern_place_elems(proj_base),
1210                     });
1211                 }
1212             }
1213         }
1214
1215         let fully_matched_with_guard = matched_candidates
1216             .iter()
1217             .position(|c| !c.has_guard)
1218             .unwrap_or(matched_candidates.len() - 1);
1219
1220         let (reachable_candidates, unreachable_candidates) =
1221             matched_candidates.split_at_mut(fully_matched_with_guard + 1);
1222
1223         let mut next_prebinding = start_block;
1224
1225         for candidate in reachable_candidates.iter_mut() {
1226             assert!(candidate.otherwise_block.is_none());
1227             assert!(candidate.pre_binding_block.is_none());
1228             candidate.pre_binding_block = Some(next_prebinding);
1229             if candidate.has_guard {
1230                 // Create the otherwise block for this candidate, which is the
1231                 // pre-binding block for the next candidate.
1232                 next_prebinding = self.cfg.start_new_block();
1233                 candidate.otherwise_block = Some(next_prebinding);
1234             }
1235         }
1236
1237         debug!(
1238             "match_candidates: add pre_binding_blocks for unreachable {:?}",
1239             unreachable_candidates,
1240         );
1241         for candidate in unreachable_candidates {
1242             assert!(candidate.pre_binding_block.is_none());
1243             candidate.pre_binding_block = Some(self.cfg.start_new_block());
1244         }
1245
1246         reachable_candidates.last_mut().unwrap().otherwise_block
1247     }
1248
1249     /// Tests a candidate where there are only or-patterns left to test, or
1250     /// forwards to [Builder::test_candidates].
1251     ///
1252     /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1253     /// so:
1254     ///
1255     /// ```text
1256     /// [ start ]
1257     ///      |
1258     /// [ match P, Q ]
1259     ///      |
1260     ///      +----------------------------------------+------------------------------------+
1261     ///      |                                        |                                    |
1262     ///      V                                        V                                    V
1263     /// [ P matches ]                           [ Q matches ]                        [ otherwise ]
1264     ///      |                                        |                                    |
1265     ///      V                                        V                                    |
1266     /// [ match R, S ]                          [ match R, S ]                             |
1267     ///      |                                        |                                    |
1268     ///      +--------------+------------+            +--------------+------------+        |
1269     ///      |              |            |            |              |            |        |
1270     ///      V              V            V            V              V            V        |
1271     /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ]  |
1272     ///      |              |            |            |              |            |        |
1273     ///      +--------------+------------|------------+--------------+            |        |
1274     ///      |                           |                                        |        |
1275     ///      |                           +----------------------------------------+--------+
1276     ///      |                           |
1277     ///      V                           V
1278     /// [ Success ]                 [ Failure ]
1279     /// ```
1280     ///
1281     /// In practice there are some complications:
1282     ///
1283     /// * If there's a guard, then the otherwise branch of the first match on
1284     ///   `R | S` goes to a test for whether `Q` matches, and the control flow
1285     ///   doesn't merge into a single success block until after the guard is
1286     ///   tested.
1287     /// * If neither `P` or `Q` has any bindings or type ascriptions and there
1288     ///   isn't a match guard, then we create a smaller CFG like:
1289     ///
1290     /// ```text
1291     ///     ...
1292     ///      +---------------+------------+
1293     ///      |               |            |
1294     /// [ P matches ] [ Q matches ] [ otherwise ]
1295     ///      |               |            |
1296     ///      +---------------+            |
1297     ///      |                           ...
1298     /// [ match R, S ]
1299     ///      |
1300     ///     ...
1301     /// ```
1302     fn test_candidates_with_or(
1303         &mut self,
1304         span: Span,
1305         scrutinee_span: Span,
1306         candidates: &mut [&mut Candidate<'_, 'tcx>],
1307         block: BasicBlock,
1308         otherwise_block: &mut Option<BasicBlock>,
1309         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1310     ) {
1311         let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap();
1312
1313         // All of the or-patterns have been sorted to the end, so if the first
1314         // pattern is an or-pattern we only have or-patterns.
1315         match first_candidate.match_pairs[0].pattern.kind {
1316             PatKind::Or { .. } => (),
1317             _ => {
1318                 self.test_candidates(
1319                     span,
1320                     scrutinee_span,
1321                     candidates,
1322                     block,
1323                     otherwise_block,
1324                     fake_borrows,
1325                 );
1326                 return;
1327             }
1328         }
1329
1330         let match_pairs = mem::take(&mut first_candidate.match_pairs);
1331         first_candidate.pre_binding_block = Some(block);
1332
1333         let mut otherwise = None;
1334         for match_pair in match_pairs {
1335             let PatKind::Or { ref pats } = &match_pair.pattern.kind else {
1336                 bug!("Or-patterns should have been sorted to the end");
1337             };
1338             let or_span = match_pair.pattern.span;
1339
1340             first_candidate.visit_leaves(|leaf_candidate| {
1341                 self.test_or_pattern(
1342                     leaf_candidate,
1343                     &mut otherwise,
1344                     pats,
1345                     or_span,
1346                     &match_pair.place,
1347                     fake_borrows,
1348                 );
1349             });
1350         }
1351
1352         let remainder_start = otherwise.unwrap_or_else(|| self.cfg.start_new_block());
1353
1354         self.match_candidates(
1355             span,
1356             scrutinee_span,
1357             remainder_start,
1358             otherwise_block,
1359             remaining_candidates,
1360             fake_borrows,
1361         )
1362     }
1363
1364     #[instrument(
1365         skip(self, otherwise, or_span, place, fake_borrows, candidate, pats),
1366         level = "debug"
1367     )]
1368     fn test_or_pattern<'pat>(
1369         &mut self,
1370         candidate: &mut Candidate<'pat, 'tcx>,
1371         otherwise: &mut Option<BasicBlock>,
1372         pats: &'pat [Box<Pat<'tcx>>],
1373         or_span: Span,
1374         place: &PlaceBuilder<'tcx>,
1375         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1376     ) {
1377         debug!("candidate={:#?}\npats={:#?}", candidate, pats);
1378         let mut or_candidates: Vec<_> = pats
1379             .iter()
1380             .map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard, self))
1381             .collect();
1382         let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
1383         let otherwise = if candidate.otherwise_block.is_some() {
1384             &mut candidate.otherwise_block
1385         } else {
1386             otherwise
1387         };
1388         self.match_candidates(
1389             or_span,
1390             or_span,
1391             candidate.pre_binding_block.unwrap(),
1392             otherwise,
1393             &mut or_candidate_refs,
1394             fake_borrows,
1395         );
1396         candidate.subcandidates = or_candidates;
1397         self.merge_trivial_subcandidates(candidate, self.source_info(or_span));
1398     }
1399
1400     /// Try to merge all of the subcandidates of the given candidate into one.
1401     /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1402     fn merge_trivial_subcandidates(
1403         &mut self,
1404         candidate: &mut Candidate<'_, 'tcx>,
1405         source_info: SourceInfo,
1406     ) {
1407         if candidate.subcandidates.is_empty() || candidate.has_guard {
1408             // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
1409             return;
1410         }
1411
1412         let mut can_merge = true;
1413
1414         // Not `Iterator::all` because we don't want to short-circuit.
1415         for subcandidate in &mut candidate.subcandidates {
1416             self.merge_trivial_subcandidates(subcandidate, source_info);
1417
1418             // FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
1419             can_merge &= subcandidate.subcandidates.is_empty()
1420                 && subcandidate.bindings.is_empty()
1421                 && subcandidate.ascriptions.is_empty();
1422         }
1423
1424         if can_merge {
1425             let any_matches = self.cfg.start_new_block();
1426             for subcandidate in mem::take(&mut candidate.subcandidates) {
1427                 let or_block = subcandidate.pre_binding_block.unwrap();
1428                 self.cfg.goto(or_block, source_info, any_matches);
1429             }
1430             candidate.pre_binding_block = Some(any_matches);
1431         }
1432     }
1433
1434     /// This is the most subtle part of the matching algorithm. At
1435     /// this point, the input candidates have been fully simplified,
1436     /// and so we know that all remaining match-pairs require some
1437     /// sort of test. To decide what test to perform, we take the highest
1438     /// priority candidate (the first one in the list, as of January 2021)
1439     /// and extract the first match-pair from the list. From this we decide
1440     /// what kind of test is needed using [`Builder::test`], defined in the
1441     /// [`test` module](mod@test).
1442     ///
1443     /// *Note:* taking the first match pair is somewhat arbitrary, and
1444     /// we might do better here by choosing more carefully what to
1445     /// test.
1446     ///
1447     /// For example, consider the following possible match-pairs:
1448     ///
1449     /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
1450     /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
1451     /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
1452     /// 4. etc.
1453     ///
1454     /// [`Switch`]: TestKind::Switch
1455     /// [`SwitchInt`]: TestKind::SwitchInt
1456     /// [`Range`]: TestKind::Range
1457     ///
1458     /// Once we know what sort of test we are going to perform, this
1459     /// test may also help us winnow down our candidates. So we walk over
1460     /// the candidates (from high to low priority) and check. This
1461     /// gives us, for each outcome of the test, a transformed list of
1462     /// candidates. For example, if we are testing `x.0`'s variant,
1463     /// and we have a candidate `(x.0 @ Some(v), x.1 @ 22)`,
1464     /// then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)`.
1465     /// Note that the first match-pair is now simpler (and, in fact, irrefutable).
1466     ///
1467     /// But there may also be candidates that the test just doesn't
1468     /// apply to. The classical example involves wildcards:
1469     ///
1470     /// ```
1471     /// # let (x, y, z) = (true, true, true);
1472     /// match (x, y, z) {
1473     ///     (true , _    , true ) => true,  // (0)
1474     ///     (_    , true , _    ) => true,  // (1)
1475     ///     (false, false, _    ) => false, // (2)
1476     ///     (true , _    , false) => false, // (3)
1477     /// }
1478     /// # ;
1479     /// ```
1480     ///
1481     /// In that case, after we test on `x`, there are 2 overlapping candidate
1482     /// sets:
1483     ///
1484     /// - If the outcome is that `x` is true, candidates 0, 1, and 3
1485     /// - If the outcome is that `x` is false, candidates 1 and 2
1486     ///
1487     /// Here, the traditional "decision tree" method would generate 2
1488     /// separate code-paths for the 2 separate cases.
1489     ///
1490     /// In some cases, this duplication can create an exponential amount of
1491     /// code. This is most easily seen by noticing that this method terminates
1492     /// with precisely the reachable arms being reachable - but that problem
1493     /// is trivially NP-complete:
1494     ///
1495     /// ```ignore (illustrative)
1496     /// match (var0, var1, var2, var3, ...) {
1497     ///     (true , _   , _    , false, true, ...) => false,
1498     ///     (_    , true, true , false, _   , ...) => false,
1499     ///     (false, _   , false, false, _   , ...) => false,
1500     ///     ...
1501     ///     _ => true
1502     /// }
1503     /// ```
1504     ///
1505     /// Here the last arm is reachable only if there is an assignment to
1506     /// the variables that does not match any of the literals. Therefore,
1507     /// compilation would take an exponential amount of time in some cases.
1508     ///
1509     /// That kind of exponential worst-case might not occur in practice, but
1510     /// our simplistic treatment of constants and guards would make it occur
1511     /// in very common situations - for example [#29740]:
1512     ///
1513     /// ```ignore (illustrative)
1514     /// match x {
1515     ///     "foo" if foo_guard => ...,
1516     ///     "bar" if bar_guard => ...,
1517     ///     "baz" if baz_guard => ...,
1518     ///     ...
1519     /// }
1520     /// ```
1521     ///
1522     /// [#29740]: https://github.com/rust-lang/rust/issues/29740
1523     ///
1524     /// Here we first test the match-pair `x @ "foo"`, which is an [`Eq` test].
1525     ///
1526     /// [`Eq` test]: TestKind::Eq
1527     ///
1528     /// It might seem that we would end up with 2 disjoint candidate
1529     /// sets, consisting of the first candidate or the other two, but our
1530     /// algorithm doesn't reason about `"foo"` being distinct from the other
1531     /// constants; it considers the latter arms to potentially match after
1532     /// both outcomes, which obviously leads to an exponential number
1533     /// of tests.
1534     ///
1535     /// To avoid these kinds of problems, our algorithm tries to ensure
1536     /// the amount of generated tests is linear. When we do a k-way test,
1537     /// we return an additional "unmatched" set alongside the obvious `k`
1538     /// sets. When we encounter a candidate that would be present in more
1539     /// than one of the sets, we put it and all candidates below it into the
1540     /// "unmatched" set. This ensures these `k+1` sets are disjoint.
1541     ///
1542     /// After we perform our test, we branch into the appropriate candidate
1543     /// set and recurse with `match_candidates`. These sub-matches are
1544     /// obviously non-exhaustive - as we discarded our otherwise set - so
1545     /// we set their continuation to do `match_candidates` on the
1546     /// "unmatched" set (which is again non-exhaustive).
1547     ///
1548     /// If you apply this to the above test, you basically wind up
1549     /// with an if-else-if chain, testing each candidate in turn,
1550     /// which is precisely what we want.
1551     ///
1552     /// In addition to avoiding exponential-time blowups, this algorithm
1553     /// also has the nice property that each guard and arm is only generated
1554     /// once.
1555     fn test_candidates<'pat, 'b, 'c>(
1556         &mut self,
1557         span: Span,
1558         scrutinee_span: Span,
1559         mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
1560         block: BasicBlock,
1561         otherwise_block: &mut Option<BasicBlock>,
1562         fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1563     ) {
1564         // extract the match-pair from the highest priority candidate
1565         let match_pair = &candidates.first().unwrap().match_pairs[0];
1566         let mut test = self.test(match_pair);
1567         let match_place = match_pair.place.clone();
1568
1569         // most of the time, the test to perform is simply a function
1570         // of the main candidate; but for a test like SwitchInt, we
1571         // may want to add cases based on the candidates that are
1572         // available
1573         match test.kind {
1574             TestKind::SwitchInt { switch_ty, ref mut options } => {
1575                 for candidate in candidates.iter() {
1576                     if !self.add_cases_to_switch(&match_place, candidate, switch_ty, options) {
1577                         break;
1578                     }
1579                 }
1580             }
1581             TestKind::Switch { adt_def: _, ref mut variants } => {
1582                 for candidate in candidates.iter() {
1583                     if !self.add_variants_to_switch(&match_place, candidate, variants) {
1584                         break;
1585                     }
1586                 }
1587             }
1588             _ => {}
1589         }
1590
1591         // Insert a Shallow borrow of any places that is switched on.
1592         if let Some(fb) = fake_borrows
1593             && let Some(resolved_place) = match_place.try_to_place(self)
1594         {
1595             fb.insert(resolved_place);
1596         }
1597
1598         // perform the test, branching to one of N blocks. For each of
1599         // those N possible outcomes, create a (initially empty)
1600         // vector of candidates. Those are the candidates that still
1601         // apply if the test has that particular outcome.
1602         debug!("test_candidates: test={:?} match_pair={:?}", test, match_pair);
1603         let mut target_candidates: Vec<Vec<&mut Candidate<'pat, 'tcx>>> = vec![];
1604         target_candidates.resize_with(test.targets(), Default::default);
1605
1606         let total_candidate_count = candidates.len();
1607
1608         // Sort the candidates into the appropriate vector in
1609         // `target_candidates`. Note that at some point we may
1610         // encounter a candidate where the test is not relevant; at
1611         // that point, we stop sorting.
1612         while let Some(candidate) = candidates.first_mut() {
1613             let Some(idx) = self.sort_candidate(&match_place, &test, candidate) else {
1614                 break;
1615             };
1616             let (candidate, rest) = candidates.split_first_mut().unwrap();
1617             target_candidates[idx].push(candidate);
1618             candidates = rest;
1619         }
1620         // at least the first candidate ought to be tested
1621         assert!(
1622             total_candidate_count > candidates.len(),
1623             "{}, {:#?}",
1624             total_candidate_count,
1625             candidates
1626         );
1627         debug!("tested_candidates: {}", total_candidate_count - candidates.len());
1628         debug!("untested_candidates: {}", candidates.len());
1629
1630         // HACK(matthewjasper) This is a closure so that we can let the test
1631         // create its blocks before the rest of the match. This currently
1632         // improves the speed of llvm when optimizing long string literal
1633         // matches
1634         let make_target_blocks = move |this: &mut Self| -> Vec<BasicBlock> {
1635             // The block that we should branch to if none of the
1636             // `target_candidates` match. This is either the block where we
1637             // start matching the untested candidates if there are any,
1638             // otherwise it's the `otherwise_block`.
1639             let remainder_start = &mut None;
1640             let remainder_start =
1641                 if candidates.is_empty() { &mut *otherwise_block } else { remainder_start };
1642
1643             // For each outcome of test, process the candidates that still
1644             // apply. Collect a list of blocks where control flow will
1645             // branch if one of the `target_candidate` sets is not
1646             // exhaustive.
1647             let target_blocks: Vec<_> = target_candidates
1648                 .into_iter()
1649                 .map(|mut candidates| {
1650                     if !candidates.is_empty() {
1651                         let candidate_start = this.cfg.start_new_block();
1652                         this.match_candidates(
1653                             span,
1654                             scrutinee_span,
1655                             candidate_start,
1656                             remainder_start,
1657                             &mut *candidates,
1658                             fake_borrows,
1659                         );
1660                         candidate_start
1661                     } else {
1662                         *remainder_start.get_or_insert_with(|| this.cfg.start_new_block())
1663                     }
1664                 })
1665                 .collect();
1666
1667             if !candidates.is_empty() {
1668                 let remainder_start = remainder_start.unwrap_or_else(|| this.cfg.start_new_block());
1669                 this.match_candidates(
1670                     span,
1671                     scrutinee_span,
1672                     remainder_start,
1673                     otherwise_block,
1674                     candidates,
1675                     fake_borrows,
1676                 );
1677             };
1678
1679             target_blocks
1680         };
1681
1682         self.perform_test(span, scrutinee_span, block, &match_place, &test, make_target_blocks);
1683     }
1684
1685     /// Determine the fake borrows that are needed from a set of places that
1686     /// have to be stable across match guards.
1687     ///
1688     /// Returns a list of places that need a fake borrow and the temporary
1689     /// that's used to store the fake borrow.
1690     ///
1691     /// Match exhaustiveness checking is not able to handle the case where the
1692     /// place being matched on is mutated in the guards. We add "fake borrows"
1693     /// to the guards that prevent any mutation of the place being matched.
1694     /// There are a some subtleties:
1695     ///
1696     /// 1. Borrowing `*x` doesn't prevent assigning to `x`. If `x` is a shared
1697     ///    reference, the borrow isn't even tracked. As such we have to add fake
1698     ///    borrows of any prefixes of a place
1699     /// 2. We don't want `match x { _ => (), }` to conflict with mutable
1700     ///    borrows of `x`, so we only add fake borrows for places which are
1701     ///    bound or tested by the match.
1702     /// 3. We don't want the fake borrows to conflict with `ref mut` bindings,
1703     ///    so we use a special BorrowKind for them.
1704     /// 4. The fake borrows may be of places in inactive variants, so it would
1705     ///    be UB to generate code for them. They therefore have to be removed
1706     ///    by a MIR pass run after borrow checking.
1707     fn calculate_fake_borrows<'b>(
1708         &mut self,
1709         fake_borrows: &'b FxIndexSet<Place<'tcx>>,
1710         temp_span: Span,
1711     ) -> Vec<(Place<'tcx>, Local)> {
1712         let tcx = self.tcx;
1713
1714         debug!("add_fake_borrows fake_borrows = {:?}", fake_borrows);
1715
1716         let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len());
1717
1718         // Insert a Shallow borrow of the prefixes of any fake borrows.
1719         for place in fake_borrows {
1720             let mut cursor = place.projection.as_ref();
1721             while let [proj_base @ .., elem] = cursor {
1722                 cursor = proj_base;
1723
1724                 if let ProjectionElem::Deref = elem {
1725                     // Insert a shallow borrow after a deref. For other
1726                     // projections the borrow of prefix_cursor will
1727                     // conflict with any mutation of base.
1728                     all_fake_borrows.push(PlaceRef { local: place.local, projection: proj_base });
1729                 }
1730             }
1731
1732             all_fake_borrows.push(place.as_ref());
1733         }
1734
1735         // Deduplicate
1736         let mut dedup = FxHashSet::default();
1737         all_fake_borrows.retain(|b| dedup.insert(*b));
1738
1739         debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);
1740
1741         all_fake_borrows
1742             .into_iter()
1743             .map(|matched_place_ref| {
1744                 let matched_place = Place {
1745                     local: matched_place_ref.local,
1746                     projection: tcx.intern_place_elems(matched_place_ref.projection),
1747                 };
1748                 let fake_borrow_deref_ty = matched_place.ty(&self.local_decls, tcx).ty;
1749                 let fake_borrow_ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
1750                 let fake_borrow_temp =
1751                     self.local_decls.push(LocalDecl::new(fake_borrow_ty, temp_span));
1752
1753                 (matched_place, fake_borrow_temp)
1754             })
1755             .collect()
1756     }
1757 }
1758
1759 ///////////////////////////////////////////////////////////////////////////
1760 // Pat binding - used for `let` and function parameters as well.
1761
1762 impl<'a, 'tcx> Builder<'a, 'tcx> {
1763     /// If the bindings have already been declared, set `declare_bindings` to
1764     /// `false` to avoid duplicated bindings declaration. Used for if-let guards.
1765     pub(crate) fn lower_let_expr(
1766         &mut self,
1767         mut block: BasicBlock,
1768         expr: &Expr<'tcx>,
1769         pat: &Pat<'tcx>,
1770         else_target: region::Scope,
1771         source_scope: Option<SourceScope>,
1772         span: Span,
1773         declare_bindings: bool,
1774     ) -> BlockAnd<()> {
1775         let expr_span = expr.span;
1776         let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
1777         let wildcard = Pat::wildcard_from_ty(pat.ty);
1778         let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false, self);
1779         let mut otherwise_candidate =
1780             Candidate::new(expr_place_builder.clone(), &wildcard, false, self);
1781         let fake_borrow_temps = self.lower_match_tree(
1782             block,
1783             pat.span,
1784             pat.span,
1785             false,
1786             &mut [&mut guard_candidate, &mut otherwise_candidate],
1787         );
1788         let expr_place = expr_place_builder.try_to_place(self);
1789         let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
1790         let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
1791         self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span));
1792
1793         if declare_bindings {
1794             self.declare_bindings(source_scope, pat.span.to(span), pat, None, opt_expr_place);
1795         }
1796
1797         let post_guard_block = self.bind_pattern(
1798             self.source_info(pat.span),
1799             guard_candidate,
1800             &fake_borrow_temps,
1801             expr.span,
1802             None,
1803             false,
1804         );
1805
1806         post_guard_block.unit()
1807     }
1808
1809     /// Initializes each of the bindings from the candidate by
1810     /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
1811     /// any, and then branches to the arm. Returns the block for the case where
1812     /// the guard succeeds.
1813     ///
1814     /// Note: we do not check earlier that if there is a guard,
1815     /// there cannot be move bindings. We avoid a use-after-move by only
1816     /// moving the binding once the guard has evaluated to true (see below).
1817     fn bind_and_guard_matched_candidate<'pat>(
1818         &mut self,
1819         candidate: Candidate<'pat, 'tcx>,
1820         parent_bindings: &[(Vec<Binding<'tcx>>, Vec<Ascription<'tcx>>)],
1821         fake_borrows: &[(Place<'tcx>, Local)],
1822         scrutinee_span: Span,
1823         arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
1824         schedule_drops: bool,
1825         storages_alive: bool,
1826     ) -> BasicBlock {
1827         debug!("bind_and_guard_matched_candidate(candidate={:?})", candidate);
1828
1829         debug_assert!(candidate.match_pairs.is_empty());
1830
1831         let candidate_source_info = self.source_info(candidate.span);
1832
1833         let mut block = candidate.pre_binding_block.unwrap();
1834
1835         if candidate.next_candidate_pre_binding_block.is_some() {
1836             let fresh_block = self.cfg.start_new_block();
1837             self.false_edges(
1838                 block,
1839                 fresh_block,
1840                 candidate.next_candidate_pre_binding_block,
1841                 candidate_source_info,
1842             );
1843             block = fresh_block;
1844         }
1845
1846         self.ascribe_types(
1847             block,
1848             parent_bindings
1849                 .iter()
1850                 .flat_map(|(_, ascriptions)| ascriptions)
1851                 .cloned()
1852                 .chain(candidate.ascriptions),
1853         );
1854
1855         // rust-lang/rust#27282: The `autoref` business deserves some
1856         // explanation here.
1857         //
1858         // The intent of the `autoref` flag is that when it is true,
1859         // then any pattern bindings of type T will map to a `&T`
1860         // within the context of the guard expression, but will
1861         // continue to map to a `T` in the context of the arm body. To
1862         // avoid surfacing this distinction in the user source code
1863         // (which would be a severe change to the language and require
1864         // far more revision to the compiler), when `autoref` is true,
1865         // then any occurrence of the identifier in the guard
1866         // expression will automatically get a deref op applied to it.
1867         //
1868         // So an input like:
1869         //
1870         // ```
1871         // let place = Foo::new();
1872         // match place { foo if inspect(foo)
1873         //     => feed(foo), ...  }
1874         // ```
1875         //
1876         // will be treated as if it were really something like:
1877         //
1878         // ```
1879         // let place = Foo::new();
1880         // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
1881         //     => { let tmp2 = place; feed(tmp2) }, ... }
1882         //
1883         // And an input like:
1884         //
1885         // ```
1886         // let place = Foo::new();
1887         // match place { ref mut foo if inspect(foo)
1888         //     => feed(foo), ...  }
1889         // ```
1890         //
1891         // will be treated as if it were really something like:
1892         //
1893         // ```
1894         // let place = Foo::new();
1895         // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
1896         //     => { let tmp2 = &mut place; feed(tmp2) }, ... }
1897         // ```
1898         //
1899         // In short, any pattern binding will always look like *some*
1900         // kind of `&T` within the guard at least in terms of how the
1901         // MIR-borrowck views it, and this will ensure that guard
1902         // expressions cannot mutate their the match inputs via such
1903         // bindings. (It also ensures that guard expressions can at
1904         // most *copy* values from such bindings; non-Copy things
1905         // cannot be moved via pattern bindings in guard expressions.)
1906         //
1907         // ----
1908         //
1909         // Implementation notes (under assumption `autoref` is true).
1910         //
1911         // To encode the distinction above, we must inject the
1912         // temporaries `tmp1` and `tmp2`.
1913         //
1914         // There are two cases of interest: binding by-value, and binding by-ref.
1915         //
1916         // 1. Binding by-value: Things are simple.
1917         //
1918         //    * Establishing `tmp1` creates a reference into the
1919         //      matched place. This code is emitted by
1920         //      bind_matched_candidate_for_guard.
1921         //
1922         //    * `tmp2` is only initialized "lazily", after we have
1923         //      checked the guard. Thus, the code that can trigger
1924         //      moves out of the candidate can only fire after the
1925         //      guard evaluated to true. This initialization code is
1926         //      emitted by bind_matched_candidate_for_arm.
1927         //
1928         // 2. Binding by-reference: Things are tricky.
1929         //
1930         //    * Here, the guard expression wants a `&&` or `&&mut`
1931         //      into the original input. This means we need to borrow
1932         //      the reference that we create for the arm.
1933         //    * So we eagerly create the reference for the arm and then take a
1934         //      reference to that.
1935         if let Some((arm, match_scope)) = arm_match_scope
1936             && let Some(guard) = &arm.guard
1937         {
1938             let tcx = self.tcx;
1939             let bindings = parent_bindings
1940                 .iter()
1941                 .flat_map(|(bindings, _)| bindings)
1942                 .chain(&candidate.bindings);
1943
1944             self.bind_matched_candidate_for_guard(block, schedule_drops, bindings.clone());
1945             let guard_frame = GuardFrame {
1946                 locals: bindings.map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode)).collect(),
1947             };
1948             debug!("entering guard building context: {:?}", guard_frame);
1949             self.guard_context.push(guard_frame);
1950
1951             let re_erased = tcx.lifetimes.re_erased;
1952             let scrutinee_source_info = self.source_info(scrutinee_span);
1953             for &(place, temp) in fake_borrows {
1954                 let borrow = Rvalue::Ref(re_erased, BorrowKind::Shallow, place);
1955                 self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
1956             }
1957
1958             let mut guard_span = rustc_span::DUMMY_SP;
1959
1960             let (post_guard_block, otherwise_post_guard_block) =
1961                 self.in_if_then_scope(match_scope, guard_span, |this| match *guard {
1962                     Guard::If(e) => {
1963                         let e = &this.thir[e];
1964                         guard_span = e.span;
1965                         this.then_else_break(
1966                             block,
1967                             e,
1968                             None,
1969                             match_scope,
1970                             this.source_info(arm.span),
1971                         )
1972                     }
1973                     Guard::IfLet(ref pat, scrutinee) => {
1974                         let s = &this.thir[scrutinee];
1975                         guard_span = s.span;
1976                         this.lower_let_expr(block, s, pat, match_scope, None, arm.span, false)
1977                     }
1978                 });
1979
1980             let source_info = self.source_info(guard_span);
1981             let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
1982             let guard_frame = self.guard_context.pop().unwrap();
1983             debug!("Exiting guard building context with locals: {:?}", guard_frame);
1984
1985             for &(_, temp) in fake_borrows {
1986                 let cause = FakeReadCause::ForMatchGuard;
1987                 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
1988             }
1989
1990             let otherwise_block = candidate.otherwise_block.unwrap_or_else(|| {
1991                 let unreachable = self.cfg.start_new_block();
1992                 self.cfg.terminate(unreachable, source_info, TerminatorKind::Unreachable);
1993                 unreachable
1994             });
1995             self.false_edges(
1996                 otherwise_post_guard_block,
1997                 otherwise_block,
1998                 candidate.next_candidate_pre_binding_block,
1999                 source_info,
2000             );
2001
2002             // We want to ensure that the matched candidates are bound
2003             // after we have confirmed this candidate *and* any
2004             // associated guard; Binding them on `block` is too soon,
2005             // because that would be before we've checked the result
2006             // from the guard.
2007             //
2008             // But binding them on the arm is *too late*, because
2009             // then all of the candidates for a single arm would be
2010             // bound in the same place, that would cause a case like:
2011             //
2012             // ```rust
2013             // match (30, 2) {
2014             //     (mut x, 1) | (2, mut x) if { true } => { ... }
2015             //     ...                                 // ^^^^^^^ (this is `arm_block`)
2016             // }
2017             // ```
2018             //
2019             // would yield an `arm_block` something like:
2020             //
2021             // ```
2022             // StorageLive(_4);        // _4 is `x`
2023             // _4 = &mut (_1.0: i32);  // this is handling `(mut x, 1)` case
2024             // _4 = &mut (_1.1: i32);  // this is handling `(2, mut x)` case
2025             // ```
2026             //
2027             // and that is clearly not correct.
2028             let by_value_bindings = parent_bindings
2029                 .iter()
2030                 .flat_map(|(bindings, _)| bindings)
2031                 .chain(&candidate.bindings)
2032                 .filter(|binding| matches!(binding.binding_mode, BindingMode::ByValue));
2033             // Read all of the by reference bindings to ensure that the
2034             // place they refer to can't be modified by the guard.
2035             for binding in by_value_bindings.clone() {
2036                 let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
2037                 let cause = FakeReadCause::ForGuardBinding;
2038                 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
2039             }
2040             assert!(schedule_drops, "patterns with guards must schedule drops");
2041             self.bind_matched_candidate_for_arm_body(
2042                 post_guard_block,
2043                 true,
2044                 by_value_bindings,
2045                 storages_alive,
2046             );
2047
2048             post_guard_block
2049         } else {
2050             // (Here, it is not too early to bind the matched
2051             // candidate on `block`, because there is no guard result
2052             // that we have to inspect before we bind them.)
2053             self.bind_matched_candidate_for_arm_body(
2054                 block,
2055                 schedule_drops,
2056                 parent_bindings
2057                     .iter()
2058                     .flat_map(|(bindings, _)| bindings)
2059                     .chain(&candidate.bindings),
2060                 storages_alive,
2061             );
2062             block
2063         }
2064     }
2065
2066     /// Append `AscribeUserType` statements onto the end of `block`
2067     /// for each ascription
2068     fn ascribe_types(
2069         &mut self,
2070         block: BasicBlock,
2071         ascriptions: impl IntoIterator<Item = Ascription<'tcx>>,
2072     ) {
2073         for ascription in ascriptions {
2074             let source_info = self.source_info(ascription.annotation.span);
2075
2076             let base = self.canonical_user_type_annotations.push(ascription.annotation);
2077             self.cfg.push(
2078                 block,
2079                 Statement {
2080                     source_info,
2081                     kind: StatementKind::AscribeUserType(
2082                         Box::new((
2083                             ascription.source,
2084                             UserTypeProjection { base, projs: Vec::new() },
2085                         )),
2086                         ascription.variance,
2087                     ),
2088                 },
2089             );
2090         }
2091     }
2092
2093     fn bind_matched_candidate_for_guard<'b>(
2094         &mut self,
2095         block: BasicBlock,
2096         schedule_drops: bool,
2097         bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2098     ) where
2099         'tcx: 'b,
2100     {
2101         debug!("bind_matched_candidate_for_guard(block={:?})", block);
2102
2103         // Assign each of the bindings. Since we are binding for a
2104         // guard expression, this will never trigger moves out of the
2105         // candidate.
2106         let re_erased = self.tcx.lifetimes.re_erased;
2107         for binding in bindings {
2108             debug!("bind_matched_candidate_for_guard(binding={:?})", binding);
2109             let source_info = self.source_info(binding.span);
2110
2111             // For each pattern ident P of type T, `ref_for_guard` is
2112             // a reference R: &T pointing to the location matched by
2113             // the pattern, and every occurrence of P within a guard
2114             // denotes *R.
2115             let ref_for_guard = self.storage_live_binding(
2116                 block,
2117                 binding.var_id,
2118                 binding.span,
2119                 RefWithinGuard,
2120                 schedule_drops,
2121             );
2122             match binding.binding_mode {
2123                 BindingMode::ByValue => {
2124                     let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
2125                     self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2126                 }
2127                 BindingMode::ByRef(borrow_kind) => {
2128                     let value_for_arm = self.storage_live_binding(
2129                         block,
2130                         binding.var_id,
2131                         binding.span,
2132                         OutsideGuard,
2133                         schedule_drops,
2134                     );
2135
2136                     let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source);
2137                     self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
2138                     let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
2139                     self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2140                 }
2141             }
2142         }
2143     }
2144
2145     fn bind_matched_candidate_for_arm_body<'b>(
2146         &mut self,
2147         block: BasicBlock,
2148         schedule_drops: bool,
2149         bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2150         storages_alive: bool,
2151     ) where
2152         'tcx: 'b,
2153     {
2154         debug!("bind_matched_candidate_for_arm_body(block={:?})", block);
2155
2156         let re_erased = self.tcx.lifetimes.re_erased;
2157         // Assign each of the bindings. This may trigger moves out of the candidate.
2158         for binding in bindings {
2159             let source_info = self.source_info(binding.span);
2160             let local = if storages_alive {
2161                 // Here storages are already alive, probably because this is a binding
2162                 // from let-else.
2163                 // We just need to schedule drop for the value.
2164                 self.var_local_id(binding.var_id, OutsideGuard).into()
2165             } else {
2166                 self.storage_live_binding(
2167                     block,
2168                     binding.var_id,
2169                     binding.span,
2170                     OutsideGuard,
2171                     schedule_drops,
2172                 )
2173             };
2174             if schedule_drops {
2175                 self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2176             }
2177             let rvalue = match binding.binding_mode {
2178                 BindingMode::ByValue => Rvalue::Use(self.consume_by_copy_or_move(binding.source)),
2179                 BindingMode::ByRef(borrow_kind) => {
2180                     Rvalue::Ref(re_erased, borrow_kind, binding.source)
2181                 }
2182             };
2183             self.cfg.push_assign(block, source_info, local, rvalue);
2184         }
2185     }
2186
2187     /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2188     /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2189     /// first local is a binding for occurrences of `var` in the guard, which
2190     /// will have type `&T`. The second local is a binding for occurrences of
2191     /// `var` in the arm body, which will have type `T`.
2192     #[instrument(skip(self), level = "debug")]
2193     fn declare_binding(
2194         &mut self,
2195         source_info: SourceInfo,
2196         visibility_scope: SourceScope,
2197         mutability: Mutability,
2198         name: Symbol,
2199         mode: BindingMode,
2200         var_id: LocalVarId,
2201         var_ty: Ty<'tcx>,
2202         user_ty: UserTypeProjections,
2203         has_guard: ArmHasGuard,
2204         opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2205         pat_span: Span,
2206     ) {
2207         let tcx = self.tcx;
2208         let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
2209         let binding_mode = match mode {
2210             BindingMode::ByValue => ty::BindingMode::BindByValue(mutability),
2211             BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability),
2212         };
2213         let local = LocalDecl::<'tcx> {
2214             mutability,
2215             ty: var_ty,
2216             user_ty: if user_ty.is_empty() { None } else { Some(Box::new(user_ty)) },
2217             source_info,
2218             internal: false,
2219             is_block_tail: None,
2220             local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
2221                 VarBindingForm {
2222                     binding_mode,
2223                     // hypothetically, `visit_primary_bindings` could try to unzip
2224                     // an outermost hir::Ty as we descend, matching up
2225                     // idents in pat; but complex w/ unclear UI payoff.
2226                     // Instead, just abandon providing diagnostic info.
2227                     opt_ty_info: None,
2228                     opt_match_place,
2229                     pat_span,
2230                 },
2231             ))))),
2232         };
2233         let for_arm_body = self.local_decls.push(local);
2234         self.var_debug_info.push(VarDebugInfo {
2235             name,
2236             source_info: debug_source_info,
2237             value: VarDebugInfoContents::Place(for_arm_body.into()),
2238         });
2239         let locals = if has_guard.0 {
2240             let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
2241                 // This variable isn't mutated but has a name, so has to be
2242                 // immutable to avoid the unused mut lint.
2243                 mutability: Mutability::Not,
2244                 ty: tcx.mk_imm_ref(tcx.lifetimes.re_erased, var_ty),
2245                 user_ty: None,
2246                 source_info,
2247                 internal: false,
2248                 is_block_tail: None,
2249                 local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
2250                     BindingForm::RefForGuard,
2251                 )))),
2252             });
2253             self.var_debug_info.push(VarDebugInfo {
2254                 name,
2255                 source_info: debug_source_info,
2256                 value: VarDebugInfoContents::Place(ref_for_guard.into()),
2257             });
2258             LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
2259         } else {
2260             LocalsForNode::One(for_arm_body)
2261         };
2262         debug!(?locals);
2263         self.var_indices.insert(var_id, locals);
2264     }
2265
2266     pub(crate) fn ast_let_else(
2267         &mut self,
2268         mut block: BasicBlock,
2269         init: &Expr<'tcx>,
2270         initializer_span: Span,
2271         else_block: BlockId,
2272         let_else_scope: &region::Scope,
2273         pattern: &Pat<'tcx>,
2274     ) -> BlockAnd<BasicBlock> {
2275         let else_block_span = self.thir[else_block].span;
2276         let (matching, failure) = self.in_if_then_scope(*let_else_scope, else_block_span, |this| {
2277             let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
2278             let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
2279             let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false, this);
2280             let mut candidate = Candidate::new(scrutinee.clone(), pattern, false, this);
2281             let fake_borrow_temps = this.lower_match_tree(
2282                 block,
2283                 initializer_span,
2284                 pattern.span,
2285                 false,
2286                 &mut [&mut candidate, &mut wildcard],
2287             );
2288             // This block is for the matching case
2289             let matching = this.bind_pattern(
2290                 this.source_info(pattern.span),
2291                 candidate,
2292                 &fake_borrow_temps,
2293                 initializer_span,
2294                 None,
2295                 true,
2296             );
2297             // This block is for the failure case
2298             let failure = this.bind_pattern(
2299                 this.source_info(else_block_span),
2300                 wildcard,
2301                 &fake_borrow_temps,
2302                 initializer_span,
2303                 None,
2304                 true,
2305             );
2306             this.break_for_else(failure, *let_else_scope, this.source_info(initializer_span));
2307             matching.unit()
2308         });
2309         matching.and(failure)
2310     }
2311 }