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