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