3 // After candidates have been simplified, the only match pairs that
4 // remain are those that require some sort of test. The functions here
5 // identify what tests are needed, perform the tests, and then filter
6 // the candidates based on the result.
8 use crate::build::Builder;
9 use crate::build::matches::{Candidate, MatchPair, Test, TestKind};
11 use crate::hair::pattern::compare_const_vals;
12 use rustc_data_structures::bit_set::BitSet;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc::ty::{self, Ty, adjustment::{PointerCast}};
15 use rustc::ty::util::IntTypeExt;
16 use rustc::ty::layout::VariantIdx;
18 use rustc::hir::RangeEnd;
19 use syntax_pos::symbol::sym;
21 use std::cmp::Ordering;
23 impl<'a, 'tcx> Builder<'a, 'tcx> {
24 /// Identifies what test is needed to decide if `match_pair` is applicable.
26 /// It is a bug to call this with a simplifiable pattern.
27 pub fn test<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> Test<'tcx> {
28 match *match_pair.pattern.kind {
29 PatternKind::Variant { ref adt_def, substs: _, variant_index: _, subpatterns: _ } => {
31 span: match_pair.pattern.span,
32 kind: TestKind::Switch {
33 adt_def: adt_def.clone(),
34 variants: BitSet::new_empty(adt_def.variants.len()),
39 PatternKind::Constant { .. } if is_switch_ty(match_pair.pattern.ty) => {
40 // For integers, we use a `SwitchInt` match, which allows
41 // us to handle more cases.
43 span: match_pair.pattern.span,
44 kind: TestKind::SwitchInt {
45 switch_ty: match_pair.pattern.ty,
47 // these maps are empty to start; cases are
48 // added below in add_cases_to_switch
50 indices: Default::default(),
55 PatternKind::Constant { value } => {
57 span: match_pair.pattern.span,
60 ty: match_pair.pattern.ty.clone()
65 PatternKind::Range(range) => {
66 assert!(range.ty == match_pair.pattern.ty);
68 span: match_pair.pattern.span,
69 kind: TestKind::Range(range),
73 PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
74 let len = prefix.len() + suffix.len();
75 let op = if slice.is_some() {
81 span: match_pair.pattern.span,
82 kind: TestKind::Len { len: len as u64, op: op },
86 PatternKind::AscribeUserType { .. } |
87 PatternKind::Array { .. } |
89 PatternKind::Binding { .. } |
90 PatternKind::Leaf { .. } |
91 PatternKind::Deref { .. } => {
92 self.error_simplifyable(match_pair)
97 pub fn add_cases_to_switch<'pat>(&mut self,
98 test_place: &Place<'tcx>,
99 candidate: &Candidate<'pat, 'tcx>,
101 options: &mut Vec<u128>,
102 indices: &mut FxHashMap<&'tcx ty::Const<'tcx>, usize>)
105 let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
106 Some(match_pair) => match_pair,
107 _ => { return false; }
110 match *match_pair.pattern.kind {
111 PatternKind::Constant { value } => {
112 let switch_ty = ty::ParamEnv::empty().and(switch_ty);
115 options.push(value.unwrap_bits(self.hir.tcx(), switch_ty));
120 PatternKind::Variant { .. } => {
121 panic!("you should have called add_variants_to_switch instead!");
123 PatternKind::Range(range) => {
124 // Check that none of the switch values are in the range.
125 self.values_not_contained_in_range(range, indices)
128 PatternKind::Slice { .. } |
129 PatternKind::Array { .. } |
131 PatternKind::Binding { .. } |
132 PatternKind::AscribeUserType { .. } |
133 PatternKind::Leaf { .. } |
134 PatternKind::Deref { .. } => {
135 // don't know how to add these patterns to a switch
141 pub fn add_variants_to_switch<'pat>(&mut self,
142 test_place: &Place<'tcx>,
143 candidate: &Candidate<'pat, 'tcx>,
144 variants: &mut BitSet<VariantIdx>)
147 let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
148 Some(match_pair) => match_pair,
149 _ => { return false; }
152 match *match_pair.pattern.kind {
153 PatternKind::Variant { adt_def: _ , variant_index, .. } => {
154 // We have a pattern testing for variant `variant_index`
155 // set the corresponding index to true
156 variants.insert(variant_index);
160 // don't know how to add these patterns to a switch
171 make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
173 debug!("perform_test({:?}, {:?}: {:?}, {:?})",
176 place.ty(&self.local_decls, self.hir.tcx()),
179 let source_info = self.source_info(test.span);
181 TestKind::Switch { adt_def, ref variants } => {
182 let target_blocks = make_target_blocks(self);
183 // Variants is a BitVec of indexes into adt_def.variants.
184 let num_enum_variants = adt_def.variants.len();
185 let used_variants = variants.count();
186 debug_assert_eq!(target_blocks.len(), num_enum_variants + 1);
187 let otherwise_block = *target_blocks.last().unwrap();
188 let mut targets = Vec::with_capacity(used_variants + 1);
189 let mut values = Vec::with_capacity(used_variants);
190 let tcx = self.hir.tcx();
191 for (idx, discr) in adt_def.discriminants(tcx) {
192 if variants.contains(idx) {
194 target_blocks[idx.index()],
196 "no canididates for tested discriminant: {:?}",
199 values.push(discr.val);
200 targets.push(target_blocks[idx.index()]);
203 target_blocks[idx.index()],
205 "found canididates for untested discriminant: {:?}",
210 targets.push(otherwise_block);
211 debug!("num_enum_variants: {}, tested variants: {:?}, variants: {:?}",
212 num_enum_variants, values, variants);
213 let discr_ty = adt_def.repr.discr_type().to_ty(tcx);
214 let discr = self.temp(discr_ty, test.span);
215 self.cfg.push_assign(block, source_info, &discr,
216 Rvalue::Discriminant(place.clone()));
217 assert_eq!(values.len() + 1, targets.len());
218 self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
219 discr: Operand::Move(discr),
221 values: From::from(values),
226 TestKind::SwitchInt { switch_ty, ref options, indices: _ } => {
227 let target_blocks = make_target_blocks(self);
228 let terminator = if switch_ty.sty == ty::Bool {
229 assert!(options.len() > 0 && options.len() <= 2);
230 if let [first_bb, second_bb] = *target_blocks {
231 let (true_bb, false_bb) = match options[0] {
232 1 => (first_bb, second_bb),
233 0 => (second_bb, first_bb),
234 v => span_bug!(test.span, "expected boolean value but got {:?}", v)
238 Operand::Copy(place.clone()),
243 bug!("`TestKind::SwitchInt` on `bool` should have two targets")
246 // The switch may be inexhaustive so we have a catch all block
247 debug_assert_eq!(options.len() + 1, target_blocks.len());
248 TerminatorKind::SwitchInt {
249 discr: Operand::Copy(place.clone()),
251 values: options.clone().into(),
252 targets: target_blocks,
255 self.cfg.terminate(block, source_info, terminator);
258 TestKind::Eq { value, ty } => {
260 // Use `PartialEq::eq` instead of `BinOp::Eq`
261 // (the binop can only handle primitives)
262 self.non_scalar_compare(
271 if let [success, fail] = *make_target_blocks(self) {
272 let val = Operand::Copy(place.clone());
273 let expect = self.literal_operand(test.span, ty, value);
274 self.compare(block, success, fail, source_info, BinOp::Eq, expect, val);
276 bug!("`TestKind::Eq` should have two target blocks");
281 TestKind::Range(PatternRange { ref lo, ref hi, ty, ref end }) => {
282 let lower_bound_success = self.cfg.start_new_block();
283 let target_blocks = make_target_blocks(self);
285 // Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
286 let lo = self.literal_operand(test.span, ty, lo);
287 let hi = self.literal_operand(test.span, ty, hi);
288 let val = Operand::Copy(place.clone());
290 if let [success, fail] = *target_blocks {
300 let op = match *end {
301 RangeEnd::Included => BinOp::Le,
302 RangeEnd::Excluded => BinOp::Lt,
304 self.compare(lower_bound_success, success, fail, source_info, op, val, hi);
306 bug!("`TestKind::Range` should have two target blocks");
310 TestKind::Len { len, op } => {
311 let target_blocks = make_target_blocks(self);
313 let usize_ty = self.hir.usize_ty();
314 let actual = self.temp(usize_ty, test.span);
316 // actual = len(place)
317 self.cfg.push_assign(block, source_info,
318 &actual, Rvalue::Len(place.clone()));
321 let expected = self.push_usize(block, source_info, len);
323 if let [true_bb, false_bb] = *target_blocks {
324 // result = actual == expected OR result = actual < expected
325 // branch based on result
332 Operand::Move(actual),
333 Operand::Move(expected),
336 bug!("`TestKind::Len` should have two target blocks");
342 /// Compare using the provided built-in comparison operator
346 success_block: BasicBlock,
347 fail_block: BasicBlock,
348 source_info: SourceInfo,
351 right: Operand<'tcx>,
353 let bool_ty = self.hir.bool_ty();
354 let result = self.temp(bool_ty, source_info.span);
356 // result = op(left, right)
357 self.cfg.push_assign(
361 Rvalue::BinaryOp(op, left, right),
364 // branch based on result
370 Operand::Move(result),
377 /// Compare two `&T` values using `<T as std::compare::PartialEq>::eq`
378 fn non_scalar_compare(
381 make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
382 source_info: SourceInfo,
383 value: &'tcx ty::Const<'tcx>,
387 use rustc::middle::lang_items::EqTraitLangItem;
389 let mut expect = self.literal_operand(source_info.span, value.ty, value);
390 let mut val = Operand::Copy(place.clone());
392 // If we're using `b"..."` as a pattern, we need to insert an
393 // unsizing coercion, as the byte string has the type `&[u8; N]`.
395 // We want to do this even when the scrutinee is a reference to an
396 // array, so we can call `<[u8]>::eq` rather than having to find an
398 let unsize = |ty: Ty<'tcx>| match ty.sty {
399 ty::Ref(region, rty, _) => match rty.sty {
400 ty::Array(inner_ty, n) => Some((region, inner_ty, n)),
405 let opt_ref_ty = unsize(ty);
406 let opt_ref_test_ty = unsize(value.ty);
407 match (opt_ref_ty, opt_ref_test_ty) {
408 // nothing to do, neither is an array
410 (Some((region, elem_ty, _)), _) |
411 (None, Some((region, elem_ty, _))) => {
412 let tcx = self.hir.tcx();
414 ty = tcx.mk_imm_ref(region, tcx.mk_slice(elem_ty));
415 if opt_ref_ty.is_some() {
416 let temp = self.temp(ty, source_info.span);
417 self.cfg.push_assign(
418 block, source_info, &temp, Rvalue::Cast(
419 CastKind::Pointer(PointerCast::Unsize), val, ty
422 val = Operand::Move(temp);
424 if opt_ref_test_ty.is_some() {
425 let slice = self.temp(ty, source_info.span);
426 self.cfg.push_assign(
427 block, source_info, &slice, Rvalue::Cast(
428 CastKind::Pointer(PointerCast::Unsize), expect, ty
431 expect = Operand::Move(slice);
436 let deref_ty = match ty.sty {
437 ty::Ref(_, deref_ty, _) => deref_ty,
438 _ => bug!("non_scalar_compare called on non-reference type: {}", ty),
441 let eq_def_id = self.hir.tcx().require_lang_item(EqTraitLangItem);
442 let (mty, method) = self.hir.trait_method(eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]);
444 let bool_ty = self.hir.bool_ty();
445 let eq_result = self.temp(bool_ty, source_info.span);
446 let eq_block = self.cfg.start_new_block();
447 let cleanup = self.diverge_cleanup();
448 self.cfg.terminate(block, source_info, TerminatorKind::Call {
449 func: Operand::Constant(box Constant {
450 span: source_info.span,
453 // FIXME(#54571): This constant comes from user input (a
454 // constant in a pattern). Are there forms where users can add
455 // type annotations here? For example, an associated constant?
456 // Need to experiment.
461 args: vec![val, expect],
462 destination: Some((eq_result.clone(), eq_block)),
463 cleanup: Some(cleanup),
464 from_hir_call: false,
467 if let [success_block, fail_block] = *make_target_blocks(self) {
474 Operand::Move(eq_result),
480 bug!("`TestKind::Eq` should have two target blocks")
484 /// Given that we are performing `test` against `test_place`, this job
485 /// sorts out what the status of `candidate` will be after the test. See
486 /// `test_candidates` for the usage of this function. The returned index is
487 /// the index that this candiate should be placed in the
488 /// `target_candidates` vec. The candidate may be modified to update its
491 /// So, for example, if this candidate is `x @ Some(P0)` and the `Test` is
492 /// a variant test, then we would modify the candidate to be `(x as
493 /// Option).0 @ P0` and return the index corresponding to the variant
496 /// However, in some cases, the test may just not be relevant to candidate.
497 /// For example, suppose we are testing whether `foo.x == 22`, but in one
498 /// match arm we have `Foo { x: _, ... }`... in that case, the test for
499 /// what value `x` has has no particular relevance to this candidate. In
500 /// such cases, this function just returns None without doing anything.
501 /// This is used by the overall `match_candidates` algorithm to structure
502 /// the match as a whole. See `match_candidates` for more details.
504 /// FIXME(#29623). In some cases, we have some tricky choices to make. for
505 /// example, if we are testing that `x == 22`, but the candidate is `x @
506 /// 13..55`, what should we do? In the event that the test is true, we know
507 /// that the candidate applies, but in the event of false, we don't know
508 /// that it *doesn't* apply. For now, we return false, indicate that the
509 /// test does not apply to this candidate, but it might be we can get
510 /// tighter match code if we do something a bit different.
511 pub fn sort_candidate<'pat>(
513 test_place: &Place<'tcx>,
515 candidate: &mut Candidate<'pat, 'tcx>,
517 // Find the match_pair for this place (if any). At present,
518 // afaik, there can be at most one. (In the future, if we
519 // adopted a more general `@` operator, there might be more
520 // than one, but it'd be very unusual to have two sides that
521 // both require tests; you'd expect one side to be simplified
523 let (match_pair_index, match_pair) = candidate.match_pairs
526 .find(|&(_, mp)| mp.place == *test_place)?;
528 match (&test.kind, &*match_pair.pattern.kind) {
529 // If we are performing a variant switch, then this
530 // informs variant patterns, but nothing else.
531 (&TestKind::Switch { adt_def: tested_adt_def, .. },
532 &PatternKind::Variant { adt_def, variant_index, ref subpatterns, .. }) => {
533 assert_eq!(adt_def, tested_adt_def);
534 self.candidate_after_variant_switch(match_pair_index,
539 Some(variant_index.as_usize())
542 (&TestKind::Switch { .. }, _) => None,
544 // If we are performing a switch over integers, then this informs integer
545 // equality, but nothing else.
547 // FIXME(#29623) we could use PatternKind::Range to rule
548 // things out here, in some cases.
549 (&TestKind::SwitchInt { switch_ty: _, options: _, ref indices },
550 &PatternKind::Constant { ref value })
551 if is_switch_ty(match_pair.pattern.ty) => {
552 let index = indices[value];
553 self.candidate_without_match_pair(match_pair_index, candidate);
557 (&TestKind::SwitchInt { switch_ty: _, ref options, ref indices },
558 &PatternKind::Range(range)) => {
559 let not_contained = self
560 .values_not_contained_in_range(range, indices)
564 // No switch values are contained in the pattern range,
565 // so the pattern can be matched only if this test fails.
566 let otherwise = options.len();
573 (&TestKind::SwitchInt { .. }, _) => None,
575 (&TestKind::Len { len: test_len, op: BinOp::Eq },
576 &PatternKind::Slice { ref prefix, ref slice, ref suffix }) => {
577 let pat_len = (prefix.len() + suffix.len()) as u64;
578 match (test_len.cmp(&pat_len), slice) {
579 (Ordering::Equal, &None) => {
580 // on true, min_len = len = $actual_length,
581 // on false, len != $actual_length
582 self.candidate_after_slice_test(match_pair_index,
589 (Ordering::Less, _) => {
590 // test_len < pat_len. If $actual_len = test_len,
591 // then $actual_len < pat_len and we don't have
595 (Ordering::Equal, &Some(_)) | (Ordering::Greater, &Some(_)) => {
596 // This can match both if $actual_len = test_len >= pat_len,
597 // and if $actual_len > test_len. We can't advance.
600 (Ordering::Greater, &None) => {
601 // test_len != pat_len, so if $actual_len = test_len, then
602 // $actual_len != pat_len.
608 (&TestKind::Len { len: test_len, op: BinOp::Ge },
609 &PatternKind::Slice { ref prefix, ref slice, ref suffix }) => {
610 // the test is `$actual_len >= test_len`
611 let pat_len = (prefix.len() + suffix.len()) as u64;
612 match (test_len.cmp(&pat_len), slice) {
613 (Ordering::Equal, &Some(_)) => {
614 // $actual_len >= test_len = pat_len,
616 self.candidate_after_slice_test(match_pair_index,
623 (Ordering::Less, _) | (Ordering::Equal, &None) => {
624 // test_len <= pat_len. If $actual_len < test_len,
625 // then it is also < pat_len, so the test passing is
626 // necessary (but insufficient).
629 (Ordering::Greater, &None) => {
630 // test_len > pat_len. If $actual_len >= test_len > pat_len,
631 // then we know we won't have a match.
634 (Ordering::Greater, &Some(_)) => {
635 // test_len < pat_len, and is therefore less
636 // strict. This can still go both ways.
642 (&TestKind::Range(test),
643 &PatternKind::Range(pat)) => {
645 self.candidate_without_match_pair(
652 let no_overlap = (|| {
653 use std::cmp::Ordering::*;
654 use rustc::hir::RangeEnd::*;
656 let param_env = ty::ParamEnv::empty().and(test.ty);
657 let tcx = self.hir.tcx();
659 let lo = compare_const_vals(tcx, test.lo, pat.hi, param_env)?;
660 let hi = compare_const_vals(tcx, test.hi, pat.lo, param_env)?;
662 match (test.end, pat.end, lo, hi) {
665 (_, Excluded, Equal, _) |
668 (Excluded, _, _, Equal) => Some(true),
673 if no_overlap == Some(true) {
674 // Testing range does not overlap with pattern range,
675 // so the pattern can be matched only if this test fails.
682 (&TestKind::Range(range), &PatternKind::Constant { value }) => {
683 if self.const_range_contains(range, value) == Some(false) {
684 // `value` is not contained in the testing range,
685 // so `value` can be matched only if this test fails.
692 (&TestKind::Range { .. }, _) => None,
694 (&TestKind::Eq { .. }, _) |
695 (&TestKind::Len { .. }, _) => {
696 // These are all binary tests.
698 // FIXME(#29623) we can be more clever here
699 let pattern_test = self.test(&match_pair);
700 if pattern_test.kind == test.kind {
701 self.candidate_without_match_pair(match_pair_index, candidate);
710 fn candidate_without_match_pair(
712 match_pair_index: usize,
713 candidate: &mut Candidate<'_, 'tcx>,
715 candidate.match_pairs.remove(match_pair_index);
718 fn candidate_after_slice_test<'pat>(&mut self,
719 match_pair_index: usize,
720 candidate: &mut Candidate<'pat, 'tcx>,
721 prefix: &'pat [Pattern<'tcx>],
722 opt_slice: Option<&'pat Pattern<'tcx>>,
723 suffix: &'pat [Pattern<'tcx>]) {
724 let removed_place = candidate.match_pairs.remove(match_pair_index).place;
725 self.prefix_slice_suffix(
726 &mut candidate.match_pairs,
733 fn candidate_after_variant_switch<'pat>(
735 match_pair_index: usize,
736 adt_def: &'tcx ty::AdtDef,
737 variant_index: VariantIdx,
738 subpatterns: &'pat [FieldPattern<'tcx>],
739 candidate: &mut Candidate<'pat, 'tcx>,
741 let match_pair = candidate.match_pairs.remove(match_pair_index);
743 // So, if we have a match-pattern like `x @ Enum::Variant(P1, P2)`,
744 // we want to create a set of derived match-patterns like
745 // `(x as Variant).0 @ P1` and `(x as Variant).1 @ P1`.
746 let elem = ProjectionElem::Downcast(
747 Some(adt_def.variants[variant_index].ident.name), variant_index);
748 let downcast_place = match_pair.place.elem(elem); // `(x as Variant)`
749 let consequent_match_pairs =
752 // e.g., `(x as Variant).0`
753 let place = downcast_place.clone().field(subpattern.field,
754 subpattern.pattern.ty);
755 // e.g., `(x as Variant).0 @ P1`
756 MatchPair::new(place, &subpattern.pattern)
759 candidate.match_pairs.extend(consequent_match_pairs);
762 fn error_simplifyable<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> ! {
763 span_bug!(match_pair.pattern.span,
764 "simplifyable pattern found: {:?}",
768 fn const_range_contains(
770 range: PatternRange<'tcx>,
771 value: &'tcx ty::Const<'tcx>,
773 use std::cmp::Ordering::*;
775 let param_env = ty::ParamEnv::empty().and(range.ty);
776 let tcx = self.hir.tcx();
778 let a = compare_const_vals(tcx, range.lo, value, param_env)?;
779 let b = compare_const_vals(tcx, value, range.hi, param_env)?;
781 match (b, range.end) {
783 (Equal, RangeEnd::Included) if a != Greater => Some(true),
788 fn values_not_contained_in_range(
790 range: PatternRange<'tcx>,
791 indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>,
793 for &val in indices.keys() {
794 if self.const_range_contains(range, val)? {
804 pub(super) fn targets(&self) -> usize {
806 TestKind::Eq { .. } | TestKind::Range(_) | TestKind::Len { .. } => {
809 TestKind::Switch { adt_def, .. } => {
810 // While the switch that we generate doesn't test for all
811 // variants, we have a target for each variant and the
812 // otherwise case, and we make sure that all of the cases not
813 // specified have the same block.
814 adt_def.variants.len() + 1
816 TestKind::SwitchInt { switch_ty, ref options, .. } => {
817 if switch_ty.is_bool() {
818 // `bool` is special cased in `perform_test` to always
819 // branch to two blocks.
829 fn is_switch_ty(ty: Ty<'_>) -> bool {
830 ty.is_integral() || ty.is_char() || ty.is_bool()