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