1 //! Code related to match expressions. These are sufficiently complex to
2 //! warrant their own module and submodules. :) This main module includes the
3 //! high-level algorithm, the submodules contain the details.
5 //! This also includes code for pattern bindings in `let` statements and
6 //! function parameters.
8 use crate::build::expr::as_place::PlaceBuilder;
9 use crate::build::scope::DropKind;
10 use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
11 use crate::build::{BlockAnd, BlockAndExtension, Builder};
12 use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
13 use rustc_data_structures::{
14 fx::{FxHashSet, FxIndexMap},
15 stack::ensure_sufficient_stack,
18 use rustc_index::bit_set::BitSet;
19 use rustc_middle::middle::region;
20 use rustc_middle::mir::*;
21 use rustc_middle::thir::{self, *};
22 use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
23 use rustc_span::symbol::Symbol;
24 use rustc_span::{BytePos, Pos, Span};
25 use rustc_target::abi::VariantIdx;
26 use smallvec::{smallvec, SmallVec};
28 // helper functions, broken out by category:
33 use std::borrow::Borrow;
34 use std::convert::TryFrom;
37 impl<'a, 'tcx> Builder<'a, 'tcx> {
38 pub(crate) fn then_else_break(
40 mut block: BasicBlock,
42 temp_scope_override: Option<region::Scope>,
43 break_scope: region::Scope,
44 variable_scope_span: Span,
47 let expr_span = expr.span;
50 ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
51 let lhs_then_block = unpack!(this.then_else_break(
59 let rhs_then_block = unpack!(this.then_else_break(
69 ExprKind::Scope { region_scope, lint_level, value } => {
70 let region_scope = (region_scope, this.source_info(expr_span));
71 this.in_scope(region_scope, lint_level, |this| {
81 ExprKind::Let { expr, ref pat } => {
82 this.lower_let_expr(block, &this.thir[expr], pat, break_scope, variable_scope_span)
85 let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope());
86 let mutability = Mutability::Mut;
88 unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability));
89 let operand = Operand::Move(Place::from(place));
91 let then_block = this.cfg.start_new_block();
92 let else_block = this.cfg.start_new_block();
93 let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block);
95 let source_info = this.source_info(expr_span);
96 this.cfg.terminate(block, source_info, term);
97 this.break_for_else(else_block, break_scope, source_info);
104 /// Generates MIR for a `match` expression.
106 /// The MIR that we generate for a match looks like this.
111 /// [ 1. Evaluate Scrutinee (expression being matched on) ]
112 /// [ (fake read of scrutinee) ]
114 /// [ 2. Decision tree -- check discriminants ] <--------+
116 /// | (once a specific arm is chosen) |
118 /// [pre_binding_block] [otherwise_block]
120 /// [ 3. Create "guard bindings" for arm ] |
121 /// [ (create fake borrows) ] |
123 /// [ 4. Execute guard code ] |
124 /// [ (read fake borrows) ] --(guard is false)-----------+
126 /// | (guard results in true)
128 /// [ 5. Create real bindings and execute arm ]
133 /// All of the different arms have been stacked on top of each other to
134 /// simplify the diagram. For an arm with no guard the blocks marked 3 and
135 /// 4 and the fake borrows are omitted.
137 /// We generate MIR in the following steps:
139 /// 1. Evaluate the scrutinee and add the fake read of it ([Builder::lower_scrutinee]).
140 /// 2. Create the decision tree ([Builder::lower_match_tree]).
141 /// 3. Determine the fake borrows that are needed from the places that were
142 /// matched against and create the required temporaries for them
143 /// ([Builder::calculate_fake_borrows]).
144 /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]).
148 /// We don't want to have the exact structure of the decision tree be
149 /// visible through borrow checking. False edges ensure that the CFG as
150 /// seen by borrow checking doesn't encode this. False edges are added:
152 /// * From each pre-binding block to the next pre-binding block.
153 /// * From each otherwise block to the next pre-binding block.
156 destination: Place<'tcx>,
158 mut block: BasicBlock,
159 scrutinee: &Expr<'tcx>,
162 let scrutinee_span = scrutinee.span;
163 let scrutinee_place =
164 unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,));
166 let mut arm_candidates = self.create_match_candidates(scrutinee_place.clone(), &arms);
168 let match_has_guard = arms.iter().copied().any(|arm| self.thir[arm].guard.is_some());
170 arm_candidates.iter_mut().map(|(_, candidate)| candidate).collect::<Vec<_>>();
172 let match_start_span = span.shrink_to_lo().to(scrutinee.span);
174 let fake_borrow_temps = self.lower_match_tree(
182 self.lower_match_arms(
187 self.source_info(span),
192 /// Evaluate the scrutinee and add the fake read of it.
195 mut block: BasicBlock,
196 scrutinee: &Expr<'tcx>,
197 scrutinee_span: Span,
198 ) -> BlockAnd<PlaceBuilder<'tcx>> {
199 let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee));
200 // Matching on a `scrutinee_place` with an uninhabited type doesn't
201 // generate any memory reads by itself, and so if the place "expression"
202 // contains unsafe operations like raw pointer dereferences or union
203 // field projections, we wouldn't know to require an `unsafe` block
204 // around a `match` equivalent to `std::intrinsics::unreachable()`.
205 // See issue #47412 for this hole being discovered in the wild.
207 // HACK(eddyb) Work around the above issue by adding a dummy inspection
208 // of `scrutinee_place`, specifically by applying `ReadForMatch`.
210 // NOTE: ReadForMatch also checks that the scrutinee is initialized.
211 // This is currently needed to not allow matching on an uninitialized,
212 // uninhabited value. If we get never patterns, those will check that
213 // the place is initialized, and so this read would only be used to
215 let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
216 let source_info = self.source_info(scrutinee_span);
218 if let Ok(scrutinee_builder) =
219 scrutinee_place_builder.clone().try_upvars_resolved(self.tcx, self.typeck_results)
221 let scrutinee_place = scrutinee_builder.into_place(self.tcx, self.typeck_results);
222 self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
225 block.and(scrutinee_place_builder)
228 /// Create the initial `Candidate`s for a `match` expression.
229 fn create_match_candidates<'pat>(
231 scrutinee: PlaceBuilder<'tcx>,
233 ) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)>
237 // Assemble a list of candidates: there is one candidate per pattern,
238 // which means there may be more than one candidate *per arm*.
242 let arm = &self.thir[arm];
243 let arm_has_guard = arm.guard.is_some();
244 let arm_candidate = Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard);
250 /// Create the decision tree for the match expression, starting from `block`.
252 /// Modifies `candidates` to store the bindings and type ascriptions for
255 /// Returns the places that need fake borrows because we bind or test them.
256 fn lower_match_tree<'pat>(
259 scrutinee_span: Span,
260 match_start_span: Span,
261 match_has_guard: bool,
262 candidates: &mut [&mut Candidate<'pat, 'tcx>],
263 ) -> Vec<(Place<'tcx>, Local)> {
264 // The set of places that we are creating fake borrows of. If there are
265 // no match guards then we don't need any fake borrows, so don't track
267 let mut fake_borrows = match_has_guard.then(FxHashSet::default);
269 let mut otherwise = None;
271 // This will generate code to test scrutinee_place and
272 // branch to the appropriate arm block
273 self.match_candidates(
282 if let Some(otherwise_block) = otherwise {
283 // See the doc comment on `match_candidates` for why we may have an
284 // otherwise block. Match checking will ensure this is actually
286 let source_info = self.source_info(scrutinee_span);
287 self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
290 // Link each leaf candidate to the `pre_binding_block` of the next one.
291 let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
293 for candidate in candidates {
294 candidate.visit_leaves(|leaf_candidate| {
295 if let Some(ref mut prev) = previous_candidate {
296 prev.next_candidate_pre_binding_block = leaf_candidate.pre_binding_block;
298 previous_candidate = Some(leaf_candidate);
302 if let Some(ref borrows) = fake_borrows {
303 self.calculate_fake_borrows(borrows, scrutinee_span)
309 /// Lower the bindings, guards and arm bodies of a `match` expression.
311 /// The decision tree should have already been created
312 /// (by [Builder::lower_match_tree]).
314 /// `outer_source_info` is the SourceInfo for the whole match.
317 destination: Place<'tcx>,
318 scrutinee_place_builder: PlaceBuilder<'tcx>,
319 scrutinee_span: Span,
320 arm_candidates: Vec<(&'_ Arm<'tcx>, Candidate<'_, 'tcx>)>,
321 outer_source_info: SourceInfo,
322 fake_borrow_temps: Vec<(Place<'tcx>, Local)>,
324 let arm_end_blocks: Vec<_> = arm_candidates
326 .map(|(arm, candidate)| {
327 debug!("lowering arm {:?}\ncandidate = {:?}", arm, candidate);
329 let arm_source_info = self.source_info(arm.span);
330 let arm_scope = (arm.scope, arm_source_info);
331 let match_scope = self.local_scope();
332 self.in_scope(arm_scope, arm.lint_level, |this| {
333 // `try_upvars_resolved` may fail if it is unable to resolve the given
334 // `PlaceBuilder` inside a closure. In this case, we don't want to include
335 // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
336 // if the only match arm is a wildcard (`_`).
341 // match foo { _ => () };
344 let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None;
345 let scrutinee_place: Place<'tcx>;
346 if let Ok(scrutinee_builder) = scrutinee_place_builder
348 .try_upvars_resolved(this.tcx, this.typeck_results)
351 scrutinee_builder.into_place(this.tcx, this.typeck_results);
352 opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
354 let scope = this.declare_bindings(
358 ArmHasGuard(arm.guard.is_some()),
362 let arm_block = this.bind_pattern(
373 if let Some(source_scope) = scope {
374 this.source_scope = source_scope;
377 this.expr_into_dest(destination, arm_block, &&this.thir[arm.body])
382 // all the arm blocks will rejoin here
383 let end_block = self.cfg.start_new_block();
385 let end_brace = self.source_info(
386 outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
388 for arm_block in arm_end_blocks {
389 let block = &self.cfg.basic_blocks[arm_block.0];
390 let last_location = block.statements.last().map(|s| s.source_info);
392 self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block);
395 self.source_scope = outer_source_info.scope;
400 /// Binds the variables and ascribes types for a given `match` arm or
403 /// Also check if the guard matches, if it's provided.
404 /// `arm_scope` should be `Some` if and only if this is called for a
408 outer_source_info: SourceInfo,
409 candidate: Candidate<'_, 'tcx>,
410 guard: Option<&Guard<'tcx>>,
411 fake_borrow_temps: &Vec<(Place<'tcx>, Local)>,
412 scrutinee_span: Span,
413 arm_span: Option<Span>,
414 arm_scope: Option<region::Scope>,
415 match_scope: Option<region::Scope>,
417 if candidate.subcandidates.is_empty() {
418 // Avoid generating another `BasicBlock` when we only have one
420 self.bind_and_guard_matched_candidate(
431 // It's helpful to avoid scheduling drops multiple times to save
432 // drop elaboration from having to clean up the extra drops.
434 // If we are in a `let` then we only schedule drops for the first
437 // If we're in a `match` arm then we could have a case like so:
439 // Ok(x) | Err(x) if return => { /* ... */ }
441 // In this case we don't want a drop of `x` scheduled when we
442 // return: it isn't bound by move until right before enter the arm.
443 // To handle this we instead unschedule it's drop after each time
444 // we lower the guard.
445 let target_block = self.cfg.start_new_block();
446 let mut schedule_drops = true;
447 // We keep a stack of all of the bindings and type ascriptions
448 // from the parent candidates that we visit, that also need to
449 // be bound for each candidate.
453 &mut |leaf_candidate, parent_bindings| {
454 if let Some(arm_scope) = arm_scope {
455 self.clear_top_scope(arm_scope);
457 let binding_end = self.bind_and_guard_matched_candidate(
467 if arm_scope.is_none() {
468 schedule_drops = false;
470 self.cfg.goto(binding_end, outer_source_info, target_block);
472 |inner_candidate, parent_bindings| {
473 parent_bindings.push((inner_candidate.bindings, inner_candidate.ascriptions));
474 inner_candidate.subcandidates.into_iter()
477 parent_bindings.pop();
485 pub(super) fn expr_into_pattern(
487 mut block: BasicBlock,
488 irrefutable_pat: Pat<'tcx>,
489 initializer: &Expr<'tcx>,
491 match *irrefutable_pat.kind {
492 // Optimize the case of `let x = ...` to write directly into `x`
493 PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => {
495 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
496 unpack!(block = self.expr_into_dest(place, block, initializer));
498 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
499 let source_info = self.source_info(irrefutable_pat.span);
500 self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place);
502 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
506 // Optimize the case of `let x: T = ...` to write directly
507 // into `x` and then require that `T == typeof(x)`.
509 // Weirdly, this is needed to prevent the
510 // `intrinsic-move-val.rs` test case from crashing. That
511 // test works with uninitialized values in a rather
512 // dubious way, so it may be that the test is kind of
514 PatKind::AscribeUserType {
518 box PatKind::Binding {
519 mode: BindingMode::ByValue,
527 thir::Ascription { user_ty: pat_ascription_ty, variance: _, user_ty_span },
530 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
531 unpack!(block = self.expr_into_dest(place, block, initializer));
533 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
534 let pattern_source_info = self.source_info(irrefutable_pat.span);
535 let cause_let = FakeReadCause::ForLet(None);
536 self.cfg.push_fake_read(block, pattern_source_info, cause_let, place);
538 let ty_source_info = self.source_info(user_ty_span);
539 let user_ty = pat_ascription_ty.user_ty(
540 &mut self.canonical_user_type_annotations,
541 place.ty(&self.local_decls, self.tcx).ty,
547 source_info: ty_source_info,
548 kind: StatementKind::AscribeUserType(
549 Box::new((place, user_ty)),
550 // We always use invariant as the variance here. This is because the
551 // variance field from the ascription refers to the variance to use
552 // when applying the type to the value being matched, but this
553 // ascription applies rather to the type of the binding. e.g., in this
560 // We are creating an ascription that defines the type of `x` to be
561 // exactly `T` (i.e., with invariance). The variance field, in
562 // contrast, is intended to be used to relate `T` to the type of
564 ty::Variance::Invariant,
569 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
574 let place_builder = unpack!(block = self.as_place_builder(block, initializer));
575 self.place_into_pattern(block, irrefutable_pat, place_builder, true)
580 crate fn place_into_pattern(
583 irrefutable_pat: Pat<'tcx>,
584 initializer: PlaceBuilder<'tcx>,
585 set_match_place: bool,
587 let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false);
588 let fake_borrow_temps = self.lower_match_tree(
590 irrefutable_pat.span,
591 irrefutable_pat.span,
593 &mut [&mut candidate],
595 // For matches and function arguments, the place that is being matched
596 // can be set when creating the variables. But the place for
597 // let PATTERN = ... might not even exist until we do the assignment.
598 // so we set it here instead.
600 let mut candidate_ref = &candidate;
601 while let Some(next) = {
602 for binding in &candidate_ref.bindings {
603 let local = self.var_local_id(binding.var_id, OutsideGuard);
605 let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
606 VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
607 )))) = self.local_decls[local].local_info else {
608 bug!("Let binding to non-user variable.")
610 // `try_upvars_resolved` may fail if it is unable to resolve the given
611 // `PlaceBuilder` inside a closure. In this case, we don't want to include
612 // a scrutinee place. `scrutinee_place_builder` will fail for destructured
613 // assignments. This is because a closure only captures the precise places
614 // that it will read and as a result a closure may not capture the entire
615 // tuple/struct and rather have individual places that will be read in the
621 // let (v1, v2) = foo;
624 if let Ok(match_pair_resolved) =
625 initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results)
627 let place = match_pair_resolved.into_place(self.tcx, self.typeck_results);
628 *match_place = Some(place);
631 // All of the subcandidates should bind the same locals, so we
632 // only visit the first one.
633 candidate_ref.subcandidates.get(0)
635 candidate_ref = next;
640 self.source_info(irrefutable_pat.span),
644 irrefutable_pat.span,
652 /// Declares the bindings of the given patterns and returns the visibility
653 /// scope for the bindings in these patterns, if such a scope had to be
654 /// created. NOTE: Declaring the bindings should always be done in their
656 crate fn declare_bindings(
658 mut visibility_scope: Option<SourceScope>,
661 has_guard: ArmHasGuard,
662 opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
663 ) -> Option<SourceScope> {
664 debug!("declare_bindings: pattern={:?}", pattern);
665 self.visit_primary_bindings(
667 UserTypeProjections::none(),
668 &mut |this, mutability, name, mode, var, span, ty, user_ty| {
669 if visibility_scope.is_none() {
671 Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
673 let source_info = SourceInfo { span, scope: this.source_scope };
674 let visibility_scope = visibility_scope.unwrap();
675 this.declare_binding(
685 opt_match_place.map(|(x, y)| (x.cloned(), y)),
693 crate fn storage_live_binding(
701 let local_id = self.var_local_id(var, for_guard);
702 let source_info = self.source_info(span);
703 self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
704 // Altough there is almost always scope for given variable in corner cases
705 // like #92893 we might get variable with no scope.
706 if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) && schedule_drop{
707 self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
709 Place::from(local_id)
712 crate fn schedule_drop_for_binding(&mut self, var: HirId, span: Span, for_guard: ForGuard) {
713 let local_id = self.var_local_id(var, for_guard);
714 if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) {
715 self.schedule_drop(span, region_scope, local_id, DropKind::Value);
719 /// Visit all of the primary bindings in a patterns, that is, visit the
720 /// leftmost occurrence of each variable bound in a pattern. A variable
721 /// will occur more than once in an or-pattern.
722 pub(super) fn visit_primary_bindings(
725 pattern_user_ty: UserTypeProjections,
738 "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}",
739 pattern, pattern_user_ty
741 match *pattern.kind {
753 f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty.clone());
755 if let Some(subpattern) = subpattern.as_ref() {
756 self.visit_primary_bindings(subpattern, pattern_user_ty, f);
760 PatKind::Array { ref prefix, ref slice, ref suffix }
761 | PatKind::Slice { ref prefix, ref slice, ref suffix } => {
762 let from = u64::try_from(prefix.len()).unwrap();
763 let to = u64::try_from(suffix.len()).unwrap();
764 for subpattern in prefix {
765 self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
767 for subpattern in slice {
768 self.visit_primary_bindings(
770 pattern_user_ty.clone().subslice(from, to),
774 for subpattern in suffix {
775 self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
779 PatKind::Constant { .. } | PatKind::Range { .. } | PatKind::Wild => {}
781 PatKind::Deref { ref subpattern } => {
782 self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
785 PatKind::AscribeUserType {
787 ascription: thir::Ascription { ref user_ty, user_ty_span, variance: _ },
789 // This corresponds to something like
792 // let A::<'a>(_): A<'static> = ...;
795 // Note that the variance doesn't apply here, as we are tracking the effect
796 // of `user_ty` on any bindings contained with subpattern.
797 let annotation = CanonicalUserTypeAnnotation {
799 user_ty: user_ty.user_ty,
800 inferred_ty: subpattern.ty,
802 let projection = UserTypeProjection {
803 base: self.canonical_user_type_annotations.push(annotation),
806 let subpattern_user_ty = pattern_user_ty.push_projection(&projection, user_ty_span);
807 self.visit_primary_bindings(subpattern, subpattern_user_ty, f)
810 PatKind::Leaf { ref subpatterns } => {
811 for subpattern in subpatterns {
812 let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);
813 debug!("visit_primary_bindings: subpattern_user_ty={:?}", subpattern_user_ty);
814 self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
818 PatKind::Variant { adt_def, substs: _, variant_index, ref subpatterns } => {
819 for subpattern in subpatterns {
820 let subpattern_user_ty =
821 pattern_user_ty.clone().variant(adt_def, variant_index, subpattern.field);
822 self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
825 PatKind::Or { ref pats } => {
826 // In cases where we recover from errors the primary bindings
827 // may not all be in the leftmost subpattern. For example in
828 // `let (x | y) = ...`, the primary binding of `y` occurs in
829 // the right subpattern
830 for subpattern in pats {
831 self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f);
839 struct Candidate<'pat, 'tcx> {
840 /// [`Span`] of the original pattern that gave rise to this candidate.
843 /// Whether this `Candidate` has a guard.
846 /// All of these must be satisfied...
847 match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
849 /// ...these bindings established...
850 bindings: Vec<Binding<'tcx>>,
852 /// ...and these types asserted...
853 ascriptions: Vec<Ascription<'tcx>>,
855 /// ...and if this is non-empty, one of these subcandidates also has to match...
856 subcandidates: Vec<Candidate<'pat, 'tcx>>,
858 /// ...and the guard must be evaluated; if it's `false` then branch to `otherwise_block`.
859 otherwise_block: Option<BasicBlock>,
861 /// The block before the `bindings` have been established.
862 pre_binding_block: Option<BasicBlock>,
863 /// The pre-binding block of the next candidate.
864 next_candidate_pre_binding_block: Option<BasicBlock>,
867 impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
868 fn new(place: PlaceBuilder<'tcx>, pattern: &'pat Pat<'tcx>, has_guard: bool) -> Self {
872 match_pairs: smallvec![MatchPair { place, pattern }],
873 bindings: Vec::new(),
874 ascriptions: Vec::new(),
875 subcandidates: Vec::new(),
876 otherwise_block: None,
877 pre_binding_block: None,
878 next_candidate_pre_binding_block: None,
882 /// Visit the leaf candidates (those with no subcandidates) contained in
884 fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
888 &mut move |c, _| visit_leaf(c),
889 move |c, _| c.subcandidates.iter_mut(),
895 /// A depth-first traversal of the `Candidate` and all of its recursive
897 fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>(
900 visit_leaf: &mut impl FnMut(C, &mut T),
901 get_children: impl Copy + Fn(C, &mut T) -> I,
902 complete_children: impl Copy + Fn(&mut T),
904 C: Borrow<Candidate<'pat, 'tcx>>,
905 I: Iterator<Item = C>,
907 if candidate.borrow().subcandidates.is_empty() {
908 visit_leaf(candidate, context)
910 for child in get_children(candidate, context) {
911 traverse_candidate(child, context, visit_leaf, get_children, complete_children);
913 complete_children(context)
917 #[derive(Clone, Debug)]
918 struct Binding<'tcx> {
922 binding_mode: BindingMode,
925 /// Indicates that the type of `source` must be a subtype of the
926 /// user-given type `user_ty`; this is basically a no-op but can
927 /// influence region inference.
928 #[derive(Clone, Debug)]
929 struct Ascription<'tcx> {
932 user_ty: PatTyProj<'tcx>,
933 variance: ty::Variance,
936 #[derive(Clone, Debug)]
937 crate struct MatchPair<'pat, 'tcx> {
939 place: PlaceBuilder<'tcx>,
941 // ... must match this pattern.
942 pattern: &'pat Pat<'tcx>,
945 /// See [`Test`] for more.
946 #[derive(Clone, Debug, PartialEq)]
947 enum TestKind<'tcx> {
948 /// Test what enum variant a value is.
950 /// The enum type being tested.
951 adt_def: ty::AdtDef<'tcx>,
952 /// The set of variants that we should create a branch for. We also
953 /// create an additional "otherwise" case.
954 variants: BitSet<VariantIdx>,
957 /// Test what value an integer, `bool`, or `char` has.
959 /// The type of the value that we're testing.
961 /// The (ordered) set of values that we test for.
963 /// For integers and `char`s we create a branch to each of the values in
964 /// `options`, as well as an "otherwise" branch for all other values, even
965 /// in the (rare) case that `options` is exhaustive.
967 /// For `bool` we always generate two edges, one for `true` and one for
969 options: FxIndexMap<ty::Const<'tcx>, u128>,
972 /// Test for equality with value, possibly after an unsizing coercion to
975 value: ty::Const<'tcx>,
976 // Integer types are handled by `SwitchInt`, and constants with ADT
977 // types are converted back into patterns, so this can only be `&str`,
978 // `&[T]`, `f32` or `f64`.
982 /// Test whether the value falls within an inclusive or exclusive range
983 Range(PatRange<'tcx>),
985 /// Test that the length of the slice is equal to `len`.
986 Len { len: u64, op: BinOp },
989 /// A test to perform to determine which [`Candidate`] matches a value.
991 /// [`Test`] is just the test to perform; it does not include the value
994 crate struct Test<'tcx> {
996 kind: TestKind<'tcx>,
999 /// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
1000 /// a match arm has a guard expression attached to it.
1001 #[derive(Copy, Clone, Debug)]
1002 crate struct ArmHasGuard(crate bool);
1004 ///////////////////////////////////////////////////////////////////////////
1005 // Main matching algorithm
1007 impl<'a, 'tcx> Builder<'a, 'tcx> {
1008 /// The main match algorithm. It begins with a set of candidates
1009 /// `candidates` and has the job of generating code to determine
1010 /// which of these candidates, if any, is the correct one. The
1011 /// candidates are sorted such that the first item in the list
1012 /// has the highest priority. When a candidate is found to match
1013 /// the value, we will set and generate a branch to the appropriate
1014 /// pre-binding block.
1016 /// If we find that *NONE* of the candidates apply, we branch to the
1017 /// `otherwise_block`, setting it to `Some` if required. In principle, this
1018 /// means that the input list was not exhaustive, though at present we
1019 /// sometimes are not smart enough to recognize all exhaustive inputs.
1021 /// It might be surprising that the input can be non-exhaustive.
1022 /// Indeed, initially, it is not, because all matches are
1023 /// exhaustive in Rust. But during processing we sometimes divide
1024 /// up the list of candidates and recurse with a non-exhaustive
1025 /// list. This is important to keep the size of the generated code
1026 /// under control. See [`Builder::test_candidates`] for more details.
1028 /// If `fake_borrows` is `Some`, then places which need fake borrows
1029 /// will be added to it.
1031 /// For an example of a case where we set `otherwise_block`, even for an
1032 /// exhaustive match, consider:
1035 /// # fn foo(x: (bool, bool)) {
1037 /// (true, true) => (),
1038 /// (_, false) => (),
1039 /// (false, true) => (),
1044 /// For this match, we check if `x.0` matches `true` (for the first
1045 /// arm). If it doesn't match, we check `x.1`. If `x.1` is `true` we check
1046 /// if `x.0` matches `false` (for the third arm). In the (impossible at
1047 /// runtime) case when `x.0` is now `true`, we branch to
1048 /// `otherwise_block`.
1049 fn match_candidates<'pat>(
1052 scrutinee_span: Span,
1053 start_block: BasicBlock,
1054 otherwise_block: &mut Option<BasicBlock>,
1055 candidates: &mut [&mut Candidate<'pat, 'tcx>],
1056 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1059 "matched_candidate(span={:?}, candidates={:?}, start_block={:?}, otherwise_block={:?})",
1060 span, candidates, start_block, otherwise_block,
1063 // Start by simplifying candidates. Once this process is complete, all
1064 // the match pairs which remain require some form of test, whether it
1065 // be a switch or pattern comparison.
1066 let mut split_or_candidate = false;
1067 for candidate in &mut *candidates {
1068 split_or_candidate |= self.simplify_candidate(candidate);
1071 ensure_sufficient_stack(|| {
1072 if split_or_candidate {
1073 // At least one of the candidates has been split into subcandidates.
1074 // We need to change the candidate list to include those.
1075 let mut new_candidates = Vec::new();
1077 for candidate in candidates {
1078 candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
1080 self.match_simplified_candidates(
1085 &mut *new_candidates,
1089 self.match_simplified_candidates(
1101 fn match_simplified_candidates(
1104 scrutinee_span: Span,
1105 start_block: BasicBlock,
1106 otherwise_block: &mut Option<BasicBlock>,
1107 candidates: &mut [&mut Candidate<'_, 'tcx>],
1108 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1110 // The candidates are sorted by priority. Check to see whether the
1111 // higher priority candidates (and hence at the front of the slice)
1112 // have satisfied all their match pairs.
1113 let fully_matched = candidates.iter().take_while(|c| c.match_pairs.is_empty()).count();
1114 debug!("match_candidates: {:?} candidates fully matched", fully_matched);
1115 let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
1117 let block = if !matched_candidates.is_empty() {
1118 let otherwise_block =
1119 self.select_matched_candidates(matched_candidates, start_block, fake_borrows);
1121 if let Some(last_otherwise_block) = otherwise_block {
1122 last_otherwise_block
1124 // Any remaining candidates are unreachable.
1125 if unmatched_candidates.is_empty() {
1128 self.cfg.start_new_block()
1134 // If there are no candidates that still need testing, we're
1135 // done. Since all matches are exhaustive, execution should
1136 // never reach this point.
1137 if unmatched_candidates.is_empty() {
1138 let source_info = self.source_info(span);
1139 if let Some(otherwise) = *otherwise_block {
1140 self.cfg.goto(block, source_info, otherwise);
1142 *otherwise_block = Some(block);
1147 // Test for the remaining candidates.
1148 self.test_candidates_with_or(
1151 unmatched_candidates,
1158 /// Link up matched candidates.
1160 /// For example, if we have something like this:
1162 /// ```ignore (illustrative)
1164 /// Some(x) if cond1 => ...
1166 /// Some(x) if cond2 => ...
1170 /// We generate real edges from:
1172 /// * `start_block` to the [pre-binding block] of the first pattern,
1173 /// * the [otherwise block] of the first pattern to the second pattern,
1174 /// * the [otherwise block] of the third pattern to a block with an
1175 /// [`Unreachable` terminator](TerminatorKind::Unreachable).
1177 /// In addition, we add fake edges from the otherwise blocks to the
1178 /// pre-binding block of the next candidate in the original set of
1181 /// [pre-binding block]: Candidate::pre_binding_block
1182 /// [otherwise block]: Candidate::otherwise_block
1183 fn select_matched_candidates(
1185 matched_candidates: &mut [&mut Candidate<'_, 'tcx>],
1186 start_block: BasicBlock,
1187 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1188 ) -> Option<BasicBlock> {
1190 !matched_candidates.is_empty(),
1191 "select_matched_candidates called with no candidates",
1194 matched_candidates.iter().all(|c| c.subcandidates.is_empty()),
1195 "subcandidates should be empty in select_matched_candidates",
1198 // Insert a borrows of prefixes of places that are bound and are
1199 // behind a dereference projection.
1201 // These borrows are taken to avoid situations like the following:
1204 // _ if { x = &[0]; false } => (),
1205 // y => (), // Out of bounds array access!
1209 // // y is bound by reference in the guard and then by copy in the
1210 // // arm, so y is 2 in the arm!
1211 // y if { y == 1 && (x = &2) == () } => y,
1214 if let Some(fake_borrows) = fake_borrows {
1215 for Binding { source, .. } in
1216 matched_candidates.iter().flat_map(|candidate| &candidate.bindings)
1219 source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref)
1221 let proj_base = &source.projection[..i];
1223 fake_borrows.insert(Place {
1224 local: source.local,
1225 projection: self.tcx.intern_place_elems(proj_base),
1231 let fully_matched_with_guard = matched_candidates
1233 .position(|c| !c.has_guard)
1234 .unwrap_or(matched_candidates.len() - 1);
1236 let (reachable_candidates, unreachable_candidates) =
1237 matched_candidates.split_at_mut(fully_matched_with_guard + 1);
1239 let mut next_prebinding = start_block;
1241 for candidate in reachable_candidates.iter_mut() {
1242 assert!(candidate.otherwise_block.is_none());
1243 assert!(candidate.pre_binding_block.is_none());
1244 candidate.pre_binding_block = Some(next_prebinding);
1245 if candidate.has_guard {
1246 // Create the otherwise block for this candidate, which is the
1247 // pre-binding block for the next candidate.
1248 next_prebinding = self.cfg.start_new_block();
1249 candidate.otherwise_block = Some(next_prebinding);
1254 "match_candidates: add pre_binding_blocks for unreachable {:?}",
1255 unreachable_candidates,
1257 for candidate in unreachable_candidates {
1258 assert!(candidate.pre_binding_block.is_none());
1259 candidate.pre_binding_block = Some(self.cfg.start_new_block());
1262 reachable_candidates.last_mut().unwrap().otherwise_block
1265 /// Tests a candidate where there are only or-patterns left to test, or
1266 /// forwards to [Builder::test_candidates].
1268 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1276 /// +----------------------------------------+------------------------------------+
1279 /// [ P matches ] [ Q matches ] [ otherwise ]
1282 /// [ match R, S ] [ match R, S ] |
1284 /// +--------------+------------+ +--------------+------------+ |
1287 /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] |
1289 /// +--------------+------------|------------+--------------+ | |
1291 /// | +----------------------------------------+--------+
1294 /// [ Success ] [ Failure ]
1297 /// In practice there are some complications:
1299 /// * If there's a guard, then the otherwise branch of the first match on
1300 /// `R | S` goes to a test for whether `Q` matches, and the control flow
1301 /// doesn't merge into a single success block until after the guard is
1303 /// * If neither `P` or `Q` has any bindings or type ascriptions and there
1304 /// isn't a match guard, then we create a smaller CFG like:
1308 /// +---------------+------------+
1310 /// [ P matches ] [ Q matches ] [ otherwise ]
1312 /// +---------------+ |
1318 fn test_candidates_with_or(
1321 scrutinee_span: Span,
1322 candidates: &mut [&mut Candidate<'_, 'tcx>],
1324 otherwise_block: &mut Option<BasicBlock>,
1325 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1327 let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap();
1329 // All of the or-patterns have been sorted to the end, so if the first
1330 // pattern is an or-pattern we only have or-patterns.
1331 match *first_candidate.match_pairs[0].pattern.kind {
1332 PatKind::Or { .. } => (),
1334 self.test_candidates(
1346 let match_pairs = mem::take(&mut first_candidate.match_pairs);
1347 first_candidate.pre_binding_block = Some(block);
1349 let mut otherwise = None;
1350 for match_pair in match_pairs {
1351 let PatKind::Or { ref pats } = &*match_pair.pattern.kind else {
1352 bug!("Or-patterns should have been sorted to the end");
1354 let or_span = match_pair.pattern.span;
1355 let place = match_pair.place;
1357 first_candidate.visit_leaves(|leaf_candidate| {
1358 self.test_or_pattern(
1369 let remainder_start = otherwise.unwrap_or_else(|| self.cfg.start_new_block());
1371 self.match_candidates(
1376 remaining_candidates,
1381 fn test_or_pattern<'pat>(
1383 candidate: &mut Candidate<'pat, 'tcx>,
1384 otherwise: &mut Option<BasicBlock>,
1385 pats: &'pat [Pat<'tcx>],
1387 place: PlaceBuilder<'tcx>,
1388 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1390 debug!("test_or_pattern:\ncandidate={:#?}\npats={:#?}", candidate, pats);
1391 let mut or_candidates: Vec<_> = pats
1393 .map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard))
1395 let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
1396 let otherwise = if candidate.otherwise_block.is_some() {
1397 &mut candidate.otherwise_block
1401 self.match_candidates(
1404 candidate.pre_binding_block.unwrap(),
1406 &mut or_candidate_refs,
1409 candidate.subcandidates = or_candidates;
1410 self.merge_trivial_subcandidates(candidate, self.source_info(or_span));
1413 /// Try to merge all of the subcandidates of the given candidate into one.
1414 /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1415 fn merge_trivial_subcandidates(
1417 candidate: &mut Candidate<'_, 'tcx>,
1418 source_info: SourceInfo,
1420 if candidate.subcandidates.is_empty() || candidate.has_guard {
1421 // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
1425 let mut can_merge = true;
1427 // Not `Iterator::all` because we don't want to short-circuit.
1428 for subcandidate in &mut candidate.subcandidates {
1429 self.merge_trivial_subcandidates(subcandidate, source_info);
1431 // FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
1432 can_merge &= subcandidate.subcandidates.is_empty()
1433 && subcandidate.bindings.is_empty()
1434 && subcandidate.ascriptions.is_empty();
1438 let any_matches = self.cfg.start_new_block();
1439 for subcandidate in mem::take(&mut candidate.subcandidates) {
1440 let or_block = subcandidate.pre_binding_block.unwrap();
1441 self.cfg.goto(or_block, source_info, any_matches);
1443 candidate.pre_binding_block = Some(any_matches);
1447 /// This is the most subtle part of the matching algorithm. At
1448 /// this point, the input candidates have been fully simplified,
1449 /// and so we know that all remaining match-pairs require some
1450 /// sort of test. To decide what test to perform, we take the highest
1451 /// priority candidate (the first one in the list, as of January 2021)
1452 /// and extract the first match-pair from the list. From this we decide
1453 /// what kind of test is needed using [`Builder::test`], defined in the
1454 /// [`test` module](mod@test).
1456 /// *Note:* taking the first match pair is somewhat arbitrary, and
1457 /// we might do better here by choosing more carefully what to
1460 /// For example, consider the following possible match-pairs:
1462 /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
1463 /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
1464 /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
1467 /// [`Switch`]: TestKind::Switch
1468 /// [`SwitchInt`]: TestKind::SwitchInt
1469 /// [`Range`]: TestKind::Range
1471 /// Once we know what sort of test we are going to perform, this
1472 /// test may also help us winnow down our candidates. So we walk over
1473 /// the candidates (from high to low priority) and check. This
1474 /// gives us, for each outcome of the test, a transformed list of
1475 /// candidates. For example, if we are testing `x.0`'s variant,
1476 /// and we have a candidate `(x.0 @ Some(v), x.1 @ 22)`,
1477 /// then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)`.
1478 /// Note that the first match-pair is now simpler (and, in fact, irrefutable).
1480 /// But there may also be candidates that the test just doesn't
1481 /// apply to. The classical example involves wildcards:
1484 /// # let (x, y, z) = (true, true, true);
1485 /// match (x, y, z) {
1486 /// (true , _ , true ) => true, // (0)
1487 /// (_ , true , _ ) => true, // (1)
1488 /// (false, false, _ ) => false, // (2)
1489 /// (true , _ , false) => false, // (3)
1494 /// In that case, after we test on `x`, there are 2 overlapping candidate
1497 /// - If the outcome is that `x` is true, candidates 0, 1, and 3
1498 /// - If the outcome is that `x` is false, candidates 1 and 2
1500 /// Here, the traditional "decision tree" method would generate 2
1501 /// separate code-paths for the 2 separate cases.
1503 /// In some cases, this duplication can create an exponential amount of
1504 /// code. This is most easily seen by noticing that this method terminates
1505 /// with precisely the reachable arms being reachable - but that problem
1506 /// is trivially NP-complete:
1508 /// ```ignore (illustrative)
1509 /// match (var0, var1, var2, var3, ...) {
1510 /// (true , _ , _ , false, true, ...) => false,
1511 /// (_ , true, true , false, _ , ...) => false,
1512 /// (false, _ , false, false, _ , ...) => false,
1518 /// Here the last arm is reachable only if there is an assignment to
1519 /// the variables that does not match any of the literals. Therefore,
1520 /// compilation would take an exponential amount of time in some cases.
1522 /// That kind of exponential worst-case might not occur in practice, but
1523 /// our simplistic treatment of constants and guards would make it occur
1524 /// in very common situations - for example [#29740]:
1526 /// ```ignore (illustrative)
1528 /// "foo" if foo_guard => ...,
1529 /// "bar" if bar_guard => ...,
1530 /// "baz" if baz_guard => ...,
1535 /// [#29740]: https://github.com/rust-lang/rust/issues/29740
1537 /// Here we first test the match-pair `x @ "foo"`, which is an [`Eq` test].
1539 /// [`Eq` test]: TestKind::Eq
1541 /// It might seem that we would end up with 2 disjoint candidate
1542 /// sets, consisting of the first candidate or the other two, but our
1543 /// algorithm doesn't reason about `"foo"` being distinct from the other
1544 /// constants; it considers the latter arms to potentially match after
1545 /// both outcomes, which obviously leads to an exponential number
1548 /// To avoid these kinds of problems, our algorithm tries to ensure
1549 /// the amount of generated tests is linear. When we do a k-way test,
1550 /// we return an additional "unmatched" set alongside the obvious `k`
1551 /// sets. When we encounter a candidate that would be present in more
1552 /// than one of the sets, we put it and all candidates below it into the
1553 /// "unmatched" set. This ensures these `k+1` sets are disjoint.
1555 /// After we perform our test, we branch into the appropriate candidate
1556 /// set and recurse with `match_candidates`. These sub-matches are
1557 /// obviously non-exhaustive - as we discarded our otherwise set - so
1558 /// we set their continuation to do `match_candidates` on the
1559 /// "unmatched" set (which is again non-exhaustive).
1561 /// If you apply this to the above test, you basically wind up
1562 /// with an if-else-if chain, testing each candidate in turn,
1563 /// which is precisely what we want.
1565 /// In addition to avoiding exponential-time blowups, this algorithm
1566 /// also has the nice property that each guard and arm is only generated
1568 fn test_candidates<'pat, 'b, 'c>(
1571 scrutinee_span: Span,
1572 mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
1574 otherwise_block: &mut Option<BasicBlock>,
1575 fake_borrows: &mut Option<FxHashSet<Place<'tcx>>>,
1577 // extract the match-pair from the highest priority candidate
1578 let match_pair = &candidates.first().unwrap().match_pairs[0];
1579 let mut test = self.test(match_pair);
1580 let match_place = match_pair.place.clone();
1582 // most of the time, the test to perform is simply a function
1583 // of the main candidate; but for a test like SwitchInt, we
1584 // may want to add cases based on the candidates that are
1587 TestKind::SwitchInt { switch_ty, ref mut options } => {
1588 for candidate in candidates.iter() {
1589 if !self.add_cases_to_switch(&match_place, candidate, switch_ty, options) {
1594 TestKind::Switch { adt_def: _, ref mut variants } => {
1595 for candidate in candidates.iter() {
1596 if !self.add_variants_to_switch(&match_place, candidate, variants) {
1604 // Insert a Shallow borrow of any places that is switched on.
1605 if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
1606 match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
1608 let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
1609 fb.insert(resolved_place);
1612 // perform the test, branching to one of N blocks. For each of
1613 // those N possible outcomes, create a (initially empty)
1614 // vector of candidates. Those are the candidates that still
1615 // apply if the test has that particular outcome.
1616 debug!("match_candidates: test={:?} match_pair={:?}", test, match_pair);
1617 let mut target_candidates: Vec<Vec<&mut Candidate<'pat, 'tcx>>> = vec![];
1618 target_candidates.resize_with(test.targets(), Default::default);
1620 let total_candidate_count = candidates.len();
1622 // Sort the candidates into the appropriate vector in
1623 // `target_candidates`. Note that at some point we may
1624 // encounter a candidate where the test is not relevant; at
1625 // that point, we stop sorting.
1626 while let Some(candidate) = candidates.first_mut() {
1627 let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else {
1630 let (candidate, rest) = candidates.split_first_mut().unwrap();
1631 target_candidates[idx].push(candidate);
1634 // at least the first candidate ought to be tested
1635 assert!(total_candidate_count > candidates.len());
1636 debug!("tested_candidates: {}", total_candidate_count - candidates.len());
1637 debug!("untested_candidates: {}", candidates.len());
1639 // HACK(matthewjasper) This is a closure so that we can let the test
1640 // create its blocks before the rest of the match. This currently
1641 // improves the speed of llvm when optimizing long string literal
1643 let make_target_blocks = move |this: &mut Self| -> Vec<BasicBlock> {
1644 // The block that we should branch to if none of the
1645 // `target_candidates` match. This is either the block where we
1646 // start matching the untested candidates if there are any,
1647 // otherwise it's the `otherwise_block`.
1648 let remainder_start = &mut None;
1649 let remainder_start =
1650 if candidates.is_empty() { &mut *otherwise_block } else { remainder_start };
1652 // For each outcome of test, process the candidates that still
1653 // apply. Collect a list of blocks where control flow will
1654 // branch if one of the `target_candidate` sets is not
1656 let target_blocks: Vec<_> = target_candidates
1658 .map(|mut candidates| {
1659 if !candidates.is_empty() {
1660 let candidate_start = this.cfg.start_new_block();
1661 this.match_candidates(
1671 *remainder_start.get_or_insert_with(|| this.cfg.start_new_block())
1676 if !candidates.is_empty() {
1677 let remainder_start = remainder_start.unwrap_or_else(|| this.cfg.start_new_block());
1678 this.match_candidates(
1691 self.perform_test(span, scrutinee_span, block, match_place, &test, make_target_blocks);
1694 /// Determine the fake borrows that are needed from a set of places that
1695 /// have to be stable across match guards.
1697 /// Returns a list of places that need a fake borrow and the temporary
1698 /// that's used to store the fake borrow.
1700 /// Match exhaustiveness checking is not able to handle the case where the
1701 /// place being matched on is mutated in the guards. We add "fake borrows"
1702 /// to the guards that prevent any mutation of the place being matched.
1703 /// There are a some subtleties:
1705 /// 1. Borrowing `*x` doesn't prevent assigning to `x`. If `x` is a shared
1706 /// reference, the borrow isn't even tracked. As such we have to add fake
1707 /// borrows of any prefixes of a place
1708 /// 2. We don't want `match x { _ => (), }` to conflict with mutable
1709 /// borrows of `x`, so we only add fake borrows for places which are
1710 /// bound or tested by the match.
1711 /// 3. We don't want the fake borrows to conflict with `ref mut` bindings,
1712 /// so we use a special BorrowKind for them.
1713 /// 4. The fake borrows may be of places in inactive variants, so it would
1714 /// be UB to generate code for them. They therefore have to be removed
1715 /// by a MIR pass run after borrow checking.
1716 fn calculate_fake_borrows<'b>(
1718 fake_borrows: &'b FxHashSet<Place<'tcx>>,
1720 ) -> Vec<(Place<'tcx>, Local)> {
1723 debug!("add_fake_borrows fake_borrows = {:?}", fake_borrows);
1725 let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len());
1727 // Insert a Shallow borrow of the prefixes of any fake borrows.
1728 for place in fake_borrows {
1729 let mut cursor = place.projection.as_ref();
1730 while let [proj_base @ .., elem] = cursor {
1733 if let ProjectionElem::Deref = elem {
1734 // Insert a shallow borrow after a deref. For other
1735 // projections the borrow of prefix_cursor will
1736 // conflict with any mutation of base.
1737 all_fake_borrows.push(PlaceRef { local: place.local, projection: proj_base });
1741 all_fake_borrows.push(place.as_ref());
1744 // Deduplicate and ensure a deterministic order.
1745 all_fake_borrows.sort();
1746 all_fake_borrows.dedup();
1748 debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);
1752 .map(|matched_place_ref| {
1753 let matched_place = Place {
1754 local: matched_place_ref.local,
1755 projection: tcx.intern_place_elems(matched_place_ref.projection),
1757 let fake_borrow_deref_ty = matched_place.ty(&self.local_decls, tcx).ty;
1758 let fake_borrow_ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
1759 let fake_borrow_temp =
1760 self.local_decls.push(LocalDecl::new(fake_borrow_ty, temp_span));
1762 (matched_place, fake_borrow_temp)
1768 ///////////////////////////////////////////////////////////////////////////
1769 // Pat binding - used for `let` and function parameters as well.
1771 impl<'a, 'tcx> Builder<'a, 'tcx> {
1772 crate fn lower_let_expr(
1774 mut block: BasicBlock,
1777 else_target: region::Scope,
1780 let expr_span = expr.span;
1781 let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
1782 let wildcard = Pat::wildcard_from_ty(pat.ty);
1783 let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false);
1784 let mut otherwise_candidate = Candidate::new(expr_place_builder.clone(), &wildcard, false);
1785 let fake_borrow_temps = self.lower_match_tree(
1790 &mut [&mut guard_candidate, &mut otherwise_candidate],
1792 let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None;
1793 let expr_place: Place<'tcx>;
1794 if let Ok(expr_builder) =
1795 expr_place_builder.try_upvars_resolved(self.tcx, self.typeck_results)
1797 expr_place = expr_builder.into_place(self.tcx, self.typeck_results);
1798 opt_expr_place = Some((Some(&expr_place), expr_span));
1800 let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
1801 self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span));
1803 self.declare_bindings(None, pat.span.to(span), pat, ArmHasGuard(false), opt_expr_place);
1804 let post_guard_block = self.bind_pattern(
1805 self.source_info(pat.span),
1815 post_guard_block.unit()
1818 /// Initializes each of the bindings from the candidate by
1819 /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
1820 /// any, and then branches to the arm. Returns the block for the case where
1821 /// the guard succeeds.
1823 /// Note: we do not check earlier that if there is a guard,
1824 /// there cannot be move bindings. We avoid a use-after-move by only
1825 /// moving the binding once the guard has evaluated to true (see below).
1826 fn bind_and_guard_matched_candidate<'pat>(
1828 candidate: Candidate<'pat, 'tcx>,
1829 parent_bindings: &[(Vec<Binding<'tcx>>, Vec<Ascription<'tcx>>)],
1830 guard: Option<&Guard<'tcx>>,
1831 fake_borrows: &Vec<(Place<'tcx>, Local)>,
1832 scrutinee_span: Span,
1833 arm_span: Option<Span>,
1834 match_scope: Option<region::Scope>,
1835 schedule_drops: bool,
1837 debug!("bind_and_guard_matched_candidate(candidate={:?})", candidate);
1839 debug_assert!(candidate.match_pairs.is_empty());
1841 let candidate_source_info = self.source_info(candidate.span);
1843 let mut block = candidate.pre_binding_block.unwrap();
1845 if candidate.next_candidate_pre_binding_block.is_some() {
1846 let fresh_block = self.cfg.start_new_block();
1850 candidate.next_candidate_pre_binding_block,
1851 candidate_source_info,
1853 block = fresh_block;
1860 .flat_map(|(_, ascriptions)| ascriptions)
1861 .chain(&candidate.ascriptions),
1864 // rust-lang/rust#27282: The `autoref` business deserves some
1865 // explanation here.
1867 // The intent of the `autoref` flag is that when it is true,
1868 // then any pattern bindings of type T will map to a `&T`
1869 // within the context of the guard expression, but will
1870 // continue to map to a `T` in the context of the arm body. To
1871 // avoid surfacing this distinction in the user source code
1872 // (which would be a severe change to the language and require
1873 // far more revision to the compiler), when `autoref` is true,
1874 // then any occurrence of the identifier in the guard
1875 // expression will automatically get a deref op applied to it.
1877 // So an input like:
1880 // let place = Foo::new();
1881 // match place { foo if inspect(foo)
1882 // => feed(foo), ... }
1885 // will be treated as if it were really something like:
1888 // let place = Foo::new();
1889 // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
1890 // => { let tmp2 = place; feed(tmp2) }, ... }
1892 // And an input like:
1895 // let place = Foo::new();
1896 // match place { ref mut foo if inspect(foo)
1897 // => feed(foo), ... }
1900 // will be treated as if it were really something like:
1903 // let place = Foo::new();
1904 // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
1905 // => { let tmp2 = &mut place; feed(tmp2) }, ... }
1908 // In short, any pattern binding will always look like *some*
1909 // kind of `&T` within the guard at least in terms of how the
1910 // MIR-borrowck views it, and this will ensure that guard
1911 // expressions cannot mutate their the match inputs via such
1912 // bindings. (It also ensures that guard expressions can at
1913 // most *copy* values from such bindings; non-Copy things
1914 // cannot be moved via pattern bindings in guard expressions.)
1918 // Implementation notes (under assumption `autoref` is true).
1920 // To encode the distinction above, we must inject the
1921 // temporaries `tmp1` and `tmp2`.
1923 // There are two cases of interest: binding by-value, and binding by-ref.
1925 // 1. Binding by-value: Things are simple.
1927 // * Establishing `tmp1` creates a reference into the
1928 // matched place. This code is emitted by
1929 // bind_matched_candidate_for_guard.
1931 // * `tmp2` is only initialized "lazily", after we have
1932 // checked the guard. Thus, the code that can trigger
1933 // moves out of the candidate can only fire after the
1934 // guard evaluated to true. This initialization code is
1935 // emitted by bind_matched_candidate_for_arm.
1937 // 2. Binding by-reference: Things are tricky.
1939 // * Here, the guard expression wants a `&&` or `&&mut`
1940 // into the original input. This means we need to borrow
1941 // the reference that we create for the arm.
1942 // * So we eagerly create the reference for the arm and then take a
1943 // reference to that.
1944 if let Some(guard) = guard {
1946 let bindings = parent_bindings
1948 .flat_map(|(bindings, _)| bindings)
1949 .chain(&candidate.bindings);
1951 self.bind_matched_candidate_for_guard(block, schedule_drops, bindings.clone());
1952 let guard_frame = GuardFrame {
1953 locals: bindings.map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode)).collect(),
1955 debug!("entering guard building context: {:?}", guard_frame);
1956 self.guard_context.push(guard_frame);
1958 let re_erased = tcx.lifetimes.re_erased;
1959 let scrutinee_source_info = self.source_info(scrutinee_span);
1960 for &(place, temp) in fake_borrows {
1961 let borrow = Rvalue::Ref(re_erased, BorrowKind::Shallow, place);
1962 self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
1965 let arm_span = arm_span.unwrap();
1966 let match_scope = match_scope.unwrap();
1967 let mut guard_span = rustc_span::DUMMY_SP;
1969 let (post_guard_block, otherwise_post_guard_block) =
1970 self.in_if_then_scope(match_scope, |this| match *guard {
1972 let e = &this.thir[e];
1973 guard_span = e.span;
1974 this.then_else_break(block, e, None, match_scope, arm_span)
1976 Guard::IfLet(ref pat, scrutinee) => {
1977 let s = &this.thir[scrutinee];
1978 guard_span = s.span;
1979 this.lower_let_expr(block, s, pat, match_scope, arm_span)
1983 let source_info = self.source_info(guard_span);
1984 let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
1985 let guard_frame = self.guard_context.pop().unwrap();
1986 debug!("Exiting guard building context with locals: {:?}", guard_frame);
1988 for &(_, temp) in fake_borrows {
1989 let cause = FakeReadCause::ForMatchGuard;
1990 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
1993 let otherwise_block = candidate.otherwise_block.unwrap_or_else(|| {
1994 let unreachable = self.cfg.start_new_block();
1995 self.cfg.terminate(unreachable, source_info, TerminatorKind::Unreachable);
1999 otherwise_post_guard_block,
2001 candidate.next_candidate_pre_binding_block,
2005 // We want to ensure that the matched candidates are bound
2006 // after we have confirmed this candidate *and* any
2007 // associated guard; Binding them on `block` is too soon,
2008 // because that would be before we've checked the result
2011 // But binding them on the arm is *too late*, because
2012 // then all of the candidates for a single arm would be
2013 // bound in the same place, that would cause a case like:
2017 // (mut x, 1) | (2, mut x) if { true } => { ... }
2018 // ... // ^^^^^^^ (this is `arm_block`)
2022 // would yield an `arm_block` something like:
2025 // StorageLive(_4); // _4 is `x`
2026 // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case
2027 // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case
2030 // and that is clearly not correct.
2031 let by_value_bindings = parent_bindings
2033 .flat_map(|(bindings, _)| bindings)
2034 .chain(&candidate.bindings)
2035 .filter(|binding| matches!(binding.binding_mode, BindingMode::ByValue));
2036 // Read all of the by reference bindings to ensure that the
2037 // place they refer to can't be modified by the guard.
2038 for binding in by_value_bindings.clone() {
2039 let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
2040 let cause = FakeReadCause::ForGuardBinding;
2041 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
2043 assert!(schedule_drops, "patterns with guards must schedule drops");
2044 self.bind_matched_candidate_for_arm_body(post_guard_block, true, by_value_bindings);
2048 // (Here, it is not too early to bind the matched
2049 // candidate on `block`, because there is no guard result
2050 // that we have to inspect before we bind them.)
2051 self.bind_matched_candidate_for_arm_body(
2056 .flat_map(|(bindings, _)| bindings)
2057 .chain(&candidate.bindings),
2063 /// Append `AscribeUserType` statements onto the end of `block`
2064 /// for each ascription
2065 fn ascribe_types<'b>(
2068 ascriptions: impl IntoIterator<Item = &'b Ascription<'tcx>>,
2072 for ascription in ascriptions {
2073 let source_info = self.source_info(ascription.span);
2076 "adding user ascription at span {:?} of place {:?} and {:?}",
2077 source_info.span, ascription.source, ascription.user_ty,
2080 let user_ty = ascription.user_ty.user_ty(
2081 &mut self.canonical_user_type_annotations,
2082 ascription.source.ty(&self.local_decls, self.tcx).ty,
2089 kind: StatementKind::AscribeUserType(
2090 Box::new((ascription.source, user_ty)),
2091 ascription.variance,
2098 fn bind_matched_candidate_for_guard<'b>(
2101 schedule_drops: bool,
2102 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2106 debug!("bind_matched_candidate_for_guard(block={:?})", block);
2108 // Assign each of the bindings. Since we are binding for a
2109 // guard expression, this will never trigger moves out of the
2111 let re_erased = self.tcx.lifetimes.re_erased;
2112 for binding in bindings {
2113 debug!("bind_matched_candidate_for_guard(binding={:?})", binding);
2114 let source_info = self.source_info(binding.span);
2116 // For each pattern ident P of type T, `ref_for_guard` is
2117 // a reference R: &T pointing to the location matched by
2118 // the pattern, and every occurrence of P within a guard
2120 let ref_for_guard = self.storage_live_binding(
2127 match binding.binding_mode {
2128 BindingMode::ByValue => {
2129 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
2130 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2132 BindingMode::ByRef(borrow_kind) => {
2133 let value_for_arm = self.storage_live_binding(
2141 let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source);
2142 self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
2143 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
2144 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2150 fn bind_matched_candidate_for_arm_body<'b>(
2153 schedule_drops: bool,
2154 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2158 debug!("bind_matched_candidate_for_arm_body(block={:?})", block);
2160 let re_erased = self.tcx.lifetimes.re_erased;
2161 // Assign each of the bindings. This may trigger moves out of the candidate.
2162 for binding in bindings {
2163 let source_info = self.source_info(binding.span);
2164 let local = self.storage_live_binding(
2172 self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2174 let rvalue = match binding.binding_mode {
2175 BindingMode::ByValue => Rvalue::Use(self.consume_by_copy_or_move(binding.source)),
2176 BindingMode::ByRef(borrow_kind) => {
2177 Rvalue::Ref(re_erased, borrow_kind, binding.source)
2180 self.cfg.push_assign(block, source_info, local, rvalue);
2184 /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2185 /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2186 /// first local is a binding for occurrences of `var` in the guard, which
2187 /// will have type `&T`. The second local is a binding for occurrences of
2188 /// `var` in the arm body, which will have type `T`.
2191 source_info: SourceInfo,
2192 visibility_scope: SourceScope,
2193 mutability: Mutability,
2198 user_ty: UserTypeProjections,
2199 has_guard: ArmHasGuard,
2200 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2204 "declare_binding(var_id={:?}, name={:?}, mode={:?}, var_ty={:?}, \
2205 visibility_scope={:?}, source_info={:?})",
2206 var_id, name, mode, var_ty, visibility_scope, source_info
2210 let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
2211 let binding_mode = match mode {
2212 BindingMode::ByValue => ty::BindingMode::BindByValue(mutability),
2213 BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability),
2215 debug!("declare_binding: user_ty={:?}", user_ty);
2216 let local = LocalDecl::<'tcx> {
2219 user_ty: if user_ty.is_empty() { None } else { Some(Box::new(user_ty)) },
2222 is_block_tail: None,
2223 local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
2226 // hypothetically, `visit_primary_bindings` could try to unzip
2227 // an outermost hir::Ty as we descend, matching up
2228 // idents in pat; but complex w/ unclear UI payoff.
2229 // Instead, just abandon providing diagnostic info.
2236 let for_arm_body = self.local_decls.push(local);
2237 self.var_debug_info.push(VarDebugInfo {
2239 source_info: debug_source_info,
2240 value: VarDebugInfoContents::Place(for_arm_body.into()),
2242 let locals = if has_guard.0 {
2243 let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
2244 // This variable isn't mutated but has a name, so has to be
2245 // immutable to avoid the unused mut lint.
2246 mutability: Mutability::Not,
2247 ty: tcx.mk_imm_ref(tcx.lifetimes.re_erased, var_ty),
2251 is_block_tail: None,
2252 local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
2253 BindingForm::RefForGuard,
2256 self.var_debug_info.push(VarDebugInfo {
2258 source_info: debug_source_info,
2259 value: VarDebugInfoContents::Place(ref_for_guard.into()),
2261 LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
2263 LocalsForNode::One(for_arm_body)
2265 debug!("declare_binding: vars={:?}", locals);
2266 self.var_indices.insert(var_id, locals);