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::*;
7 use rustc_middle::ty::TypeVisitable;
8 use smallvec::SmallVec;
10 impl<'a, 'tcx> Builder<'a, 'tcx> {
11 pub(crate) fn field_match_pairs<'pat>(
13 place: PlaceBuilder<'tcx>,
14 subpatterns: &'pat [FieldPat<'tcx>],
15 ) -> Vec<MatchPair<'pat, 'tcx>> {
19 let place = place.clone().field(self, fieldpat.field);
21 MatchPair::new(place, &fieldpat.pattern, self)
26 pub(crate) fn prefix_slice_suffix<'pat>(
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>>],
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),
41 ((prefix.len() + suffix.len()).try_into().unwrap(), false)
44 match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
46 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
47 MatchPair::new(place.clone_project(elem), subpattern, self)
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,
57 match_pairs.push(MatchPair::new(subslice, subslice_pat, self));
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 },
65 from_end: !exact_size,
67 let place = place.clone_project(elem);
68 MatchPair::new(place, subpattern, self)
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(
77 from_block: BasicBlock,
78 real_target: BasicBlock,
79 imaginary_target: Option<BasicBlock>,
80 source_info: SourceInfo,
82 match imaginary_target {
83 Some(target) if target != real_target => {
87 TerminatorKind::FalseEdge { real_target, imaginary_target: target },
90 _ => self.cfg.goto(from_block, source_info, real_target),
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) {
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()
117 place = place.project(ProjectionElem::OpaqueCast(pattern.ty));
119 MatchPair { place, pattern }