]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/matches/util.rs
Auto merge of #103880 - b-naber:field-ty-mir, r=lcnr
[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 rustc_middle::ty::TypeVisitable;
8 use smallvec::SmallVec;
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(self, fieldpat.field);
20
21                 MatchPair::new(place, &fieldpat.pattern, self)
22             })
23             .collect()
24     }
25
26     pub(crate) fn prefix_slice_suffix<'pat>(
27         &mut self,
28         match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
29         place: &PlaceBuilder<'tcx>,
30         prefix: &'pat [Box<Pat<'tcx>>],
31         opt_slice: &'pat Option<Box<Pat<'tcx>>>,
32         suffix: &'pat [Box<Pat<'tcx>>],
33     ) {
34         let tcx = self.tcx;
35         let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
36             match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
37                 ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
38                 _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
39             }
40         } else {
41             ((prefix.len() + suffix.len()).try_into().unwrap(), false)
42         };
43
44         match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
45             let elem =
46                 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
47             MatchPair::new(place.clone_project(elem), subpattern, self)
48         }));
49
50         if let Some(subslice_pat) = opt_slice {
51             let suffix_len = suffix.len() as u64;
52             let subslice = place.clone_project(PlaceElem::Subslice {
53                 from: prefix.len() as u64,
54                 to: if exact_size { min_length - suffix_len } else { suffix_len },
55                 from_end: !exact_size,
56             });
57             match_pairs.push(MatchPair::new(subslice, subslice_pat, self));
58         }
59
60         match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
61             let end_offset = (idx + 1) as u64;
62             let elem = ProjectionElem::ConstantIndex {
63                 offset: if exact_size { min_length - end_offset } else { end_offset },
64                 min_length,
65                 from_end: !exact_size,
66             };
67             let place = place.clone_project(elem);
68             MatchPair::new(place, subpattern, self)
69         }));
70     }
71
72     /// Creates a false edge to `imaginary_target` and a real edge to
73     /// real_target. If `imaginary_target` is none, or is the same as the real
74     /// target, a Goto is generated instead to simplify the generated MIR.
75     pub(crate) fn false_edges(
76         &mut self,
77         from_block: BasicBlock,
78         real_target: BasicBlock,
79         imaginary_target: Option<BasicBlock>,
80         source_info: SourceInfo,
81     ) {
82         match imaginary_target {
83             Some(target) if target != real_target => {
84                 self.cfg.terminate(
85                     from_block,
86                     source_info,
87                     TerminatorKind::FalseEdge { real_target, imaginary_target: target },
88                 );
89             }
90             _ => self.cfg.goto(from_block, source_info, real_target),
91         }
92     }
93 }
94
95 impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
96     pub(in crate::build) fn new(
97         mut place: PlaceBuilder<'tcx>,
98         pattern: &'pat Pat<'tcx>,
99         cx: &Builder<'_, 'tcx>,
100     ) -> MatchPair<'pat, 'tcx> {
101         // Force the place type to the pattern's type.
102         // FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
103         if let Some(resolved) = place.resolve_upvar(cx) {
104             place = resolved;
105         }
106
107         // Only add the OpaqueCast projection if the given place is an opaque type and the
108         // expected type from the pattern is not.
109         let may_need_cast = match place {
110             PlaceBuilder::Local { local, ref projection } => {
111                 let ty = Place::ty_from(local, projection, &cx.local_decls, cx.tcx).ty;
112                 ty != pattern.ty && ty.has_opaque_types()
113             }
114             _ => true,
115         };
116         if may_need_cast {
117             place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
118         }
119         MatchPair { place, pattern }
120     }
121 }