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.
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.
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
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};
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};
29 // helper functions, broken out by category:
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);
39 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
42 destination: &Place<'tcx>,
44 mut block: BasicBlock,
45 discriminant: ExprRef<'tcx>,
48 let tcx = self.hir.tcx();
49 let discriminant_span = discriminant.span();
50 let discriminant_place = unpack!(block = self.as_place(block, discriminant));
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.
59 // HACK(eddyb) Work around the above issue by adding a dummy inspection
60 // of `discriminant_place`, specifically by applying `ReadForMatch`.
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
68 let source_info = self.source_info(discriminant_span);
69 self.cfg.push(block, Statement {
71 kind: StatementKind::FakeRead(
72 FakeReadCause::ForMatchedPlace,
73 discriminant_place.clone(),
77 let mut arm_blocks = ArmBlocks {
78 blocks: arms.iter().map(|_| self.cfg.start_new_block()).collect(),
81 // Get the arm bodies and their scopes, while declaring bindings.
82 let arm_bodies: Vec<_> = arms.iter()
84 // BUG: use arm lint level
85 let body = self.hir.mirror(arm.body.clone());
86 let scope = self.declare_bindings(
91 ArmHasGuard(arm.guard.is_some()),
92 Some((Some(&discriminant_place), discriminant_span)),
94 (body, scope.unwrap_or(self.source_scope))
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())
104 let mut has_guard = false;
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)
112 let candidates: Vec<_> = arms.iter()
114 .flat_map(|(arm_index, arm)| {
118 .map(move |(pat_index, pat)| (arm_index, pat_index, pat, arm.guard.clone()))
123 .zip(pre_binding_blocks.iter().skip(1)),
127 (arm_index, pat_index, pattern, guard),
128 (pre_binding_block, next_candidate_pre_binding_block)
130 has_guard |= guard.is_some();
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?
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.
142 // ... /* mutate `foo` in arm body */ ...
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
155 match_pairs: vec![MatchPair::new(discriminant_place.clone(), pattern)],
161 pre_binding_block: *pre_binding_block,
162 next_candidate_pre_binding_block: *next_candidate_pre_binding_block,
168 let outer_source_info = self.source_info(span);
170 *pre_binding_blocks.last().unwrap(),
172 TerminatorKind::Unreachable,
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())
186 let pre_binding_blocks: Vec<_> = candidates
188 .map(|cand| (cand.pre_binding_block, cand.span))
191 // this will generate code to test discriminant_place and
192 // branch to the appropriate arm block
193 let otherwise = self.match_candidates(
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.
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);
210 let mut otherwise = otherwise;
212 otherwise.dedup(); // variant switches can introduce duplicate target blocks
213 for block in otherwise {
215 .terminate(block, source_info, TerminatorKind::Unreachable);
219 if let Some(fake_borrows) = fake_borrows {
220 self.add_fake_borrows(&pre_binding_blocks, fake_borrows, source_info, block);
223 // all the arm blocks will rejoin here
224 let end_block = self.cfg.start_new_block();
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));
235 TerminatorKind::Goto { target: end_block },
238 self.source_scope = outer_source_info.scope;
243 pub(super) fn expr_into_pattern(
245 mut block: BasicBlock,
246 irrefutable_pat: Pattern<'tcx>,
247 initializer: ExprRef<'tcx>,
249 match *irrefutable_pat.kind {
250 // Optimize the case of `let x = ...` to write directly into `x`
251 PatternKind::Binding {
252 mode: BindingMode::ByValue,
258 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
259 unpack!(block = self.into(&place, block, initializer));
262 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
263 let source_info = self.source_info(irrefutable_pat.span);
268 kind: StatementKind::FakeRead(FakeReadCause::ForLet, place),
272 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
276 // Optimize the case of `let x: T = ...` to write directly
277 // into `x` and then require that `T == typeof(x)`.
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
284 PatternKind::AscribeUserType {
285 subpattern: Pattern {
286 kind: box PatternKind::Binding {
287 mode: BindingMode::ByValue,
294 user_ty: pat_ascription_ty,
298 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard);
299 unpack!(block = self.into(&place, block, initializer));
301 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
302 let pattern_source_info = self.source_info(irrefutable_pat.span);
306 source_info: pattern_source_info,
307 kind: StatementKind::FakeRead(FakeReadCause::ForLet, place.clone()),
311 let ty_source_info = self.source_info(user_ty_span);
315 source_info: ty_source_info,
316 kind: StatementKind::AscribeUserType(
318 ty::Variance::Invariant,
319 box pat_ascription_ty.user_ty(),
324 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
328 let place = unpack!(block = self.as_place(block, initializer));
329 self.place_into_pattern(block, irrefutable_pat, &place, true)
334 pub fn place_into_pattern(
336 mut block: BasicBlock,
337 irrefutable_pat: Pattern<'tcx>,
338 initializer: &Place<'tcx>,
339 set_match_place: bool,
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)],
349 // since we don't call `match_candidates`, next fields is unused
352 pre_binding_block: block,
353 next_candidate_pre_binding_block: block,
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));
360 if !candidate.match_pairs.is_empty() {
362 candidate.match_pairs[0].pattern.span,
363 "match pairs {:?} remaining after simplifying \
364 irrefutable pattern",
365 candidate.match_pairs
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
374 for binding in &candidate.bindings {
375 let local = self.var_local_id(binding.var_id, OutsideGuard);
377 if let Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
378 opt_match_place: Some((ref mut match_place, _)),
380 }))) = self.local_decls[local].is_user_variable
382 *match_place = Some(initializer.clone());
384 bug!("Let binding to non-user variable.")
389 self.ascribe_types(block, &candidate.ascriptions);
391 // now apply the bindings, which will also declare the variables
392 self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
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
401 pub fn declare_bindings(
403 mut visibility_scope: Option<SourceScope>,
405 lint_level: LintLevel,
406 patterns: &[Pattern<'tcx>],
407 has_guard: ArmHasGuard,
408 opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
409 ) -> Option<SourceScope> {
411 !(visibility_scope.is_some() && lint_level.is_explicit()),
412 "can't have both a visibility and a lint scope at the same time"
414 let mut scope = self.source_scope;
415 let num_patterns = patterns.len();
419 &mut |this, mutability, name, mode, var, span, ty, user_ty| {
420 if visibility_scope.is_none() {
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);
430 let source_info = SourceInfo { span, scope };
431 let visibility_scope = visibility_scope.unwrap();
432 this.declare_binding(
443 opt_match_place.map(|(x, y)| (x.cloned(), y)),
451 pub fn storage_live_binding(
458 let local_id = self.var_local_id(var, for_guard);
459 let source_info = self.source_info(span);
464 kind: StatementKind::StorageLive(local_id),
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);
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);
483 &Place::Local(local_id),
486 cached_block: CachedBlock::default(),
491 pub(super) fn visit_bindings(
493 pattern: &Pattern<'tcx>,
494 mut pattern_user_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
503 Option<(PatternTypeAnnotation<'tcx>, Span)>,
506 match *pattern.kind {
507 PatternKind::Binding {
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.
524 // FIXME(#47184): extract or handle `pattern_user_ty` somehow
525 pattern_user_ty = None;
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);
539 | PatternKind::Slice {
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);
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);
554 PatternKind::AscribeUserType { ref subpattern, user_ty, user_ty_span } => {
555 // This corresponds to something like
558 // let A::<'a>(_): A<'static> = ...;
561 // FIXME(#47184): handle `pattern_user_ty` somehow
562 self.visit_bindings(subpattern, Some((user_ty, user_ty_span)), f)
564 PatternKind::Leaf { ref subpatterns }
565 | PatternKind::Variant {
568 // FIXME(#47184): extract or handle `pattern_user_ty` somehow
569 for subpattern in subpatterns {
570 self.visit_bindings(&subpattern.pattern, None, f);
577 /// List of blocks for each arm (and potentially other metadata in the
580 blocks: Vec<BasicBlock>,
583 #[derive(Clone, Debug)]
584 pub struct Candidate<'pat, 'tcx: 'pat> {
585 // span of the original pattern that gave rise to this candidate
588 // all of these must be satisfied...
589 match_pairs: Vec<MatchPair<'pat, 'tcx>>,
591 // ...these bindings established...
592 bindings: Vec<Binding<'tcx>>,
594 // ...these types asserted...
595 ascriptions: Vec<Ascription<'tcx>>,
597 // ...and the guard must be evaluated...
598 guard: Option<Guard<'tcx>>,
600 // ...and then we branch to arm with this index.
603 // ...and the blocks for add false edges between candidates
604 pre_binding_block: BasicBlock,
605 next_candidate_pre_binding_block: BasicBlock,
607 // This uniquely identifies this candidate *within* the arm.
611 #[derive(Clone, Debug)]
612 struct Binding<'tcx> {
618 mutability: Mutability,
619 binding_mode: BindingMode<'tcx>,
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> {
629 user_ty: PatternTypeAnnotation<'tcx>,
632 #[derive(Clone, Debug)]
633 pub struct MatchPair<'pat, 'tcx: 'pat> {
637 // ... must match this pattern.
638 pattern: &'pat Pattern<'tcx>,
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,
648 #[derive(Clone, Debug, PartialEq)]
649 enum TestKind<'tcx> {
650 // test the branches of enum
652 adt_def: &'tcx ty::AdtDef,
653 variants: BitSet<usize>,
656 // test the branches of enum
660 indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
665 value: &'tcx ty::Const<'tcx>,
669 // test whether the value falls within an inclusive or exclusive range
671 lo: &'tcx ty::Const<'tcx>,
672 hi: &'tcx ty::Const<'tcx>,
677 // test length of the slice is equal to len
685 pub struct Test<'tcx> {
687 kind: TestKind<'tcx>,
690 ///////////////////////////////////////////////////////////////////////////
691 // Main matching algorithm
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`.
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.
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.
715 /// If `add_fake_borrows` is true, then places which need fake borrows
716 /// will be added to it.
717 fn match_candidates<'pat>(
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> {
726 "matched_candidate(span={:?}, block={:?}, candidates={:?})",
727 span, block, candidates
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));
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
741 let fully_matched = candidates
743 .take_while(|c| c.match_pairs.is_empty())
746 "match_candidates: {:?} candidates fully matched",
749 let mut unmatched_candidates = candidates.split_off(fully_matched);
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)
756 fake_borrows.insert(source.clone(), BorrowKind::Shared);
760 let fully_matched_with_guard = candidates.iter().take_while(|c| c.guard.is_some()).count();
762 let unreachable_candidates = if fully_matched_with_guard + 1 < candidates.len() {
763 candidates.split_off(fully_matched_with_guard + 1)
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) {
774 // if None is returned, then any remaining candidates
775 // are unreachable (at least not through this path).
776 // Link them with false edges.
778 "match_candidates: add false edges for unreachable {:?} and unmatched {:?}",
779 unreachable_candidates, unmatched_candidates
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)
788 .terminate(otherwise, source_info, TerminatorKind::Unreachable);
792 if unmatched_candidates.is_empty() {
795 let target = self.cfg.start_new_block();
796 return self.match_candidates(
799 unmatched_candidates,
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() {
813 // Test candidates where possible.
814 let (otherwise, tested_candidates) =
815 self.test_candidates(span, arm_blocks, &unmatched_candidates, block, fake_borrows);
817 // If the target candidates were exhaustive, then we are done.
818 // But for borrowck continue build decision tree.
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 {
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)
832 fn join_otherwise_blocks(&mut self, span: Span, mut otherwise: Vec<BasicBlock>) -> BasicBlock {
833 let source_info = self.source_info(span);
835 otherwise.dedup(); // variant switches can introduce duplicate target blocks
836 if otherwise.len() == 1 {
839 let join_block = self.cfg.start_new_block();
840 for block in otherwise {
844 TerminatorKind::Goto { target: join_block },
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.
859 /// *Note:* taking the first match pair is somewhat arbitrary, and
860 /// we might do better here by choosing more carefully what to
863 /// For example, consider the following possible match-pairs:
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
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).
880 /// But there may also be candidates that the test just doesn't
881 /// apply to. The classical example involves wildcards:
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)
893 /// In that case, after we test on `x`, there are 2 overlapping candidate
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
899 /// Here, the traditional "decision tree" method would generate 2
900 /// separate code-paths for the 2 separate cases.
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:
908 /// match (var0, var1, var2, var3, ..) {
909 /// (true, _, _, false, true, ...) => false,
910 /// (_, true, true, false, _, ...) => false,
911 /// (false, _, false, false, _, ...) => false,
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.
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:
927 /// "foo" if foo_guard => ...,
928 /// "bar" if bar_guard => ...,
929 /// "baz" if baz_guard => ...,
934 /// Here we first test the match-pair `x @ "foo"`, which is an `Eq` test.
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
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.
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).
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.
960 /// In addition to avoiding exponential-time blowups, this algorithm
961 /// also has nice property that each guard and arm is only generated
963 fn test_candidates<'pat>(
966 arm_blocks: &mut ArmBlocks,
967 candidates: &[Candidate<'pat, 'tcx>],
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);
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
980 TestKind::SwitchInt {
985 for candidate in candidates.iter() {
986 if !self.add_cases_to_switch(
1001 for candidate in candidates.iter() {
1002 if !self.add_variants_to_switch(&match_pair.place, candidate, variants) {
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)
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.
1020 "match_candidates: test={:?} match_pair={:?}",
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();
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
1033 self.sort_candidate(&match_pair.place, &test, c, &mut target_candidates)
1036 assert!(tested_candidates > 0); // at least the last candidate ought to be tested
1037 debug!("tested_candidates: {}", tested_candidates);
1039 "untested_candidates: {}",
1040 candidates.len() - tested_candidates
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
1047 let otherwise: Vec<_> = target_blocks
1049 .zip(target_candidates)
1050 .flat_map(|(target_block, target_candidates)| {
1051 self.match_candidates(
1061 (otherwise, tested_candidates)
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.
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
1076 fn bind_and_guard_matched_candidate<'pat>(
1078 mut block: BasicBlock,
1079 arm_blocks: &mut ArmBlocks,
1080 candidate: Candidate<'pat, 'tcx>,
1081 ) -> Option<BasicBlock> {
1083 "bind_and_guard_matched_candidate(block={:?}, candidate={:?})",
1087 debug_assert!(candidate.match_pairs.is_empty());
1089 self.ascribe_types(block, &candidate.ascriptions);
1091 let arm_block = arm_blocks.blocks[candidate.arm_index];
1092 let candidate_source_info = self.source_info(candidate.span);
1096 candidate_source_info,
1097 TerminatorKind::Goto {
1098 target: candidate.pre_binding_block,
1102 block = self.cfg.start_new_block();
1104 candidate.pre_binding_block,
1105 candidate_source_info,
1106 TerminatorKind::FalseEdges {
1108 imaginary_targets: vec![candidate.next_candidate_pre_binding_block],
1112 // rust-lang/rust#27282: The `autoref` business deserves some
1113 // explanation here.
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.
1125 // So an input like:
1128 // let place = Foo::new();
1129 // match place { foo if inspect(foo)
1130 // => feed(foo), ... }
1133 // will be treated as if it were really something like:
1136 // let place = Foo::new();
1137 // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
1138 // => { let tmp2 = place; feed(tmp2) }, ... }
1140 // And an input like:
1143 // let place = Foo::new();
1144 // match place { ref mut foo if inspect(foo)
1145 // => feed(foo), ... }
1148 // will be treated as if it were really something like:
1151 // let place = Foo::new();
1152 // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
1153 // => { let tmp2 = &mut place; feed(tmp2) }, ... }
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.)
1166 // Implementation notes (under assumption `autoref` is true).
1168 // To encode the distinction above, we must inject the
1169 // temporaries `tmp1` and `tmp2`.
1171 // There are two cases of interest: binding by-value, and binding by-ref.
1173 // 1. Binding by-value: Things are simple.
1175 // * Establishing `tmp1` creates a reference into the
1176 // matched place. This code is emitted by
1177 // bind_matched_candidate_for_guard.
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.
1185 // 2. Binding by-reference: Things are tricky.
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).
1194 let autoref = self.hir
1196 .all_pat_vars_are_implicit_refs_within_guards();
1197 if let Some(guard) = candidate.guard {
1199 self.bind_matched_candidate_for_guard(
1201 candidate.pat_index,
1202 &candidate.bindings,
1204 let guard_frame = GuardFrame {
1208 .map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode))
1211 debug!("Entering guard building context: {:?}", guard_frame);
1212 self.guard_context.push(guard_frame);
1214 self.bind_matched_candidate_for_arm_body(block, &candidate.bindings);
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),
1222 let source_info = self.source_info(guard.span);
1223 let cond = unpack!(block = self.as_local_operand(block, guard));
1225 let guard_frame = self.guard_context.pop().unwrap();
1227 "Exiting guard building context with locals: {:?}",
1232 let false_edge_block = self.cfg.start_new_block();
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
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:
1246 // (mut x, 1) | (2, mut x) if { true } => { ... }
1247 // ... // ^^^^^^^ (this is `arm_block`)
1251 // would yield a `arm_block` something like:
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
1259 // and that is clearly not correct.
1260 let post_guard_block = self.cfg.start_new_block();
1264 TerminatorKind::if_(self.hir.tcx(), cond, post_guard_block, false_edge_block),
1268 self.bind_matched_candidate_for_arm_body(post_guard_block, &candidate.bindings);
1274 TerminatorKind::Goto { target: arm_block },
1277 let otherwise = self.cfg.start_new_block();
1282 TerminatorKind::FalseEdges {
1283 real_target: otherwise,
1284 imaginary_targets: vec![candidate.next_candidate_pre_binding_block],
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);
1295 candidate_source_info,
1296 TerminatorKind::Goto { target: arm_block },
1302 /// Append `AscribeUserType` statements onto the end of `block`
1303 /// for each ascription
1304 fn ascribe_types<'pat>(
1307 ascriptions: &[Ascription<'tcx>],
1309 for ascription in ascriptions {
1310 let source_info = self.source_info(ascription.span);
1313 "adding user ascription at span {:?} of place {:?} and {:?}",
1323 kind: StatementKind::AscribeUserType(
1324 ascription.source.clone(),
1325 ty::Variance::Covariant,
1326 box ascription.user_ty.user_ty(),
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(
1339 bindings: &[Binding<'tcx>],
1342 "bind_matched_candidate_for_guard(block={:?}, pat_index={:?}, bindings={:?})",
1343 block, pat_index, bindings
1346 // Assign each of the bindings. Since we are binding for a
1347 // guard expression, this will never trigger moves out of the
1349 let re_empty = self.hir.tcx().types.re_empty;
1350 for binding in bindings {
1351 let source_info = self.source_info(binding.span);
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
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());
1367 .push_assign(block, source_info, &ref_for_guard, rvalue);
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.
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.
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(
1387 ValWithinGuard(pat_index),
1389 self.schedule_drop_for_binding(
1392 ValWithinGuard(pat_index),
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 {
1403 | BorrowKind::Shallow
1404 | BorrowKind::Unique => borrow_kind,
1405 BorrowKind::Mut { .. } => BorrowKind::Mut {
1406 allow_two_phase_borrow: true,
1409 let rvalue = Rvalue::Ref(region, borrow_kind, binding.source.clone());
1411 .push_assign(block, source_info, &val_for_guard, rvalue);
1412 let rvalue = Rvalue::Ref(region, BorrowKind::Shared, val_for_guard);
1414 .push_assign(block, source_info, &ref_for_guard, rvalue);
1420 fn bind_matched_candidate_for_arm_body(
1423 bindings: &[Binding<'tcx>],
1426 "bind_matched_candidate_for_arm_body(block={:?}, bindings={:?}",
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);
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()))
1440 BindingMode::ByRef(region, borrow_kind) => {
1441 Rvalue::Ref(region, borrow_kind, binding.source.clone())
1444 self.cfg.push_assign(block, source_info, &local, rvalue);
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`.
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).
1465 source_info: SourceInfo,
1466 visibility_scope: SourceScope,
1467 mutability: Mutability,
1470 num_patterns: usize,
1473 user_var_ty: Option<(PatternTypeAnnotation<'tcx>, Span)>,
1474 has_guard: ArmHasGuard,
1475 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
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
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()),
1489 let local = LocalDecl::<'tcx> {
1492 user_ty: user_var_ty.map(|(pat_ty, span)|(pat_ty.user_ty(), span)),
1497 is_block_tail: None,
1498 is_user_variable: Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
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.
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,
1519 vals_for_guard.push(val_for_guard_idx);
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),
1529 // FIXME: should these secretly injected ref_for_guard's be marked as `internal`?
1531 is_block_tail: None,
1532 is_user_variable: Some(ClearCrossCrate::Set(BindingForm::RefForGuard)),
1534 LocalsForNode::ForGuard {
1540 LocalsForNode::One(for_arm_body)
1542 debug!("declare_binding: vars={:?}", locals);
1543 self.var_indices.insert(var_id, locals);
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>(
1550 pre_binding_blocks: &[(BasicBlock, Span)],
1551 fake_borrows: FxHashMap<Place<'tcx>, BorrowKind>,
1552 source_info: SourceInfo,
1553 start_block: BasicBlock,
1555 let tcx = self.hir.tcx();
1557 debug!("add_fake_borrows pre_binding_blocks = {:?}, fake_borrows = {:?}",
1558 pre_binding_blocks, fake_borrows);
1560 let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len());
1562 // Insert a Shallow borrow of the prefixes of any fake borrows.
1563 for (place, borrow_kind) in fake_borrows
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));
1574 prefix_cursor = base;
1578 all_fake_borrows.push((place, borrow_kind));
1581 // Deduplicate and ensure a deterministic order.
1582 all_fake_borrows.sort();
1583 all_fake_borrows.dedup();
1585 debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);
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());
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(
1599 &borrowed_input_temp,
1602 borrowed_input_temps.push(borrowed_input_temp);
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
1610 // Another option would be to make our own block and add our own false
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,