Skip to main content

rustc_mir_build/builder/matches/
match_pair.rs

1use std::sync::Arc;
2
3use rustc_abi::FieldIdx;
4use rustc_middle::mir::*;
5use rustc_middle::span_bug;
6use rustc_middle::thir::*;
7use rustc_middle::ty::{self, Ty, TypeVisitableExt};
8
9use crate::builder::Builder;
10use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
11use crate::builder::matches::{
12    FlatPat, MatchPairTree, PatConstKind, PatternExtraData, SliceLenOp, TestableCase,
13};
14
15impl<'a, 'tcx> Builder<'a, 'tcx> {
16    /// Builds and pushes [`MatchPairTree`] subtrees, one for each pattern in
17    /// `subpatterns`, representing the fields of a [`PatKind::Variant`] or
18    /// [`PatKind::Leaf`].
19    ///
20    /// Used internally by [`MatchPairTree::for_pattern`].
21    fn field_match_pairs(
22        &mut self,
23        match_pairs: &mut Vec<MatchPairTree<'tcx>>,
24        extra_data: &mut PatternExtraData<'tcx>,
25        place: PlaceBuilder<'tcx>,
26        subpatterns: &[FieldPat<'tcx>],
27    ) {
28        for fieldpat in subpatterns {
29            let place = place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
30            MatchPairTree::for_pattern(place, &fieldpat.pattern, self, match_pairs, extra_data);
31        }
32    }
33
34    /// Builds [`MatchPairTree`] subtrees for the prefix/middle/suffix parts of an
35    /// array pattern or slice pattern, and adds those trees to `match_pairs`.
36    ///
37    /// Used internally by [`MatchPairTree::for_pattern`].
38    fn prefix_slice_suffix(
39        &mut self,
40        match_pairs: &mut Vec<MatchPairTree<'tcx>>,
41        extra_data: &mut PatternExtraData<'tcx>,
42        place: &PlaceBuilder<'tcx>,
43        array_len: Option<u64>,
44        prefix: &[Pat<'tcx>],
45        opt_slice: &Option<Box<Pat<'tcx>>>,
46        suffix: &[Pat<'tcx>],
47    ) {
48        let prefix_len = u64::try_from(prefix.len()).unwrap();
49        let suffix_len = u64::try_from(suffix.len()).unwrap();
50
51        // For slice patterns with a `..` followed by 0 or more suffix subpatterns,
52        // the actual slice index of those subpatterns isn't statically known, so
53        // we have to index them relative to the end of the slice.
54        //
55        // For array patterns, all subpatterns are indexed relative to the start.
56        let (min_length, is_array) = match array_len {
57            Some(len) => (len, true),
58            None => (prefix_len + suffix_len, false),
59        };
60
61        for (offset, subpattern) in (0u64..).zip(prefix) {
62            let elem = ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
63            let place = place.clone_project(elem);
64            MatchPairTree::for_pattern(place, subpattern, self, match_pairs, extra_data)
65        }
66
67        if let Some(subslice_pat) = opt_slice {
68            let subslice = place.clone_project(PlaceElem::Subslice {
69                from: prefix_len,
70                to: if is_array { min_length - suffix_len } else { suffix_len },
71                from_end: !is_array,
72            });
73            MatchPairTree::for_pattern(subslice, subslice_pat, self, match_pairs, extra_data);
74        }
75
76        for (end_offset, subpattern) in (1u64..).zip(suffix.iter().rev()) {
77            let elem = ProjectionElem::ConstantIndex {
78                offset: if is_array { min_length - end_offset } else { end_offset },
79                min_length,
80                from_end: !is_array,
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        if !cx.tcx.next_trait_solver_globally() {
105            // Only add the OpaqueCast projection if the given place is an opaque type and the
106            // expected type from the pattern is not.
107            let may_need_cast = match place_builder.base() {
108                PlaceBase::Local(local) => {
109                    let ty =
110                        Place::ty_from(local, place_builder.projection(), &cx.local_decls, cx.tcx)
111                            .ty;
112                    ty != pattern.ty && ty.has_opaque_types()
113                }
114                _ => true,
115            };
116            if may_need_cast {
117                place_builder = place_builder.project(ProjectionElem::OpaqueCast(pattern.ty));
118            }
119        }
120
121        let place = place_builder.try_to_place(cx);
122
123        // Apply any type ascriptions to the value at `match_pair.place`.
124        if let Some(place) = place
125            && let Some(extra) = &pattern.extra
126        {
127            for &Ascription { ref annotation, variance } in &extra.ascriptions {
128                extra_data.ascriptions.push(super::Ascription {
129                    source: place,
130                    annotation: annotation.clone(),
131                    variance,
132                });
133            }
134        }
135
136        let mut subpairs = Vec::new();
137        let testable_case = match pattern.kind {
138            PatKind::Missing | PatKind::Wild | PatKind::Error(_) => None,
139
140            PatKind::Or { ref pats } => {
141                let pats: Box<[FlatPat<'tcx>]> =
142                    pats.iter().map(|pat| FlatPat::new(place_builder.clone(), pat, cx)).collect();
143                if !pats[0].extra_data.bindings.is_empty() {
144                    // Hold a place for any bindings established in (possibly-nested) or-patterns.
145                    // By only holding a place when bindings are present, we skip over any
146                    // or-patterns that will be simplified by `merge_trivial_subcandidates`. In
147                    // other words, we can assume this expands into subcandidates.
148                    // FIXME(@dianne): this needs updating/removing if we always merge or-patterns
149                    extra_data.bindings.push(super::SubpatternBindings::FromOrPattern);
150                }
151                Some(TestableCase::Or { pats })
152            }
153
154            PatKind::Range(ref range) => {
155                if range.is_full_range(cx.tcx) == Some(true) {
156                    None
157                } else {
158                    Some(TestableCase::Range(Arc::clone(range)))
159                }
160            }
161
162            PatKind::Constant { value } => {
163                match (&pattern.ty, &value.ty) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(pattern.ty, value.ty);
164
165                // Classify the constant-pattern into further kinds, to
166                // reduce the number of ad-hoc type tests needed later on.
167                let pat_ty = pattern.ty;
168                let const_kind = if pat_ty.is_bool() {
169                    PatConstKind::Bool
170                } else if pat_ty.is_integral() || pat_ty.is_char() {
171                    PatConstKind::IntOrChar
172                } else if pat_ty.is_floating_point() {
173                    PatConstKind::Float
174                } else if pat_ty.is_str() {
175                    PatConstKind::String
176                } else {
177                    // FIXME(Zalathar): This still covers several different
178                    // categories (e.g. raw pointer, pattern-type)
179                    // which could be split out into their own kinds.
180                    PatConstKind::Other
181                };
182                Some(TestableCase::Constant { value, kind: const_kind })
183            }
184
185            PatKind::Binding { mode, var, is_shorthand, ref subpattern, .. } => {
186                // In order to please the borrow checker, when lowering a pattern
187                // like `x @ subpat` we must establish any bindings in `subpat`
188                // before establishing the binding for `x`.
189                //
190                // For example (from #69971):
191                //
192                // ```ignore (illustrative)
193                // struct NonCopyStruct {
194                //     copy_field: u32,
195                // }
196                //
197                // fn foo1(x: NonCopyStruct) {
198                //     let y @ NonCopyStruct { copy_field: z } = x;
199                //     // the above should turn into
200                //     let z = x.copy_field;
201                //     let y = x;
202                // }
203                // ```
204
205                // First, recurse into the subpattern, if any.
206                if let Some(subpattern) = subpattern.as_ref() {
207                    // this is the `x @ P` case; have to keep matching against `P` now
208                    MatchPairTree::for_pattern(
209                        place_builder,
210                        subpattern,
211                        cx,
212                        &mut subpairs,
213                        extra_data,
214                    );
215                }
216
217                // Then push this binding, after any bindings in the subpattern.
218                if let Some(source) = place {
219                    extra_data.bindings.push(super::SubpatternBindings::One(super::Binding {
220                        span: pattern.span,
221                        source,
222                        var_id: var,
223                        binding_mode: mode,
224                        is_shorthand,
225                    }));
226                }
227
228                None
229            }
230
231            PatKind::Array { ref prefix, ref slice, ref suffix } => {
232                // Determine the statically-known length of the array type being matched.
233                // This should always succeed for legal programs, but could fail for
234                // erroneous programs (e.g. the type is `[u8; const { panic!() }]`),
235                // so take care not to ICE if this fails.
236                let array_len = match pattern.ty.kind() {
237                    ty::Array(_, len) => len.try_to_target_usize(cx.tcx),
238                    _ => None,
239                };
240                if let Some(array_len) = array_len {
241                    cx.prefix_slice_suffix(
242                        &mut subpairs,
243                        extra_data,
244                        &place_builder,
245                        Some(array_len),
246                        prefix,
247                        slice,
248                        suffix,
249                    );
250                } else {
251                    // If the array length couldn't be determined, ignore the
252                    // subpatterns and delayed-assert that compilation will fail.
253                    cx.tcx.dcx().span_delayed_bug(
254                        pattern.span,
255                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("array length in pattern couldn\'t be determined for ty={0:?}",
                pattern.ty))
    })format!(
256                            "array length in pattern couldn't be determined for ty={:?}",
257                            pattern.ty
258                        ),
259                    );
260                }
261
262                None
263            }
264            PatKind::Slice { ref prefix, ref slice, ref suffix } => {
265                cx.prefix_slice_suffix(
266                    &mut subpairs,
267                    extra_data,
268                    &place_builder,
269                    None,
270                    prefix,
271                    slice,
272                    suffix,
273                );
274
275                if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
276                    // This pattern is shaped like `[..]`. It can match a slice
277                    // of any length, so no length test is needed.
278                    None
279                } else {
280                    // Any other shape of slice pattern requires a length test.
281                    // Slice patterns with a `..` subpattern require a minimum
282                    // length; those without `..` require an exact length.
283                    Some(TestableCase::Slice {
284                        len: u64::try_from(prefix.len() + suffix.len()).unwrap(),
285                        op: if slice.is_some() {
286                            SliceLenOp::GreaterOrEqual
287                        } else {
288                            SliceLenOp::Equal
289                        },
290                    })
291                }
292            }
293
294            PatKind::Variant { adt_def, variant_index, args, ref subpatterns } => {
295                let downcast_place = place_builder.downcast(adt_def, variant_index); // `(x as Variant)`
296                cx.field_match_pairs(&mut subpairs, extra_data, downcast_place, subpatterns);
297
298                let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
299                    i == variant_index
300                        || !v.inhabited_predicate(cx.tcx, adt_def).instantiate(cx.tcx, args).apply(
301                            cx.tcx,
302                            cx.infcx.typing_env(cx.param_env),
303                            cx.def_id.into(),
304                        )
305                }) && !adt_def.variant_list_has_applicable_non_exhaustive();
306                if irrefutable {
307                    None
308                } else {
309                    Some(TestableCase::Variant { adt_def, variant_index })
310                }
311            }
312
313            PatKind::Leaf { ref subpatterns } => {
314                cx.field_match_pairs(&mut subpairs, extra_data, place_builder, subpatterns);
315                None
316            }
317
318            PatKind::Deref { pin: Pinnedness::Pinned, ref subpattern } => {
319                let pinned_ref_ty = match pattern.ty.pinned_ty() {
320                    Some(p_ty) if p_ty.is_ref() => p_ty,
321                    _ => ::rustc_middle::util::bug::span_bug_fmt(pattern.span,
    format_args!("bad type for pinned deref: {0:?}", pattern.ty))span_bug!(pattern.span, "bad type for pinned deref: {:?}", pattern.ty),
322                };
323                MatchPairTree::for_pattern(
324                    // Project into the `Pin(_)` struct, then deref the inner `&` or `&mut`.
325                    place_builder.field(FieldIdx::ZERO, pinned_ref_ty).deref(),
326                    subpattern,
327                    cx,
328                    &mut subpairs,
329                    extra_data,
330                );
331
332                None
333            }
334
335            PatKind::Deref { pin: Pinnedness::Not, ref subpattern }
336            | PatKind::DerefPattern { ref subpattern, borrow: DerefPatBorrowMode::Box } => {
337                MatchPairTree::for_pattern(
338                    place_builder.deref(),
339                    subpattern,
340                    cx,
341                    &mut subpairs,
342                    extra_data,
343                );
344                None
345            }
346
347            PatKind::DerefPattern {
348                ref subpattern,
349                borrow: DerefPatBorrowMode::Borrow(mutability),
350            } => {
351                // Create a new temporary for each deref pattern.
352                // FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?
353                let temp = cx.temp(
354                    Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, subpattern.ty, mutability),
355                    pattern.span,
356                );
357                MatchPairTree::for_pattern(
358                    PlaceBuilder::from(temp).deref(),
359                    subpattern,
360                    cx,
361                    &mut subpairs,
362                    extra_data,
363                );
364                Some(TestableCase::Deref { temp, mutability })
365            }
366
367            PatKind::Never => Some(TestableCase::Never),
368        };
369
370        if let Some(testable_case) = testable_case {
371            // This pattern is refutable, so push a new match-pair node.
372            //
373            // Note: unless test_case is TestCase::Or, place must not be None.
374            // This means that the closure capture analysis in
375            // rustc_hir_typeck::upvar, and in particular the pattern handling
376            // code of ExprUseVisitor, must capture all of the places we'll use.
377            // Make sure to keep these two parts in sync!
378            match_pairs.push(MatchPairTree {
379                place,
380                testable_case,
381                subpairs,
382                pattern_ty: pattern.ty,
383                pattern_span: pattern.span,
384            })
385        } else {
386            // This pattern is irrefutable, so it doesn't need its own match-pair node.
387            // Just push its refutable subpatterns instead, if any.
388            match_pairs.extend(subpairs);
389        }
390    }
391}