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