]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir_build/build/matches/util.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / librustc_mir_build / build / matches / util.rs
CommitLineData
dfeec247
XL
1use crate::build::matches::MatchPair;
2use crate::build::Builder;
3use crate::hair::*;
ba9703b0
XL
4use rustc_middle::mir::*;
5use rustc_middle::ty;
dfeec247
XL
6use smallvec::SmallVec;
7use std::convert::TryInto;
dfeec247
XL
8
9impl<'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| {
ba9703b0
XL
18 let place =
19 self.hir.tcx().mk_place_field(place, fieldpat.field, fieldpat.pattern.ty);
dfeec247
XL
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) => {
36 (length.eval_usize(tcx, self.hir.param_env).try_into().unwrap(), true)
37 }
38 _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
39 };
40
41 match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
42 let elem =
43 ProjectionElem::ConstantIndex { offset: idx as u32, min_length, from_end: false };
ba9703b0 44 let place = tcx.mk_place_elem(*place, elem);
dfeec247
XL
45 MatchPair::new(place, subpattern)
46 }));
47
48 if let Some(subslice_pat) = opt_slice {
49 let suffix_len = suffix.len() as u32;
50 let subslice = tcx.mk_place_elem(
ba9703b0 51 *place,
dfeec247
XL
52 ProjectionElem::Subslice {
53 from: prefix.len() as u32,
54 to: if exact_size { min_length - suffix_len } else { suffix_len },
55 from_end: !exact_size,
56 },
57 );
58 match_pairs.push(MatchPair::new(subslice, subslice_pat));
59 }
60
61 match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
62 let end_offset = (idx + 1) as u32;
63 let elem = ProjectionElem::ConstantIndex {
64 offset: if exact_size { min_length - end_offset } else { end_offset },
65 min_length,
66 from_end: !exact_size,
67 };
ba9703b0 68 let place = tcx.mk_place_elem(*place, elem);
dfeec247
XL
69 MatchPair::new(place, subpattern)
70 }));
71 }
72
73 /// Creates a false edge to `imaginary_target` and a real edge to
74 /// real_target. If `imaginary_target` is none, or is the same as the real
75 /// target, a Goto is generated instead to simplify the generated MIR.
76 crate fn false_edges(
77 &mut self,
78 from_block: BasicBlock,
79 real_target: BasicBlock,
80 imaginary_target: Option<BasicBlock>,
81 source_info: SourceInfo,
82 ) {
83 match imaginary_target {
84 Some(target) if target != real_target => {
85 self.cfg.terminate(
86 from_block,
87 source_info,
f035d41b 88 TerminatorKind::FalseEdge { real_target, imaginary_target: target },
dfeec247
XL
89 );
90 }
91 _ => self.cfg.goto(from_block, source_info, real_target),
92 }
93 }
94}
95
96impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
97 crate fn new(place: Place<'tcx>, pattern: &'pat Pat<'tcx>) -> MatchPair<'pat, 'tcx> {
98 MatchPair { place, pattern }
99 }
100}