]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/matches/mod.rs
Refactoring: added `PatternTypeAnnotation` wrapper around `UserTypeAnnotation` to...
[rust.git] / src / librustc_mir / build / matches / mod.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Code related to match expressions. These are sufficiently complex
12 //! to warrant their own module and submodules. :) This main module
13 //! includes the high-level algorithm, the submodules contain the
14 //! details.
15
16 use build::scope::{CachedBlock, DropKind};
17 use build::ForGuard::{self, OutsideGuard, RefWithinGuard, ValWithinGuard};
18 use build::{BlockAnd, BlockAndExtension, Builder};
19 use build::{GuardFrame, GuardFrameLocal, LocalsForNode};
20 use hair::*;
21 use rustc::hir;
22 use rustc::mir::*;
23 use rustc::ty::{self, Ty};
24 use rustc_data_structures::bit_set::BitSet;
25 use rustc_data_structures::fx::FxHashMap;
26 use syntax::ast::{Name, NodeId};
27 use syntax_pos::Span;
28
29 // helper functions, broken out by category:
30 mod simplify;
31 mod test;
32 mod util;
33
34 /// ArmHasGuard is isomorphic to a boolean flag. It indicates whether
35 /// a match arm has a guard expression attached to it.
36 #[derive(Copy, Clone, Debug)]
37 pub(crate) struct ArmHasGuard(pub bool);
38
39 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
40     pub fn match_expr(
41         &mut self,
42         destination: &Place<'tcx>,
43         span: Span,
44         mut block: BasicBlock,
45         discriminant: ExprRef<'tcx>,
46         arms: Vec<Arm<'tcx>>,
47     ) -> BlockAnd<()> {
48         let tcx = self.hir.tcx();
49         let discriminant_span = discriminant.span();
50         let discriminant_place = unpack!(block = self.as_place(block, discriminant));
51
52         // Matching on a `discriminant_place` with an uninhabited type doesn't
53         // generate any memory reads by itself, and so if the place "expression"
54         // contains unsafe operations like raw pointer dereferences or union
55         // field projections, we wouldn't know to require an `unsafe` block
56         // around a `match` equivalent to `std::intrinsics::unreachable()`.
57         // See issue #47412 for this hole being discovered in the wild.
58         //
59         // HACK(eddyb) Work around the above issue by adding a dummy inspection
60         // of `discriminant_place`, specifically by applying `ReadForMatch`.
61         //
62         // NOTE: ReadForMatch also checks that the discriminant is initialized.
63         // This is currently needed to not allow matching on an uninitialized,
64         // uninhabited value. If we get never patterns, those will check that
65         // the place is initialized, and so this read would only be used to
66         // check safety.
67
68         let source_info = self.source_info(discriminant_span);
69         self.cfg.push(block, Statement {
70             source_info,
71             kind: StatementKind::FakeRead(
72                 FakeReadCause::ForMatchedPlace,
73                 discriminant_place.clone(),
74             ),
75         });
76
77         let mut arm_blocks = ArmBlocks {
78             blocks: arms.iter().map(|_| self.cfg.start_new_block()).collect(),
79         };
80
81         // Get the arm bodies and their scopes, while declaring bindings.
82         let arm_bodies: Vec<_> = arms.iter()
83             .map(|arm| {
84                 // BUG: use arm lint level
85                 let body = self.hir.mirror(arm.body.clone());
86                 let scope = self.declare_bindings(
87                     None,
88                     body.span,
89                     LintLevel::Inherited,
90                     &arm.patterns[..],
91                     ArmHasGuard(arm.guard.is_some()),
92                     Some((Some(&discriminant_place), discriminant_span)),
93                 );
94                 (body, scope.unwrap_or(self.source_scope))
95             })
96             .collect();
97
98         // create binding start block for link them by false edges
99         let candidate_count = arms.iter().fold(0, |ac, c| ac + c.patterns.len());
100         let pre_binding_blocks: Vec<_> = (0..candidate_count + 1)
101             .map(|_| self.cfg.start_new_block())
102             .collect();
103
104         let mut has_guard = false;
105
106         // assemble a list of candidates: there is one candidate per
107         // pattern, which means there may be more than one candidate
108         // *per arm*. These candidates are kept sorted such that the
109         // highest priority candidate comes first in the list.
110         // (i.e. same order as in source)
111
112         let candidates: Vec<_> = arms.iter()
113             .enumerate()
114             .flat_map(|(arm_index, arm)| {
115                 arm.patterns
116                     .iter()
117                     .enumerate()
118                     .map(move |(pat_index, pat)| (arm_index, pat_index, pat, arm.guard.clone()))
119             })
120             .zip(
121                 pre_binding_blocks
122                     .iter()
123                     .zip(pre_binding_blocks.iter().skip(1)),
124             )
125             .map(
126                 |(
127                     (arm_index, pat_index, pattern, guard),
128                     (pre_binding_block, next_candidate_pre_binding_block)
129                 )| {
130                     has_guard |= guard.is_some();
131
132                     // One might ask: why not build up the match pair such that it
133                     // matches via `borrowed_input_temp.deref()` instead of
134                     // using the `discriminant_place` directly, as it is doing here?
135                     //
136                     // The basic answer is that if you do that, then you end up with
137                     // accceses to a shared borrow of the input and that conflicts with
138                     // any arms that look like e.g.
139                     //
140                     // match Some(&4) {
141                     //     ref mut foo => {
142                     //         ... /* mutate `foo` in arm body */ ...
143                     //     }
144                     // }
145                     //
146                     // (Perhaps we could further revise the MIR
147                     //  construction here so that it only does a
148                     //  shared borrow at the outset and delays doing
149                     //  the mutable borrow until after the pattern is
150                     //  matched *and* the guard (if any) for the arm
151                     //  has been run.)
152
153                     Candidate {
154                         span: pattern.span,
155                         match_pairs: vec![MatchPair::new(discriminant_place.clone(), pattern)],
156                         bindings: vec![],
157                         ascriptions: vec![],
158                         guard,
159                         arm_index,
160                         pat_index,
161                         pre_binding_block: *pre_binding_block,
162                         next_candidate_pre_binding_block: *next_candidate_pre_binding_block,
163                     }
164                 },
165             )
166             .collect();
167
168         let outer_source_info = self.source_info(span);
169         self.cfg.terminate(
170             *pre_binding_blocks.last().unwrap(),
171             outer_source_info,
172             TerminatorKind::Unreachable,
173         );
174
175         // Maps a place to the kind of Fake borrow that we want to perform on
176         // it: either Shallow or Shared, depending on whether the place is
177         // bound in the match, or just switched on.
178         // If there are no match guards then we don't need any fake borrows,
179         // so don't track them.
180         let mut fake_borrows = if has_guard && tcx.generate_borrow_of_any_match_input() {
181             Some(FxHashMap::default())
182         } else {
183             None
184         };
185
186         let pre_binding_blocks: Vec<_> = candidates
187             .iter()
188             .map(|cand| (cand.pre_binding_block, cand.span))
189             .collect();
190
191         // this will generate code to test discriminant_place and
192         // branch to the appropriate arm block
193         let otherwise = self.match_candidates(
194             discriminant_span,
195             &mut arm_blocks,
196             candidates,
197             block,
198             &mut fake_borrows,
199         );
200
201         if !otherwise.is_empty() {
202             // All matches are exhaustive. However, because some matches
203             // only have exponentially-large exhaustive decision trees, we
204             // sometimes generate an inexhaustive decision tree.
205             //
206             // In that case, the inexhaustive tips of the decision tree
207             // can't be reached - terminate them with an `unreachable`.
208             let source_info = self.source_info(span);
209
210             let mut otherwise = otherwise;
211             otherwise.sort();
212             otherwise.dedup(); // variant switches can introduce duplicate target blocks
213             for block in otherwise {
214                 self.cfg
215                     .terminate(block, source_info, TerminatorKind::Unreachable);
216             }
217         }
218
219         if let Some(fake_borrows) = fake_borrows {
220             self.add_fake_borrows(&pre_binding_blocks, fake_borrows, source_info, block);
221         }
222
223         // all the arm blocks will rejoin here
224         let end_block = self.cfg.start_new_block();
225
226         let outer_source_info = self.source_info(span);
227         for (arm_index, (body, source_scope)) in arm_bodies.into_iter().enumerate() {
228             let mut arm_block = arm_blocks.blocks[arm_index];
229             // Re-enter the source scope we created the bindings in.
230             self.source_scope = source_scope;
231             unpack!(arm_block = self.into(destination, arm_block, body));
232             self.cfg.terminate(
233                 arm_block,
234                 outer_source_info,
235                 TerminatorKind::Goto { target: end_block },
236             );
237         }
238         self.source_scope = outer_source_info.scope;
239
240         end_block.unit()
241     }
242
243     pub(super) fn expr_into_pattern(
244         &mut self,
245         mut block: BasicBlock,
246         irrefutable_pat: Pattern<'tcx>,
247         initializer: ExprRef<'tcx>,
248     ) -> BlockAnd<()> {
249         match *irrefutable_pat.kind {
250             // Optimize the case of `let x = ...` to write directly into `x`
251             PatternKind::Binding {
252                 mode: BindingMode::ByValue,
253                 var,
254                 subpattern: None,
255                 ..
256             } => {
257                 let place =
258                     self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
259                 unpack!(block = self.into(&place, block, initializer));
260
261
262                 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
263                 let source_info = self.source_info(irrefutable_pat.span);
264                 self.cfg.push(
265                     block,
266                     Statement {
267                         source_info,
268                         kind: StatementKind::FakeRead(FakeReadCause::ForLet, place),
269                     },
270                 );
271
272                 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
273                 block.unit()
274             }
275
276             // Optimize the case of `let x: T = ...` to write directly
277             // into `x` and then require that `T == typeof(x)`.
278             //
279             // Weirdly, this is needed to prevent the
280             // `intrinsic-move-val.rs` test case from crashing. That
281             // test works with uninitialized values in a rather
282             // dubious way, so it may be that the test is kind of
283             // broken.
284             PatternKind::AscribeUserType {
285                 subpattern: Pattern {
286                     kind: box PatternKind::Binding {
287                         mode: BindingMode::ByValue,
288                         var,
289                         subpattern: None,
290                         ..
291                     },
292                     ..
293                 },
294                 user_ty: pat_ascription_ty,
295                 user_ty_span,
296             } => {
297                 let place =
298                     self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
299                 unpack!(block = self.into(&place, block, initializer));
300
301                 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
302                 let pattern_source_info = self.source_info(irrefutable_pat.span);
303                 self.cfg.push(
304                     block,
305                     Statement {
306                         source_info: pattern_source_info,
307                         kind: StatementKind::FakeRead(FakeReadCause::ForLet, place.clone()),
308                     },
309                 );
310
311                 let ty_source_info = self.source_info(user_ty_span);
312                 self.cfg.push(
313                     block,
314                     Statement {
315                         source_info: ty_source_info,
316                         kind: StatementKind::AscribeUserType(
317                             place,
318                             ty::Variance::Invariant,
319                             box pat_ascription_ty.user_ty(),
320                         ),
321                     },
322                 );
323
324                 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
325                 block.unit()
326             }
327             _ => {
328                 let place = unpack!(block = self.as_place(block, initializer));
329                 self.place_into_pattern(block, irrefutable_pat, &place, true)
330             }
331         }
332     }
333
334     pub fn place_into_pattern(
335         &mut self,
336         mut block: BasicBlock,
337         irrefutable_pat: Pattern<'tcx>,
338         initializer: &Place<'tcx>,
339         set_match_place: bool,
340     ) -> BlockAnd<()> {
341         // create a dummy candidate
342         let mut candidate = Candidate {
343             span: irrefutable_pat.span,
344             match_pairs: vec![MatchPair::new(initializer.clone(), &irrefutable_pat)],
345             bindings: vec![],
346             ascriptions: vec![],
347             guard: None,
348
349             // since we don't call `match_candidates`, next fields is unused
350             arm_index: 0,
351             pat_index: 0,
352             pre_binding_block: block,
353             next_candidate_pre_binding_block: block,
354         };
355
356         // Simplify the candidate. Since the pattern is irrefutable, this should
357         // always convert all match-pairs into bindings.
358         unpack!(block = self.simplify_candidate(block, &mut candidate));
359
360         if !candidate.match_pairs.is_empty() {
361             span_bug!(
362                 candidate.match_pairs[0].pattern.span,
363                 "match pairs {:?} remaining after simplifying \
364                  irrefutable pattern",
365                 candidate.match_pairs
366             );
367         }
368
369         // for matches and function arguments, the place that is being matched
370         // can be set when creating the variables. But the place for
371         // let PATTERN = ... might not even exist until we do the assignment.
372         // so we set it here instead
373         if set_match_place {
374             for binding in &candidate.bindings {
375                 let local = self.var_local_id(binding.var_id, OutsideGuard);
376
377                 if let Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
378                     opt_match_place: Some((ref mut match_place, _)),
379                     ..
380                 }))) = self.local_decls[local].is_user_variable
381                 {
382                     *match_place = Some(initializer.clone());
383                 } else {
384                     bug!("Let binding to non-user variable.")
385                 }
386             }
387         }
388
389         self.ascribe_types(block, &candidate.ascriptions);
390
391         // now apply the bindings, which will also declare the variables
392         self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
393
394         block.unit()
395     }
396
397     /// Declares the bindings of the given patterns and returns the visibility
398     /// scope for the bindings in these patterns, if such a scope had to be
399     /// created. NOTE: Declaring the bindings should always be done in their
400     /// drop scope.
401     pub fn declare_bindings(
402         &mut self,
403         mut visibility_scope: Option<SourceScope>,
404         scope_span: Span,
405         lint_level: LintLevel,
406         patterns: &[Pattern<'tcx>],
407         has_guard: ArmHasGuard,
408         opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
409     ) -> Option<SourceScope> {
410         assert!(
411             !(visibility_scope.is_some() && lint_level.is_explicit()),
412             "can't have both a visibility and a lint scope at the same time"
413         );
414         let mut scope = self.source_scope;
415         let num_patterns = patterns.len();
416         self.visit_bindings(
417             &patterns[0],
418             None,
419             &mut |this, mutability, name, mode, var, span, ty, user_ty| {
420                 if visibility_scope.is_none() {
421                     visibility_scope =
422                         Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
423                     // If we have lints, create a new source scope
424                     // that marks the lints for the locals. See the comment
425                     // on the `source_info` field for why this is needed.
426                     if lint_level.is_explicit() {
427                         scope = this.new_source_scope(scope_span, lint_level, None);
428                     }
429                 }
430                 let source_info = SourceInfo { span, scope };
431                 let visibility_scope = visibility_scope.unwrap();
432                 this.declare_binding(
433                     source_info,
434                     visibility_scope,
435                     mutability,
436                     name,
437                     mode,
438                     num_patterns,
439                     var,
440                     ty,
441                     user_ty,
442                     has_guard,
443                     opt_match_place.map(|(x, y)| (x.cloned(), y)),
444                     patterns[0].span,
445                 );
446             },
447         );
448         visibility_scope
449     }
450
451     pub fn storage_live_binding(
452         &mut self,
453         block: BasicBlock,
454         var: NodeId,
455         span: Span,
456         for_guard: ForGuard,
457     ) -> Place<'tcx> {
458         let local_id = self.var_local_id(var, for_guard);
459         let source_info = self.source_info(span);
460         self.cfg.push(
461             block,
462             Statement {
463                 source_info,
464                 kind: StatementKind::StorageLive(local_id),
465             },
466         );
467         let place = Place::Local(local_id);
468         let var_ty = self.local_decls[local_id].ty;
469         let hir_id = self.hir.tcx().hir.node_to_hir_id(var);
470         let region_scope = self.hir.region_scope_tree.var_scope(hir_id.local_id);
471         self.schedule_drop(span, region_scope, &place, var_ty, DropKind::Storage);
472         place
473     }
474
475     pub fn schedule_drop_for_binding(&mut self, var: NodeId, span: Span, for_guard: ForGuard) {
476         let local_id = self.var_local_id(var, for_guard);
477         let var_ty = self.local_decls[local_id].ty;
478         let hir_id = self.hir.tcx().hir.node_to_hir_id(var);
479         let region_scope = self.hir.region_scope_tree.var_scope(hir_id.local_id);
480         self.schedule_drop(
481             span,
482             region_scope,
483             &Place::Local(local_id),
484             var_ty,
485             DropKind::Value {
486                 cached_block: CachedBlock::default(),
487             },
488         );
489     }
490
491     pub(super) fn visit_bindings(
492         &mut self,
493         pattern: &Pattern<'tcx>,
494         mut pattern_user_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
495         f: &mut impl FnMut(
496             &mut Self,
497             Mutability,
498             Name,
499             BindingMode,
500             NodeId,
501             Span,
502             Ty<'tcx>,
503             Option<(PatternTypeAnnotation<'tcx>, Span)>,
504         ),
505     ) {
506         match *pattern.kind {
507             PatternKind::Binding {
508                 mutability,
509                 name,
510                 mode,
511                 var,
512                 ty,
513                 ref subpattern,
514                 ..
515             } => {
516                 match mode {
517                     BindingMode::ByValue => { }
518                     BindingMode::ByRef(..) => {
519                         // If this is a `ref` binding (e.g., `let ref
520                         // x: T = ..`), then the type of `x` is not
521                         // `T` but rather `&T`, so ignore
522                         // `pattern_user_ty` for now.
523                         //
524                         // FIXME(#47184): extract or handle `pattern_user_ty` somehow
525                         pattern_user_ty = None;
526                     }
527                 }
528
529                 f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty);
530                 if let Some(subpattern) = subpattern.as_ref() {
531                     self.visit_bindings(subpattern, pattern_user_ty, f);
532                 }
533             }
534             PatternKind::Array {
535                 ref prefix,
536                 ref slice,
537                 ref suffix,
538             }
539             | PatternKind::Slice {
540                 ref prefix,
541                 ref slice,
542                 ref suffix,
543             } => {
544                 // FIXME(#47184): extract or handle `pattern_user_ty` somehow
545                 for subpattern in prefix.iter().chain(slice).chain(suffix) {
546                     self.visit_bindings(subpattern, None, f);
547                 }
548             }
549             PatternKind::Constant { .. } | PatternKind::Range { .. } | PatternKind::Wild => {}
550             PatternKind::Deref { ref subpattern } => {
551                 // FIXME(#47184): extract or handle `pattern_user_ty` somehow
552                 self.visit_bindings(subpattern, None, f);
553             }
554             PatternKind::AscribeUserType { ref subpattern, user_ty, user_ty_span } => {
555                 // This corresponds to something like
556                 //
557                 // ```
558                 // let A::<'a>(_): A<'static> = ...;
559                 // ```
560                 //
561                 // FIXME(#47184): handle `pattern_user_ty` somehow
562                 self.visit_bindings(subpattern, Some((user_ty, user_ty_span)), f)
563             }
564             PatternKind::Leaf { ref subpatterns }
565             | PatternKind::Variant {
566                 ref subpatterns, ..
567             } => {
568                 // FIXME(#47184): extract or handle `pattern_user_ty` somehow
569                 for subpattern in subpatterns {
570                     self.visit_bindings(&subpattern.pattern, None, f);
571                 }
572             }
573         }
574     }
575 }
576
577 /// List of blocks for each arm (and potentially other metadata in the
578 /// future).
579 struct ArmBlocks {
580     blocks: Vec<BasicBlock>,
581 }
582
583 #[derive(Clone, Debug)]
584 pub struct Candidate<'pat, 'tcx: 'pat> {
585     // span of the original pattern that gave rise to this candidate
586     span: Span,
587
588     // all of these must be satisfied...
589     match_pairs: Vec<MatchPair<'pat, 'tcx>>,
590
591     // ...these bindings established...
592     bindings: Vec<Binding<'tcx>>,
593
594     // ...these types asserted...
595     ascriptions: Vec<Ascription<'tcx>>,
596
597     // ...and the guard must be evaluated...
598     guard: Option<Guard<'tcx>>,
599
600     // ...and then we branch to arm with this index.
601     arm_index: usize,
602
603     // ...and the blocks for add false edges between candidates
604     pre_binding_block: BasicBlock,
605     next_candidate_pre_binding_block: BasicBlock,
606
607     // This uniquely identifies this candidate *within* the arm.
608     pat_index: usize,
609 }
610
611 #[derive(Clone, Debug)]
612 struct Binding<'tcx> {
613     span: Span,
614     source: Place<'tcx>,
615     name: Name,
616     var_id: NodeId,
617     var_ty: Ty<'tcx>,
618     mutability: Mutability,
619     binding_mode: BindingMode<'tcx>,
620 }
621
622 /// Indicates that the type of `source` must be a subtype of the
623 /// user-given type `user_ty`; this is basically a no-op but can
624 /// influence region inference.
625 #[derive(Clone, Debug)]
626 struct Ascription<'tcx> {
627     span: Span,
628     source: Place<'tcx>,
629     user_ty: PatternTypeAnnotation<'tcx>,
630 }
631
632 #[derive(Clone, Debug)]
633 pub struct MatchPair<'pat, 'tcx: 'pat> {
634     // this place...
635     place: Place<'tcx>,
636
637     // ... must match this pattern.
638     pattern: &'pat Pattern<'tcx>,
639
640     // HACK(eddyb) This is used to toggle whether a Slice pattern
641     // has had its length checked. This is only necessary because
642     // the "rest" part of the pattern right now has type &[T] and
643     // as such, it requires an Rvalue::Slice to be generated.
644     // See RFC 495 / issue #23121 for the eventual (proper) solution.
645     slice_len_checked: bool,
646 }
647
648 #[derive(Clone, Debug, PartialEq)]
649 enum TestKind<'tcx> {
650     // test the branches of enum
651     Switch {
652         adt_def: &'tcx ty::AdtDef,
653         variants: BitSet<usize>,
654     },
655
656     // test the branches of enum
657     SwitchInt {
658         switch_ty: Ty<'tcx>,
659         options: Vec<u128>,
660         indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
661     },
662
663     // test for equality
664     Eq {
665         value: &'tcx ty::Const<'tcx>,
666         ty: Ty<'tcx>,
667     },
668
669     // test whether the value falls within an inclusive or exclusive range
670     Range {
671         lo: &'tcx ty::Const<'tcx>,
672         hi: &'tcx ty::Const<'tcx>,
673         ty: Ty<'tcx>,
674         end: hir::RangeEnd,
675     },
676
677     // test length of the slice is equal to len
678     Len {
679         len: u64,
680         op: BinOp,
681     },
682 }
683
684 #[derive(Debug)]
685 pub struct Test<'tcx> {
686     span: Span,
687     kind: TestKind<'tcx>,
688 }
689
690 ///////////////////////////////////////////////////////////////////////////
691 // Main matching algorithm
692
693 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
694     /// The main match algorithm. It begins with a set of candidates
695     /// `candidates` and has the job of generating code to determine
696     /// which of these candidates, if any, is the correct one. The
697     /// candidates are sorted such that the first item in the list
698     /// has the highest priority. When a candidate is found to match
699     /// the value, we will generate a branch to the appropriate
700     /// block found in `arm_blocks`.
701     ///
702     /// The return value is a list of "otherwise" blocks. These are
703     /// points in execution where we found that *NONE* of the
704     /// candidates apply.  In principle, this means that the input
705     /// list was not exhaustive, though at present we sometimes are
706     /// not smart enough to recognize all exhaustive inputs.
707     ///
708     /// It might be surprising that the input can be inexhaustive.
709     /// Indeed, initially, it is not, because all matches are
710     /// exhaustive in Rust. But during processing we sometimes divide
711     /// up the list of candidates and recurse with a non-exhaustive
712     /// list. This is important to keep the size of the generated code
713     /// under control. See `test_candidates` for more details.
714     ///
715     /// If `add_fake_borrows` is true, then places which need fake borrows
716     /// will be added to it.
717     fn match_candidates<'pat>(
718         &mut self,
719         span: Span,
720         arm_blocks: &mut ArmBlocks,
721         mut candidates: Vec<Candidate<'pat, 'tcx>>,
722         mut block: BasicBlock,
723         fake_borrows: &mut Option<FxHashMap<Place<'tcx>, BorrowKind>>,
724     ) -> Vec<BasicBlock> {
725         debug!(
726             "matched_candidate(span={:?}, block={:?}, candidates={:?})",
727             span, block, candidates
728         );
729
730         // Start by simplifying candidates. Once this process is
731         // complete, all the match pairs which remain require some
732         // form of test, whether it be a switch or pattern comparison.
733         for candidate in &mut candidates {
734             unpack!(block = self.simplify_candidate(block, candidate));
735         }
736
737         // The candidates are sorted by priority. Check to see
738         // whether the higher priority candidates (and hence at
739         // the front of the vec) have satisfied all their match
740         // pairs.
741         let fully_matched = candidates
742             .iter()
743             .take_while(|c| c.match_pairs.is_empty())
744             .count();
745         debug!(
746             "match_candidates: {:?} candidates fully matched",
747             fully_matched
748         );
749         let mut unmatched_candidates = candidates.split_off(fully_matched);
750
751         // Insert a *Shared* borrow of any places that are bound.
752         if let Some(fake_borrows) = fake_borrows {
753             for Binding { source, .. }
754                 in candidates.iter().flat_map(|candidate| &candidate.bindings)
755             {
756                 fake_borrows.insert(source.clone(), BorrowKind::Shared);
757             }
758         }
759
760         let fully_matched_with_guard = candidates.iter().take_while(|c| c.guard.is_some()).count();
761
762         let unreachable_candidates = if fully_matched_with_guard + 1 < candidates.len() {
763             candidates.split_off(fully_matched_with_guard + 1)
764         } else {
765             vec![]
766         };
767
768         for candidate in candidates {
769             // If so, apply any bindings, test the guard (if any), and
770             // branch to the arm.
771             if let Some(b) = self.bind_and_guard_matched_candidate(block, arm_blocks, candidate) {
772                 block = b;
773             } else {
774                 // if None is returned, then any remaining candidates
775                 // are unreachable (at least not through this path).
776                 // Link them with false edges.
777                 debug!(
778                     "match_candidates: add false edges for unreachable {:?} and unmatched {:?}",
779                     unreachable_candidates, unmatched_candidates
780                 );
781                 for candidate in unreachable_candidates {
782                     let source_info = self.source_info(candidate.span);
783                     let target = self.cfg.start_new_block();
784                     if let Some(otherwise) =
785                         self.bind_and_guard_matched_candidate(target, arm_blocks, candidate)
786                     {
787                         self.cfg
788                             .terminate(otherwise, source_info, TerminatorKind::Unreachable);
789                     }
790                 }
791
792                 if unmatched_candidates.is_empty() {
793                     return vec![];
794                 } else {
795                     let target = self.cfg.start_new_block();
796                     return self.match_candidates(
797                         span,
798                         arm_blocks,
799                         unmatched_candidates,
800                         target,
801                         &mut None,
802                     );
803                 }
804             }
805         }
806
807         // If there are no candidates that still need testing, we're done.
808         // Since all matches are exhaustive, execution should never reach this point.
809         if unmatched_candidates.is_empty() {
810             return vec![block];
811         }
812
813         // Test candidates where possible.
814         let (otherwise, tested_candidates) =
815             self.test_candidates(span, arm_blocks, &unmatched_candidates, block, fake_borrows);
816
817         // If the target candidates were exhaustive, then we are done.
818         // But for borrowck continue build decision tree.
819
820         // If all candidates were sorted into `target_candidates` somewhere, then
821         // the initial set was inexhaustive.
822         let untested_candidates = unmatched_candidates.split_off(tested_candidates);
823         if untested_candidates.len() == 0 {
824             return otherwise;
825         }
826
827         // Otherwise, let's process those remaining candidates.
828         let join_block = self.join_otherwise_blocks(span, otherwise);
829         self.match_candidates(span, arm_blocks, untested_candidates, join_block, &mut None)
830     }
831
832     fn join_otherwise_blocks(&mut self, span: Span, mut otherwise: Vec<BasicBlock>) -> BasicBlock {
833         let source_info = self.source_info(span);
834         otherwise.sort();
835         otherwise.dedup(); // variant switches can introduce duplicate target blocks
836         if otherwise.len() == 1 {
837             otherwise[0]
838         } else {
839             let join_block = self.cfg.start_new_block();
840             for block in otherwise {
841                 self.cfg.terminate(
842                     block,
843                     source_info,
844                     TerminatorKind::Goto { target: join_block },
845                 );
846             }
847             join_block
848         }
849     }
850
851     /// This is the most subtle part of the matching algorithm.  At
852     /// this point, the input candidates have been fully simplified,
853     /// and so we know that all remaining match-pairs require some
854     /// sort of test. To decide what test to do, we take the highest
855     /// priority candidate (last one in the list) and extract the
856     /// first match-pair from the list. From this we decide what kind
857     /// of test is needed using `test`, defined in the `test` module.
858     ///
859     /// *Note:* taking the first match pair is somewhat arbitrary, and
860     /// we might do better here by choosing more carefully what to
861     /// test.
862     ///
863     /// For example, consider the following possible match-pairs:
864     ///
865     /// 1. `x @ Some(P)` -- we will do a `Switch` to decide what variant `x` has
866     /// 2. `x @ 22` -- we will do a `SwitchInt`
867     /// 3. `x @ 3..5` -- we will do a range test
868     /// 4. etc.
869     ///
870     /// Once we know what sort of test we are going to perform, this
871     /// test may also help us with other candidates. So we walk over
872     /// the candidates (from high to low priority) and check. This
873     /// gives us, for each outcome of the test, a transformed list of
874     /// candidates.  For example, if we are testing the current
875     /// variant of `x.0`, and we have a candidate `{x.0 @ Some(v), x.1
876     /// @ 22}`, then we would have a resulting candidate of `{(x.0 as
877     /// Some).0 @ v, x.1 @ 22}`. Note that the first match-pair is now
878     /// simpler (and, in fact, irrefutable).
879     ///
880     /// But there may also be candidates that the test just doesn't
881     /// apply to. The classical example involves wildcards:
882     ///
883     /// ```
884     /// # let (x, y, z) = (true, true, true);
885     /// match (x, y, z) {
886     ///     (true, _, true) => true,    // (0)
887     ///     (_, true, _) => true,       // (1)
888     ///     (false, false, _) => false, // (2)
889     ///     (true, _, false) => false,  // (3)
890     /// }
891     /// ```
892     ///
893     /// In that case, after we test on `x`, there are 2 overlapping candidate
894     /// sets:
895     ///
896     /// - If the outcome is that `x` is true, candidates 0, 1, and 3
897     /// - If the outcome is that `x` is false, candidates 1 and 2
898     ///
899     /// Here, the traditional "decision tree" method would generate 2
900     /// separate code-paths for the 2 separate cases.
901     ///
902     /// In some cases, this duplication can create an exponential amount of
903     /// code. This is most easily seen by noticing that this method terminates
904     /// with precisely the reachable arms being reachable - but that problem
905     /// is trivially NP-complete:
906     ///
907     /// ```rust
908     ///     match (var0, var1, var2, var3, ..) {
909     ///         (true, _, _, false, true, ...) => false,
910     ///         (_, true, true, false, _, ...) => false,
911     ///         (false, _, false, false, _, ...) => false,
912     ///         ...
913     ///         _ => true
914     ///     }
915     /// ```
916     ///
917     /// Here the last arm is reachable only if there is an assignment to
918     /// the variables that does not match any of the literals. Therefore,
919     /// compilation would take an exponential amount of time in some cases.
920     ///
921     /// That kind of exponential worst-case might not occur in practice, but
922     /// our simplistic treatment of constants and guards would make it occur
923     /// in very common situations - for example #29740:
924     ///
925     /// ```rust
926     /// match x {
927     ///     "foo" if foo_guard => ...,
928     ///     "bar" if bar_guard => ...,
929     ///     "baz" if baz_guard => ...,
930     ///     ...
931     /// }
932     /// ```
933     ///
934     /// Here we first test the match-pair `x @ "foo"`, which is an `Eq` test.
935     ///
936     /// It might seem that we would end up with 2 disjoint candidate
937     /// sets, consisting of the first candidate or the other 3, but our
938     /// algorithm doesn't reason about "foo" being distinct from the other
939     /// constants; it considers the latter arms to potentially match after
940     /// both outcomes, which obviously leads to an exponential amount
941     /// of tests.
942     ///
943     /// To avoid these kinds of problems, our algorithm tries to ensure
944     /// the amount of generated tests is linear. When we do a k-way test,
945     /// we return an additional "unmatched" set alongside the obvious `k`
946     /// sets. When we encounter a candidate that would be present in more
947     /// than one of the sets, we put it and all candidates below it into the
948     /// "unmatched" set. This ensures these `k+1` sets are disjoint.
949     ///
950     /// After we perform our test, we branch into the appropriate candidate
951     /// set and recurse with `match_candidates`. These sub-matches are
952     /// obviously inexhaustive - as we discarded our otherwise set - so
953     /// we set their continuation to do `match_candidates` on the
954     /// "unmatched" set (which is again inexhaustive).
955     ///
956     /// If you apply this to the above test, you basically wind up
957     /// with an if-else-if chain, testing each candidate in turn,
958     /// which is precisely what we want.
959     ///
960     /// In addition to avoiding exponential-time blowups, this algorithm
961     /// also has nice property that each guard and arm is only generated
962     /// once.
963     fn test_candidates<'pat>(
964         &mut self,
965         span: Span,
966         arm_blocks: &mut ArmBlocks,
967         candidates: &[Candidate<'pat, 'tcx>],
968         block: BasicBlock,
969         fake_borrows: &mut Option<FxHashMap<Place<'tcx>, BorrowKind>>,
970     ) -> (Vec<BasicBlock>, usize) {
971         // extract the match-pair from the highest priority candidate
972         let match_pair = &candidates.first().unwrap().match_pairs[0];
973         let mut test = self.test(match_pair);
974
975         // most of the time, the test to perform is simply a function
976         // of the main candidate; but for a test like SwitchInt, we
977         // may want to add cases based on the candidates that are
978         // available
979         match test.kind {
980             TestKind::SwitchInt {
981                 switch_ty,
982                 ref mut options,
983                 ref mut indices,
984             } => {
985                 for candidate in candidates.iter() {
986                     if !self.add_cases_to_switch(
987                         &match_pair.place,
988                         candidate,
989                         switch_ty,
990                         options,
991                         indices,
992                     ) {
993                         break;
994                     }
995                 }
996             }
997             TestKind::Switch {
998                 adt_def: _,
999                 ref mut variants,
1000             } => {
1001                 for candidate in candidates.iter() {
1002                     if !self.add_variants_to_switch(&match_pair.place, candidate, variants) {
1003                         break;
1004                     }
1005                 }
1006             }
1007             _ => {}
1008         }
1009
1010         // Insert a Shallow borrow of any places that is switched on.
1011         fake_borrows.as_mut().map(|fb| {
1012             fb.entry(match_pair.place.clone()).or_insert(BorrowKind::Shallow)
1013         });
1014
1015         // perform the test, branching to one of N blocks. For each of
1016         // those N possible outcomes, create a (initially empty)
1017         // vector of candidates. Those are the candidates that still
1018         // apply if the test has that particular outcome.
1019         debug!(
1020             "match_candidates: test={:?} match_pair={:?}",
1021             test, match_pair
1022         );
1023         let target_blocks = self.perform_test(block, &match_pair.place, &test);
1024         let mut target_candidates: Vec<_> = (0..target_blocks.len()).map(|_| vec![]).collect();
1025
1026         // Sort the candidates into the appropriate vector in
1027         // `target_candidates`. Note that at some point we may
1028         // encounter a candidate where the test is not relevant; at
1029         // that point, we stop sorting.
1030         let tested_candidates = candidates
1031             .iter()
1032             .take_while(|c| {
1033                 self.sort_candidate(&match_pair.place, &test, c, &mut target_candidates)
1034             })
1035             .count();
1036         assert!(tested_candidates > 0); // at least the last candidate ought to be tested
1037         debug!("tested_candidates: {}", tested_candidates);
1038         debug!(
1039             "untested_candidates: {}",
1040             candidates.len() - tested_candidates
1041         );
1042
1043         // For each outcome of test, process the candidates that still
1044         // apply. Collect a list of blocks where control flow will
1045         // branch if one of the `target_candidate` sets is not
1046         // exhaustive.
1047         let otherwise: Vec<_> = target_blocks
1048             .into_iter()
1049             .zip(target_candidates)
1050             .flat_map(|(target_block, target_candidates)| {
1051                 self.match_candidates(
1052                     span,
1053                     arm_blocks,
1054                     target_candidates,
1055                     target_block,
1056                     fake_borrows,
1057                 )
1058             })
1059             .collect();
1060
1061         (otherwise, tested_candidates)
1062     }
1063
1064     /// Initializes each of the bindings from the candidate by
1065     /// moving/copying/ref'ing the source as appropriate. Tests the
1066     /// guard, if any, and then branches to the arm. Returns the block
1067     /// for the case where the guard fails.
1068     ///
1069     /// Note: we check earlier that if there is a guard, there cannot
1070     /// be move bindings.  This isn't really important for the
1071     /// self-consistency of this fn, but the reason for it should be
1072     /// clear: after we've done the assignments, if there were move
1073     /// bindings, further tests would be a use-after-move (which would
1074     /// in turn be detected by the borrowck code that runs on the
1075     /// MIR).
1076     fn bind_and_guard_matched_candidate<'pat>(
1077         &mut self,
1078         mut block: BasicBlock,
1079         arm_blocks: &mut ArmBlocks,
1080         candidate: Candidate<'pat, 'tcx>,
1081     ) -> Option<BasicBlock> {
1082         debug!(
1083             "bind_and_guard_matched_candidate(block={:?}, candidate={:?})",
1084             block, candidate
1085         );
1086
1087         debug_assert!(candidate.match_pairs.is_empty());
1088
1089         self.ascribe_types(block, &candidate.ascriptions);
1090
1091         let arm_block = arm_blocks.blocks[candidate.arm_index];
1092         let candidate_source_info = self.source_info(candidate.span);
1093
1094         self.cfg.terminate(
1095             block,
1096             candidate_source_info,
1097             TerminatorKind::Goto {
1098                 target: candidate.pre_binding_block,
1099             },
1100         );
1101
1102         block = self.cfg.start_new_block();
1103         self.cfg.terminate(
1104             candidate.pre_binding_block,
1105             candidate_source_info,
1106             TerminatorKind::FalseEdges {
1107                 real_target: block,
1108                 imaginary_targets: vec![candidate.next_candidate_pre_binding_block],
1109             },
1110         );
1111
1112         // rust-lang/rust#27282: The `autoref` business deserves some
1113         // explanation here.
1114         //
1115         // The intent of the `autoref` flag is that when it is true,
1116         // then any pattern bindings of type T will map to a `&T`
1117         // within the context of the guard expression, but will
1118         // continue to map to a `T` in the context of the arm body. To
1119         // avoid surfacing this distinction in the user source code
1120         // (which would be a severe change to the language and require
1121         // far more revision to the compiler), when `autoref` is true,
1122         // then any occurrence of the identifier in the guard
1123         // expression will automatically get a deref op applied to it.
1124         //
1125         // So an input like:
1126         //
1127         // ```
1128         // let place = Foo::new();
1129         // match place { foo if inspect(foo)
1130         //     => feed(foo), ...  }
1131         // ```
1132         //
1133         // will be treated as if it were really something like:
1134         //
1135         // ```
1136         // let place = Foo::new();
1137         // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
1138         //     => { let tmp2 = place; feed(tmp2) }, ... }
1139         //
1140         // And an input like:
1141         //
1142         // ```
1143         // let place = Foo::new();
1144         // match place { ref mut foo if inspect(foo)
1145         //     => feed(foo), ...  }
1146         // ```
1147         //
1148         // will be treated as if it were really something like:
1149         //
1150         // ```
1151         // let place = Foo::new();
1152         // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
1153         //     => { let tmp2 = &mut place; feed(tmp2) }, ... }
1154         // ```
1155         //
1156         // In short, any pattern binding will always look like *some*
1157         // kind of `&T` within the guard at least in terms of how the
1158         // MIR-borrowck views it, and this will ensure that guard
1159         // expressions cannot mutate their the match inputs via such
1160         // bindings. (It also ensures that guard expressions can at
1161         // most *copy* values from such bindings; non-Copy things
1162         // cannot be moved via pattern bindings in guard expressions.)
1163         //
1164         // ----
1165         //
1166         // Implementation notes (under assumption `autoref` is true).
1167         //
1168         // To encode the distinction above, we must inject the
1169         // temporaries `tmp1` and `tmp2`.
1170         //
1171         // There are two cases of interest: binding by-value, and binding by-ref.
1172         //
1173         // 1. Binding by-value: Things are simple.
1174         //
1175         //    * Establishing `tmp1` creates a reference into the
1176         //      matched place. This code is emitted by
1177         //      bind_matched_candidate_for_guard.
1178         //
1179         //    * `tmp2` is only initialized "lazily", after we have
1180         //      checked the guard. Thus, the code that can trigger
1181         //      moves out of the candidate can only fire after the
1182         //      guard evaluated to true. This initialization code is
1183         //      emitted by bind_matched_candidate_for_arm.
1184         //
1185         // 2. Binding by-reference: Things are tricky.
1186         //
1187         //    * Here, the guard expression wants a `&&` or `&&mut`
1188         //      into the original input. This means we need to borrow
1189         //      a reference that we do not immediately have at hand
1190         //      (because all we have is the places associated with the
1191         //      match input itself; it is up to us to create a place
1192         //      holding a `&` or `&mut` that we can then borrow).
1193
1194         let autoref = self.hir
1195             .tcx()
1196             .all_pat_vars_are_implicit_refs_within_guards();
1197         if let Some(guard) = candidate.guard {
1198             if autoref {
1199                 self.bind_matched_candidate_for_guard(
1200                     block,
1201                     candidate.pat_index,
1202                     &candidate.bindings,
1203                 );
1204                 let guard_frame = GuardFrame {
1205                     locals: candidate
1206                         .bindings
1207                         .iter()
1208                         .map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode))
1209                         .collect(),
1210                 };
1211                 debug!("Entering guard building context: {:?}", guard_frame);
1212                 self.guard_context.push(guard_frame);
1213             } else {
1214                 self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
1215             }
1216
1217             // the block to branch to if the guard fails; if there is no
1218             // guard, this block is simply unreachable
1219             let guard = match guard {
1220                 Guard::If(e) => self.hir.mirror(e),
1221             };
1222             let source_info = self.source_info(guard.span);
1223             let cond = unpack!(block = self.as_local_operand(block, guard));
1224             if autoref {
1225                 let guard_frame = self.guard_context.pop().unwrap();
1226                 debug!(
1227                     "Exiting guard building context with locals: {:?}",
1228                     guard_frame
1229                 );
1230             }
1231
1232             let false_edge_block = self.cfg.start_new_block();
1233
1234             // We want to ensure that the matched candidates are bound
1235             // after we have confirmed this candidate *and* any
1236             // associated guard; Binding them on `block` is too soon,
1237             // because that would be before we've checked the result
1238             // from the guard.
1239             //
1240             // But binding them on `arm_block` is *too late*, because
1241             // then all of the candidates for a single arm would be
1242             // bound in the same place, that would cause a case like:
1243             //
1244             // ```rust
1245             // match (30, 2) {
1246             //     (mut x, 1) | (2, mut x) if { true } => { ... }
1247             //     ...                                 // ^^^^^^^ (this is `arm_block`)
1248             // }
1249             // ```
1250             //
1251             // would yield a `arm_block` something like:
1252             //
1253             // ```
1254             // StorageLive(_4);        // _4 is `x`
1255             // _4 = &mut (_1.0: i32);  // this is handling `(mut x, 1)` case
1256             // _4 = &mut (_1.1: i32);  // this is handling `(2, mut x)` case
1257             // ```
1258             //
1259             // and that is clearly not correct.
1260             let post_guard_block = self.cfg.start_new_block();
1261             self.cfg.terminate(
1262                 block,
1263                 source_info,
1264                 TerminatorKind::if_(self.hir.tcx(), cond, post_guard_block, false_edge_block),
1265             );
1266
1267             if autoref {
1268                 self.bind_matched_candidate_for_arm_body(post_guard_block, &candidate.bindings);
1269             }
1270
1271             self.cfg.terminate(
1272                 post_guard_block,
1273                 source_info,
1274                 TerminatorKind::Goto { target: arm_block },
1275             );
1276
1277             let otherwise = self.cfg.start_new_block();
1278
1279             self.cfg.terminate(
1280                 false_edge_block,
1281                 source_info,
1282                 TerminatorKind::FalseEdges {
1283                     real_target: otherwise,
1284                     imaginary_targets: vec![candidate.next_candidate_pre_binding_block],
1285                 },
1286             );
1287             Some(otherwise)
1288         } else {
1289             // (Here, it is not too early to bind the matched
1290             // candidate on `block`, because there is no guard result
1291             // that we have to inspect before we bind them.)
1292             self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
1293             self.cfg.terminate(
1294                 block,
1295                 candidate_source_info,
1296                 TerminatorKind::Goto { target: arm_block },
1297             );
1298             None
1299         }
1300     }
1301
1302     /// Append `AscribeUserType` statements onto the end of `block`
1303     /// for each ascription
1304     fn ascribe_types<'pat>(
1305         &mut self,
1306         block: BasicBlock,
1307         ascriptions: &[Ascription<'tcx>],
1308     ) {
1309         for ascription in ascriptions {
1310             let source_info = self.source_info(ascription.span);
1311
1312             debug!(
1313                 "adding user ascription at span {:?} of place {:?} and {:?}",
1314                 source_info.span,
1315                 ascription.source,
1316                 ascription.user_ty,
1317             );
1318
1319             self.cfg.push(
1320                 block,
1321                 Statement {
1322                     source_info,
1323                     kind: StatementKind::AscribeUserType(
1324                         ascription.source.clone(),
1325                         ty::Variance::Covariant,
1326                         box ascription.user_ty.user_ty(),
1327                     ),
1328                 },
1329             );
1330         }
1331     }
1332
1333     // Only called when all_pat_vars_are_implicit_refs_within_guards,
1334     // and thus all code/comments assume we are in that context.
1335     fn bind_matched_candidate_for_guard(
1336         &mut self,
1337         block: BasicBlock,
1338         pat_index: usize,
1339         bindings: &[Binding<'tcx>],
1340     ) {
1341         debug!(
1342             "bind_matched_candidate_for_guard(block={:?}, pat_index={:?}, bindings={:?})",
1343             block, pat_index, bindings
1344         );
1345
1346         // Assign each of the bindings. Since we are binding for a
1347         // guard expression, this will never trigger moves out of the
1348         // candidate.
1349         let re_empty = self.hir.tcx().types.re_empty;
1350         for binding in bindings {
1351             let source_info = self.source_info(binding.span);
1352
1353             // For each pattern ident P of type T, `ref_for_guard` is
1354             // a reference R: &T pointing to the location matched by
1355             // the pattern, and every occurrence of P within a guard
1356             // denotes *R.
1357             let ref_for_guard =
1358                 self.storage_live_binding(block, binding.var_id, binding.span, RefWithinGuard);
1359             // Question: Why schedule drops if bindings are all
1360             // shared-&'s?  Answer: Because schedule_drop_for_binding
1361             // also emits StorageDead's for those locals.
1362             self.schedule_drop_for_binding(binding.var_id, binding.span, RefWithinGuard);
1363             match binding.binding_mode {
1364                 BindingMode::ByValue => {
1365                     let rvalue = Rvalue::Ref(re_empty, BorrowKind::Shared, binding.source.clone());
1366                     self.cfg
1367                         .push_assign(block, source_info, &ref_for_guard, rvalue);
1368                 }
1369                 BindingMode::ByRef(region, borrow_kind) => {
1370                     // Tricky business: For `ref id` and `ref mut id`
1371                     // patterns, we want `id` within the guard to
1372                     // correspond to a temp of type `& &T` or `& &mut
1373                     // T` (i.e. a "borrow of a borrow") that is
1374                     // implicitly dereferenced.
1375                     //
1376                     // To borrow a borrow, we need that inner borrow
1377                     // to point to. So, create a temp for the inner
1378                     // borrow, and then take a reference to it.
1379                     //
1380                     // Note: the temp created here is *not* the one
1381                     // used by the arm body itself. This eases
1382                     // observing two-phase borrow restrictions.
1383                     let val_for_guard = self.storage_live_binding(
1384                         block,
1385                         binding.var_id,
1386                         binding.span,
1387                         ValWithinGuard(pat_index),
1388                     );
1389                     self.schedule_drop_for_binding(
1390                         binding.var_id,
1391                         binding.span,
1392                         ValWithinGuard(pat_index),
1393                     );
1394
1395                     // rust-lang/rust#27282: We reuse the two-phase
1396                     // borrow infrastructure so that the mutable
1397                     // borrow (whose mutabilty is *unusable* within
1398                     // the guard) does not conflict with the implicit
1399                     // borrow of the whole match input. See additional
1400                     // discussion on rust-lang/rust#49870.
1401                     let borrow_kind = match borrow_kind {
1402                         BorrowKind::Shared
1403                         | BorrowKind::Shallow
1404                         | BorrowKind::Unique => borrow_kind,
1405                         BorrowKind::Mut { .. } => BorrowKind::Mut {
1406                             allow_two_phase_borrow: true,
1407                         },
1408                     };
1409                     let rvalue = Rvalue::Ref(region, borrow_kind, binding.source.clone());
1410                     self.cfg
1411                         .push_assign(block, source_info, &val_for_guard, rvalue);
1412                     let rvalue = Rvalue::Ref(region, BorrowKind::Shared, val_for_guard);
1413                     self.cfg
1414                         .push_assign(block, source_info, &ref_for_guard, rvalue);
1415                 }
1416             }
1417         }
1418     }
1419
1420     fn bind_matched_candidate_for_arm_body(
1421         &mut self,
1422         block: BasicBlock,
1423         bindings: &[Binding<'tcx>],
1424     ) {
1425         debug!(
1426             "bind_matched_candidate_for_arm_body(block={:?}, bindings={:?}",
1427             block, bindings
1428         );
1429
1430         // Assign each of the bindings. This may trigger moves out of the candidate.
1431         for binding in bindings {
1432             let source_info = self.source_info(binding.span);
1433             let local =
1434                 self.storage_live_binding(block, binding.var_id, binding.span, OutsideGuard);
1435             self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
1436             let rvalue = match binding.binding_mode {
1437                 BindingMode::ByValue => {
1438                     Rvalue::Use(self.consume_by_copy_or_move(binding.source.clone()))
1439                 }
1440                 BindingMode::ByRef(region, borrow_kind) => {
1441                     Rvalue::Ref(region, borrow_kind, binding.source.clone())
1442                 }
1443             };
1444             self.cfg.push_assign(block, source_info, &local, rvalue);
1445         }
1446     }
1447
1448     /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where
1449     /// the bound `var` has type `T` in the arm body) in a pattern
1450     /// maps to `2+N` locals. The first local is a binding for
1451     /// occurrences of `var` in the guard, which will all have type
1452     /// `&T`. The N locals are bindings for the `T` that is referenced
1453     /// by the first local; they are not used outside of the
1454     /// guard. The last local is a binding for occurrences of `var` in
1455     /// the arm body, which will have type `T`.
1456     ///
1457     /// The reason we have N locals rather than just 1 is to
1458     /// accommodate rust-lang/rust#51348: If the arm has N candidate
1459     /// patterns, then in general they can correspond to distinct
1460     /// parts of the matched data, and we want them to be distinct
1461     /// temps in order to simplify checks performed by our internal
1462     /// leveraging of two-phase borrows).
1463     fn declare_binding(
1464         &mut self,
1465         source_info: SourceInfo,
1466         visibility_scope: SourceScope,
1467         mutability: Mutability,
1468         name: Name,
1469         mode: BindingMode,
1470         num_patterns: usize,
1471         var_id: NodeId,
1472         var_ty: Ty<'tcx>,
1473         user_var_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
1474         has_guard: ArmHasGuard,
1475         opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
1476         pat_span: Span,
1477     ) {
1478         debug!(
1479             "declare_binding(var_id={:?}, name={:?}, mode={:?}, var_ty={:?}, \
1480              visibility_scope={:?}, source_info={:?})",
1481             var_id, name, mode, var_ty, visibility_scope, source_info
1482         );
1483
1484         let tcx = self.hir.tcx();
1485         let binding_mode = match mode {
1486             BindingMode::ByValue => ty::BindingMode::BindByValue(mutability.into()),
1487             BindingMode::ByRef { .. } => ty::BindingMode::BindByReference(mutability.into()),
1488         };
1489         let local = LocalDecl::<'tcx> {
1490             mutability,
1491             ty: var_ty,
1492             user_ty: user_var_ty.map(|(pat_ty, span)|(pat_ty.user_ty(), span)),
1493             name: Some(name),
1494             source_info,
1495             visibility_scope,
1496             internal: false,
1497             is_block_tail: None,
1498             is_user_variable: Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
1499                 binding_mode,
1500                 // hypothetically, `visit_bindings` could try to unzip
1501                 // an outermost hir::Ty as we descend, matching up
1502                 // idents in pat; but complex w/ unclear UI payoff.
1503                 // Instead, just abandon providing diagnostic info.
1504                 opt_ty_info: None,
1505                 opt_match_place,
1506                 pat_span,
1507             }))),
1508         };
1509         let for_arm_body = self.local_decls.push(local.clone());
1510         let locals = if has_guard.0 && tcx.all_pat_vars_are_implicit_refs_within_guards() {
1511             let mut vals_for_guard = Vec::with_capacity(num_patterns);
1512             for _ in 0..num_patterns {
1513                 let val_for_guard_idx = self.local_decls.push(LocalDecl {
1514                     // This variable isn't mutated but has a name, so has to be
1515                     // immutable to avoid the unused mut lint.
1516                     mutability: Mutability::Not,
1517                     ..local.clone()
1518                 });
1519                 vals_for_guard.push(val_for_guard_idx);
1520             }
1521             let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
1522                 // See previous comment.
1523                 mutability: Mutability::Not,
1524                 ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
1525                 user_ty: None,
1526                 name: Some(name),
1527                 source_info,
1528                 visibility_scope,
1529                 // FIXME: should these secretly injected ref_for_guard's be marked as `internal`?
1530                 internal: false,
1531                 is_block_tail: None,
1532                 is_user_variable: Some(ClearCrossCrate::Set(BindingForm::RefForGuard)),
1533             });
1534             LocalsForNode::ForGuard {
1535                 vals_for_guard,
1536                 ref_for_guard,
1537                 for_arm_body,
1538             }
1539         } else {
1540             LocalsForNode::One(for_arm_body)
1541         };
1542         debug!("declare_binding: vars={:?}", locals);
1543         self.var_indices.insert(var_id, locals);
1544     }
1545
1546     // Determine the fake borrows that are needed to ensure that the place
1547     // will evaluate to the same thing until an arm has been chosen.
1548     fn add_fake_borrows<'pat>(
1549         &mut self,
1550         pre_binding_blocks: &[(BasicBlock, Span)],
1551         fake_borrows: FxHashMap<Place<'tcx>, BorrowKind>,
1552         source_info: SourceInfo,
1553         start_block: BasicBlock,
1554     ) {
1555         let tcx = self.hir.tcx();
1556
1557         debug!("add_fake_borrows pre_binding_blocks = {:?}, fake_borrows = {:?}",
1558                pre_binding_blocks, fake_borrows);
1559
1560         let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len());
1561
1562         // Insert a Shallow borrow of the prefixes of any fake borrows.
1563         for (place, borrow_kind) in fake_borrows
1564         {
1565             {
1566                 let mut prefix_cursor = &place;
1567                 while let Place::Projection(box Projection { base, elem }) = prefix_cursor {
1568                     if let ProjectionElem::Deref = elem {
1569                         // Insert a shallow borrow after a deref. For other
1570                         // projections the borrow of prefix_cursor will
1571                         // conflict with any mutation of base.
1572                         all_fake_borrows.push((base.clone(), BorrowKind::Shallow));
1573                     }
1574                     prefix_cursor = base;
1575                 }
1576             }
1577
1578             all_fake_borrows.push((place, borrow_kind));
1579         }
1580
1581         // Deduplicate and ensure a deterministic order.
1582         all_fake_borrows.sort();
1583         all_fake_borrows.dedup();
1584
1585         debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);
1586
1587         // Add fake borrows to the start of the match and reads of them before
1588         // the start of each arm.
1589         let mut borrowed_input_temps = Vec::with_capacity(all_fake_borrows.len());
1590
1591         for (matched_place, borrow_kind) in all_fake_borrows {
1592             let borrowed_input =
1593                 Rvalue::Ref(tcx.types.re_empty, borrow_kind, matched_place.clone());
1594             let borrowed_input_ty = borrowed_input.ty(&self.local_decls, tcx);
1595             let borrowed_input_temp = self.temp(borrowed_input_ty, source_info.span);
1596             self.cfg.push_assign(
1597                 start_block,
1598                 source_info,
1599                 &borrowed_input_temp,
1600                 borrowed_input
1601             );
1602             borrowed_input_temps.push(borrowed_input_temp);
1603         }
1604
1605         // FIXME: This could be a lot of reads (#fake borrows * #patterns).
1606         // The false edges that we currently generate would allow us to only do
1607         // this on the last Candidate, but it's possible that there might not be
1608         // so many false edges in the future, so we read for all Candidates for
1609         // now.
1610         // Another option would be to make our own block and add our own false
1611         // edges to it.
1612         if tcx.emit_read_for_match() {
1613             for &(pre_binding_block, span) in pre_binding_blocks {
1614                 let pattern_source_info = self.source_info(span);
1615                 for temp in &borrowed_input_temps {
1616                     self.cfg.push(pre_binding_block, Statement {
1617                         source_info: pattern_source_info,
1618                         kind: StatementKind::FakeRead(
1619                             FakeReadCause::ForMatchGuard,
1620                             temp.clone(),
1621                         ),
1622                     });
1623                 }
1624             }
1625         }
1626     }
1627 }