rustc_mir_build/builder/matches/
match_pair.rs

1use std::sync::Arc;
2
3use rustc_middle::mir::*;
4use rustc_middle::thir::*;
5use rustc_middle::ty::{self, Ty, TypeVisitableExt};
6
7use crate::builder::Builder;
8use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
9use crate::builder::matches::{FlatPat, MatchPairTree, PatternExtraData, TestCase};
10
11impl<'a, 'tcx> Builder<'a, 'tcx> {
12    /// Builds and pushes [`MatchPairTree`] subtrees, one for each pattern in
13    /// `subpatterns`, representing the fields of a [`PatKind::Variant`] or
14    /// [`PatKind::Leaf`].
15    ///
16    /// Used internally by [`MatchPairTree::for_pattern`].
17    fn field_match_pairs(
18        &mut self,
19        match_pairs: &mut Vec<MatchPairTree<'tcx>>,
20        extra_data: &mut PatternExtraData<'tcx>,
21        place: PlaceBuilder<'tcx>,
22        subpatterns: &[FieldPat<'tcx>],
23    ) {
24        for fieldpat in subpatterns {
25            let place = place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
26            MatchPairTree::for_pattern(place, &fieldpat.pattern, self, match_pairs, extra_data);
27        }
28    }
29
30    /// Builds [`MatchPairTree`] subtrees for the prefix/middle/suffix parts of an
31    /// array pattern or slice pattern, and adds those trees to `match_pairs`.
32    ///
33    /// Used internally by [`MatchPairTree::for_pattern`].
34    fn prefix_slice_suffix(
35        &mut self,
36        match_pairs: &mut Vec<MatchPairTree<'tcx>>,
37        extra_data: &mut PatternExtraData<'tcx>,
38        place: &PlaceBuilder<'tcx>,
39        prefix: &[Pat<'tcx>],
40        opt_slice: &Option<Box<Pat<'tcx>>>,
41        suffix: &[Pat<'tcx>],
42    ) {
43        let tcx = self.tcx;
44        let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
45            match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
46                ty::Array(_, length) => (
47                    length
48                        .try_to_target_usize(tcx)
49                        .expect("expected len of array pat to be definite"),
50                    true,
51                ),
52                _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
53            }
54        } else {
55            ((prefix.len() + suffix.len()).try_into().unwrap(), false)
56        };
57
58        for (idx, subpattern) in prefix.iter().enumerate() {
59            let elem =
60                ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
61            let place = place.clone_project(elem);
62            MatchPairTree::for_pattern(place, subpattern, self, match_pairs, extra_data)
63        }
64
65        if let Some(subslice_pat) = opt_slice {
66            let suffix_len = suffix.len() as u64;
67            let subslice = place.clone_project(PlaceElem::Subslice {
68                from: prefix.len() as u64,
69                to: if exact_size { min_length - suffix_len } else { suffix_len },
70                from_end: !exact_size,
71            });
72            MatchPairTree::for_pattern(subslice, subslice_pat, self, match_pairs, extra_data);
73        }
74
75        for (idx, subpattern) in suffix.iter().rev().enumerate() {
76            let end_offset = (idx + 1) as u64;
77            let elem = ProjectionElem::ConstantIndex {
78                offset: if exact_size { min_length - end_offset } else { end_offset },
79                min_length,
80                from_end: !exact_size,
81            };
82            let place = place.clone_project(elem);
83            MatchPairTree::for_pattern(place, subpattern, self, match_pairs, extra_data)
84        }
85    }
86}
87
88impl<'tcx> MatchPairTree<'tcx> {
89    /// Recursively builds a match pair tree for the given pattern and its
90    /// subpatterns.
91    pub(super) fn for_pattern(
92        mut place_builder: PlaceBuilder<'tcx>,
93        pattern: &Pat<'tcx>,
94        cx: &mut Builder<'_, 'tcx>,
95        match_pairs: &mut Vec<Self>, // Newly-created nodes are added to this vector
96        extra_data: &mut PatternExtraData<'tcx>, // Bindings/ascriptions are added here
97    ) {
98        // Force the place type to the pattern's type.
99        // FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
100        if let Some(resolved) = place_builder.resolve_upvar(cx) {
101            place_builder = resolved;
102        }
103
104        // Only add the OpaqueCast projection if the given place is an opaque type and the
105        // expected type from the pattern is not.
106        let may_need_cast = match place_builder.base() {
107            PlaceBase::Local(local) => {
108                let ty =
109                    Place::ty_from(local, place_builder.projection(), &cx.local_decls, cx.tcx).ty;
110                ty != pattern.ty && ty.has_opaque_types()
111            }
112            _ => true,
113        };
114        if may_need_cast {
115            place_builder = place_builder.project(ProjectionElem::OpaqueCast(pattern.ty));
116        }
117
118        let place = place_builder.try_to_place(cx);
119        let mut subpairs = Vec::new();
120        let test_case = match pattern.kind {
121            PatKind::Wild | PatKind::Error(_) => None,
122
123            PatKind::Or { ref pats } => Some(TestCase::Or {
124                pats: pats.iter().map(|pat| FlatPat::new(place_builder.clone(), pat, cx)).collect(),
125            }),
126
127            PatKind::Range(ref range) => {
128                if range.is_full_range(cx.tcx) == Some(true) {
129                    None
130                } else {
131                    Some(TestCase::Range(Arc::clone(range)))
132                }
133            }
134
135            PatKind::Constant { value } => Some(TestCase::Constant { value }),
136
137            PatKind::AscribeUserType {
138                ascription: Ascription { ref annotation, variance },
139                ref subpattern,
140                ..
141            } => {
142                MatchPairTree::for_pattern(
143                    place_builder,
144                    subpattern,
145                    cx,
146                    &mut subpairs,
147                    extra_data,
148                );
149
150                // Apply the type ascription to the value at `match_pair.place`
151                if let Some(source) = place {
152                    let annotation = annotation.clone();
153                    extra_data.ascriptions.push(super::Ascription { source, annotation, variance });
154                }
155
156                None
157            }
158
159            PatKind::Binding { mode, var, ref subpattern, .. } => {
160                // In order to please the borrow checker, when lowering a pattern
161                // like `x @ subpat` we must establish any bindings in `subpat`
162                // before establishing the binding for `x`.
163                //
164                // For example (from #69971):
165                //
166                // ```ignore (illustrative)
167                // struct NonCopyStruct {
168                //     copy_field: u32,
169                // }
170                //
171                // fn foo1(x: NonCopyStruct) {
172                //     let y @ NonCopyStruct { copy_field: z } = x;
173                //     // the above should turn into
174                //     let z = x.copy_field;
175                //     let y = x;
176                // }
177                // ```
178
179                // First, recurse into the subpattern, if any.
180                if let Some(subpattern) = subpattern.as_ref() {
181                    // this is the `x @ P` case; have to keep matching against `P` now
182                    MatchPairTree::for_pattern(
183                        place_builder,
184                        subpattern,
185                        cx,
186                        &mut subpairs,
187                        extra_data,
188                    );
189                }
190
191                // Then push this binding, after any bindings in the subpattern.
192                if let Some(source) = place {
193                    extra_data.bindings.push(super::Binding {
194                        span: pattern.span,
195                        source,
196                        var_id: var,
197                        binding_mode: mode,
198                    });
199                }
200
201                None
202            }
203
204            PatKind::ExpandedConstant { subpattern: ref pattern, def_id: _, is_inline: false } => {
205                MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
206                None
207            }
208            PatKind::ExpandedConstant { subpattern: ref pattern, def_id, is_inline: true } => {
209                MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
210
211                // Apply a type ascription for the inline constant to the value at `match_pair.place`
212                if let Some(source) = place {
213                    let span = pattern.span;
214                    let parent_id = cx.tcx.typeck_root_def_id(cx.def_id.to_def_id());
215                    let args = ty::InlineConstArgs::new(
216                        cx.tcx,
217                        ty::InlineConstArgsParts {
218                            parent_args: ty::GenericArgs::identity_for_item(cx.tcx, parent_id),
219                            ty: cx.infcx.next_ty_var(span),
220                        },
221                    )
222                    .args;
223                    let user_ty = cx.infcx.canonicalize_user_type_annotation(ty::UserType::new(
224                        ty::UserTypeKind::TypeOf(def_id, ty::UserArgs { args, user_self_ty: None }),
225                    ));
226                    let annotation = ty::CanonicalUserTypeAnnotation {
227                        inferred_ty: pattern.ty,
228                        span,
229                        user_ty: Box::new(user_ty),
230                    };
231                    let variance = ty::Contravariant;
232                    extra_data.ascriptions.push(super::Ascription { annotation, source, variance });
233                }
234
235                None
236            }
237
238            PatKind::Array { ref prefix, ref slice, ref suffix } => {
239                cx.prefix_slice_suffix(
240                    &mut subpairs,
241                    extra_data,
242                    &place_builder,
243                    prefix,
244                    slice,
245                    suffix,
246                );
247                None
248            }
249            PatKind::Slice { ref prefix, ref slice, ref suffix } => {
250                cx.prefix_slice_suffix(
251                    &mut subpairs,
252                    extra_data,
253                    &place_builder,
254                    prefix,
255                    slice,
256                    suffix,
257                );
258
259                if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
260                    None
261                } else {
262                    Some(TestCase::Slice {
263                        len: prefix.len() + suffix.len(),
264                        variable_length: slice.is_some(),
265                    })
266                }
267            }
268
269            PatKind::Variant { adt_def, variant_index, args, ref subpatterns } => {
270                let downcast_place = place_builder.downcast(adt_def, variant_index); // `(x as Variant)`
271                cx.field_match_pairs(&mut subpairs, extra_data, downcast_place, subpatterns);
272
273                let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
274                    i == variant_index
275                        || !v.inhabited_predicate(cx.tcx, adt_def).instantiate(cx.tcx, args).apply(
276                            cx.tcx,
277                            cx.infcx.typing_env(cx.param_env),
278                            cx.def_id.into(),
279                        )
280                }) && !adt_def.variant_list_has_applicable_non_exhaustive();
281                if irrefutable { None } else { Some(TestCase::Variant { adt_def, variant_index }) }
282            }
283
284            PatKind::Leaf { ref subpatterns } => {
285                cx.field_match_pairs(&mut subpairs, extra_data, place_builder, subpatterns);
286                None
287            }
288
289            PatKind::Deref { ref subpattern } => {
290                MatchPairTree::for_pattern(
291                    place_builder.deref(),
292                    subpattern,
293                    cx,
294                    &mut subpairs,
295                    extra_data,
296                );
297                None
298            }
299
300            PatKind::DerefPattern { ref subpattern, mutability } => {
301                // Create a new temporary for each deref pattern.
302                // FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?
303                let temp = cx.temp(
304                    Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, subpattern.ty, mutability),
305                    pattern.span,
306                );
307                MatchPairTree::for_pattern(
308                    PlaceBuilder::from(temp).deref(),
309                    subpattern,
310                    cx,
311                    &mut subpairs,
312                    extra_data,
313                );
314                Some(TestCase::Deref { temp, mutability })
315            }
316
317            PatKind::Never => Some(TestCase::Never),
318        };
319
320        if let Some(test_case) = test_case {
321            // This pattern is refutable, so push a new match-pair node.
322            match_pairs.push(MatchPairTree {
323                place,
324                test_case,
325                subpairs,
326                pattern_ty: pattern.ty,
327                pattern_span: pattern.span,
328            })
329        } else {
330            // This pattern is irrefutable, so it doesn't need its own match-pair node.
331            // Just push its refutable subpatterns instead, if any.
332            match_pairs.extend(subpairs);
333        }
334    }
335}