rustc_mir_build/builder/matches/
match_pair.rs1use std::sync::Arc;
2
3use rustc_hir::ByRef;
4use rustc_middle::mir::*;
5use rustc_middle::thir::*;
6use rustc_middle::ty::{self, Ty, TypeVisitableExt};
7
8use crate::builder::Builder;
9use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
10use crate::builder::matches::{FlatPat, MatchPairTree, PatternExtraData, TestCase};
11
12impl<'a, 'tcx> Builder<'a, 'tcx> {
13 fn field_match_pairs(
19 &mut self,
20 match_pairs: &mut Vec<MatchPairTree<'tcx>>,
21 extra_data: &mut PatternExtraData<'tcx>,
22 place: PlaceBuilder<'tcx>,
23 subpatterns: &[FieldPat<'tcx>],
24 ) {
25 for fieldpat in subpatterns {
26 let place = place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
27 MatchPairTree::for_pattern(place, &fieldpat.pattern, self, match_pairs, extra_data);
28 }
29 }
30
31 fn prefix_slice_suffix(
36 &mut self,
37 match_pairs: &mut Vec<MatchPairTree<'tcx>>,
38 extra_data: &mut PatternExtraData<'tcx>,
39 place: &PlaceBuilder<'tcx>,
40 prefix: &[Pat<'tcx>],
41 opt_slice: &Option<Box<Pat<'tcx>>>,
42 suffix: &[Pat<'tcx>],
43 ) {
44 let tcx = self.tcx;
45 let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
46 match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
47 ty::Array(_, length) => (
48 length
49 .try_to_target_usize(tcx)
50 .expect("expected len of array pat to be definite"),
51 true,
52 ),
53 _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
54 }
55 } else {
56 ((prefix.len() + suffix.len()).try_into().unwrap(), false)
57 };
58
59 for (idx, subpattern) in prefix.iter().enumerate() {
60 let elem =
61 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
62 let place = place.clone_project(elem);
63 MatchPairTree::for_pattern(place, subpattern, self, match_pairs, extra_data)
64 }
65
66 if let Some(subslice_pat) = opt_slice {
67 let suffix_len = suffix.len() as u64;
68 let subslice = place.clone_project(PlaceElem::Subslice {
69 from: prefix.len() as u64,
70 to: if exact_size { min_length - suffix_len } else { suffix_len },
71 from_end: !exact_size,
72 });
73 MatchPairTree::for_pattern(subslice, subslice_pat, self, match_pairs, extra_data);
74 }
75
76 for (idx, subpattern) in suffix.iter().rev().enumerate() {
77 let end_offset = (idx + 1) as u64;
78 let elem = ProjectionElem::ConstantIndex {
79 offset: if exact_size { min_length - end_offset } else { end_offset },
80 min_length,
81 from_end: !exact_size,
82 };
83 let place = place.clone_project(elem);
84 MatchPairTree::for_pattern(place, subpattern, self, match_pairs, extra_data)
85 }
86 }
87}
88
89impl<'tcx> MatchPairTree<'tcx> {
90 pub(super) fn for_pattern(
93 mut place_builder: PlaceBuilder<'tcx>,
94 pattern: &Pat<'tcx>,
95 cx: &mut Builder<'_, 'tcx>,
96 match_pairs: &mut Vec<Self>, extra_data: &mut PatternExtraData<'tcx>, ) {
99 if let Some(resolved) = place_builder.resolve_upvar(cx) {
102 place_builder = resolved;
103 }
104
105 if !cx.tcx.next_trait_solver_globally() {
106 let may_need_cast = match place_builder.base() {
109 PlaceBase::Local(local) => {
110 let ty =
111 Place::ty_from(local, place_builder.projection(), &cx.local_decls, cx.tcx)
112 .ty;
113 ty != pattern.ty && ty.has_opaque_types()
114 }
115 _ => true,
116 };
117 if may_need_cast {
118 place_builder = place_builder.project(ProjectionElem::OpaqueCast(pattern.ty));
119 }
120 }
121
122 let place = place_builder.try_to_place(cx);
123 let mut subpairs = Vec::new();
124 let test_case = match pattern.kind {
125 PatKind::Missing | PatKind::Wild | PatKind::Error(_) => None,
126
127 PatKind::Or { ref pats } => Some(TestCase::Or {
128 pats: pats.iter().map(|pat| FlatPat::new(place_builder.clone(), pat, cx)).collect(),
129 }),
130
131 PatKind::Range(ref range) => {
132 if range.is_full_range(cx.tcx) == Some(true) {
133 None
134 } else {
135 Some(TestCase::Range(Arc::clone(range)))
136 }
137 }
138
139 PatKind::Constant { value } => Some(TestCase::Constant { value }),
140
141 PatKind::AscribeUserType {
142 ascription: Ascription { ref annotation, variance },
143 ref subpattern,
144 ..
145 } => {
146 MatchPairTree::for_pattern(
147 place_builder,
148 subpattern,
149 cx,
150 &mut subpairs,
151 extra_data,
152 );
153
154 if let Some(source) = place {
156 let annotation = annotation.clone();
157 extra_data.ascriptions.push(super::Ascription { source, annotation, variance });
158 }
159
160 None
161 }
162
163 PatKind::Binding { mode, var, ref subpattern, .. } => {
164 if let Some(subpattern) = subpattern.as_ref() {
185 MatchPairTree::for_pattern(
187 place_builder,
188 subpattern,
189 cx,
190 &mut subpairs,
191 extra_data,
192 );
193 }
194
195 if let Some(source) = place {
197 extra_data.bindings.push(super::Binding {
198 span: pattern.span,
199 source,
200 var_id: var,
201 binding_mode: mode,
202 });
203 }
204
205 None
206 }
207
208 PatKind::ExpandedConstant { subpattern: ref pattern, .. } => {
209 MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
210 None
211 }
212
213 PatKind::Array { ref prefix, ref slice, ref suffix } => {
214 cx.prefix_slice_suffix(
215 &mut subpairs,
216 extra_data,
217 &place_builder,
218 prefix,
219 slice,
220 suffix,
221 );
222 None
223 }
224 PatKind::Slice { ref prefix, ref slice, ref suffix } => {
225 cx.prefix_slice_suffix(
226 &mut subpairs,
227 extra_data,
228 &place_builder,
229 prefix,
230 slice,
231 suffix,
232 );
233
234 if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
235 None
236 } else {
237 Some(TestCase::Slice {
238 len: prefix.len() + suffix.len(),
239 variable_length: slice.is_some(),
240 })
241 }
242 }
243
244 PatKind::Variant { adt_def, variant_index, args, ref subpatterns } => {
245 let downcast_place = place_builder.downcast(adt_def, variant_index); cx.field_match_pairs(&mut subpairs, extra_data, downcast_place, subpatterns);
247
248 let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
249 i == variant_index
250 || !v.inhabited_predicate(cx.tcx, adt_def).instantiate(cx.tcx, args).apply(
251 cx.tcx,
252 cx.infcx.typing_env(cx.param_env),
253 cx.def_id.into(),
254 )
255 }) && !adt_def.variant_list_has_applicable_non_exhaustive();
256 if irrefutable { None } else { Some(TestCase::Variant { adt_def, variant_index }) }
257 }
258
259 PatKind::Leaf { ref subpatterns } => {
260 cx.field_match_pairs(&mut subpairs, extra_data, place_builder, subpatterns);
261 None
262 }
263
264 PatKind::Deref { ref subpattern }
265 | PatKind::DerefPattern { ref subpattern, borrow: ByRef::No } => {
266 if cfg!(debug_assertions) && matches!(pattern.kind, PatKind::DerefPattern { .. }) {
267 debug_assert!(pattern.ty.is_box());
269 }
270
271 MatchPairTree::for_pattern(
272 place_builder.deref(),
273 subpattern,
274 cx,
275 &mut subpairs,
276 extra_data,
277 );
278 None
279 }
280
281 PatKind::DerefPattern { ref subpattern, borrow: ByRef::Yes(mutability) } => {
282 let temp = cx.temp(
285 Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, subpattern.ty, mutability),
286 pattern.span,
287 );
288 MatchPairTree::for_pattern(
289 PlaceBuilder::from(temp).deref(),
290 subpattern,
291 cx,
292 &mut subpairs,
293 extra_data,
294 );
295 Some(TestCase::Deref { temp, mutability })
296 }
297
298 PatKind::Never => Some(TestCase::Never),
299 };
300
301 if let Some(test_case) = test_case {
302 match_pairs.push(MatchPairTree {
304 place,
305 test_case,
306 subpairs,
307 pattern_ty: pattern.ty,
308 pattern_span: pattern.span,
309 })
310 } else {
311 match_pairs.extend(subpairs);
314 }
315 }
316}