]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/matches/util.rs
db1f678a5c68d46a535f9acf8d907ffceb5e043b
[rust.git] / compiler / rustc_mir_build / src / build / matches / util.rs
1 use crate::build::matches::MatchPair;
2 use crate::build::Builder;
3 use crate::thir::*;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty;
6 use smallvec::SmallVec;
7 use std::convert::TryInto;
8
9 impl<'a, 'tcx> Builder<'a, 'tcx> {
10     crate fn field_match_pairs<'pat>(
11         &mut self,
12         place: Place<'tcx>,
13         subpatterns: &'pat [FieldPat<'tcx>],
14     ) -> Vec<MatchPair<'pat, 'tcx>> {
15         subpatterns
16             .iter()
17             .map(|fieldpat| {
18                 let place =
19                     self.hir.tcx().mk_place_field(place, fieldpat.field, fieldpat.pattern.ty);
20                 MatchPair::new(place, &fieldpat.pattern)
21             })
22             .collect()
23     }
24
25     crate fn prefix_slice_suffix<'pat>(
26         &mut self,
27         match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
28         place: &Place<'tcx>,
29         prefix: &'pat [Pat<'tcx>],
30         opt_slice: Option<&'pat Pat<'tcx>>,
31         suffix: &'pat [Pat<'tcx>],
32     ) {
33         let tcx = self.hir.tcx();
34         let (min_length, exact_size) = match place.ty(&self.local_decls, tcx).ty.kind() {
35             ty::Array(_, length) => (length.eval_usize(tcx, self.hir.param_env), true),
36             _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
37         };
38
39         match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
40             let elem =
41                 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
42             let place = tcx.mk_place_elem(*place, elem);
43             MatchPair::new(place, subpattern)
44         }));
45
46         if let Some(subslice_pat) = opt_slice {
47             let suffix_len = suffix.len() as u64;
48             let subslice = tcx.mk_place_elem(
49                 *place,
50                 ProjectionElem::Subslice {
51                     from: prefix.len() as u64,
52                     to: if exact_size { min_length - suffix_len } else { suffix_len },
53                     from_end: !exact_size,
54                 },
55             );
56             match_pairs.push(MatchPair::new(subslice, subslice_pat));
57         }
58
59         match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
60             let end_offset = (idx + 1) as u64;
61             let elem = ProjectionElem::ConstantIndex {
62                 offset: if exact_size { min_length - end_offset } else { end_offset },
63                 min_length,
64                 from_end: !exact_size,
65             };
66             let place = tcx.mk_place_elem(*place, elem);
67             MatchPair::new(place, subpattern)
68         }));
69     }
70
71     /// Creates a false edge to `imaginary_target` and a real edge to
72     /// real_target. If `imaginary_target` is none, or is the same as the real
73     /// target, a Goto is generated instead to simplify the generated MIR.
74     crate fn false_edges(
75         &mut self,
76         from_block: BasicBlock,
77         real_target: BasicBlock,
78         imaginary_target: Option<BasicBlock>,
79         source_info: SourceInfo,
80     ) {
81         match imaginary_target {
82             Some(target) if target != real_target => {
83                 self.cfg.terminate(
84                     from_block,
85                     source_info,
86                     TerminatorKind::FalseEdge { real_target, imaginary_target: target },
87                 );
88             }
89             _ => self.cfg.goto(from_block, source_info, real_target),
90         }
91     }
92 }
93
94 impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
95     crate fn new(place: Place<'tcx>, pattern: &'pat Pat<'tcx>) -> MatchPair<'pat, 'tcx> {
96         MatchPair { place, pattern }
97     }
98 }