1 //! Simplifying Candidates
3 //! *Simplifying* a match pair `place @ pattern` means breaking it down
4 //! into bindings or other, simpler match pairs. For example:
6 //! - `place @ (P1, P2)` can be simplified to `[place.0 @ P1, place.1 @ P2]`
7 //! - `place @ x` can be simplified to `[]` by binding `x` to `place`
9 //! The `simplify_candidate` routine just repeatedly applies these
10 //! sort of simplifications until there is nothing left to
11 //! simplify. Match pairs cannot be simplified if they require some
12 //! sort of test: for example, testing which variant an enum is, or
13 //! testing a value against a constant.
15 use crate::build
::expr
::as_place
::PlaceBuilder
;
16 use crate::build
::matches
::{Ascription, Binding, Candidate, MatchPair}
;
17 use crate::build
::Builder
;
18 use rustc_middle
::thir
::{self, *}
;
22 impl<'a
, 'tcx
> Builder
<'a
, 'tcx
> {
23 /// Simplify a candidate so that all match pairs require a test.
25 /// This method will also split a candidate, in which the only
26 /// match-pair is an or-pattern, into multiple candidates.
34 /// only generates a single switch. If this happens this method returns
36 #[instrument(skip(self, candidate), level = "debug")]
37 pub(super) fn simplify_candidate
<'pat
>(
39 candidate
: &mut Candidate
<'pat
, 'tcx
>,
41 // repeatedly simplify match pairs until fixed point is reached
42 debug
!("{candidate:#?}");
44 // existing_bindings and new_bindings exists to keep the semantics in order.
45 // Reversing the binding order for bindings after `@` changes the binding order in places
46 // it shouldn't be changed, for example `let (Some(a), Some(b)) = (x, y)`
48 // To avoid this, the binding occurs in the following manner:
49 // * the bindings for one iteration of the following loop occurs in order (i.e. left to
51 // * the bindings from the previous iteration of the loop is prepended to the bindings from
52 // the current iteration (in the implementation this is done by mem::swap and extend)
53 // * after all iterations, these new bindings are then appended to the bindings that were
54 // preexisting (i.e. `candidate.binding` when the function was called).
57 // candidate.bindings = [1, 2, 3]
58 // binding in iter 1: [4, 5]
59 // binding in iter 2: [6, 7]
61 // final binding: [1, 2, 3, 6, 7, 4, 5]
62 let mut existing_bindings
= mem
::take(&mut candidate
.bindings
);
63 let mut new_bindings
= Vec
::new();
65 let match_pairs
= mem
::take(&mut candidate
.match_pairs
);
67 if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }
, .. }, place
}] =
70 existing_bindings
.extend_from_slice(&new_bindings
);
71 mem
::swap(&mut candidate
.bindings
, &mut existing_bindings
);
72 candidate
.subcandidates
= self.create_or_subcandidates(candidate
, &place
, pats
);
76 let mut changed
= false;
77 for match_pair
in match_pairs
{
78 match self.simplify_match_pair(match_pair
, candidate
) {
83 candidate
.match_pairs
.push(match_pair
);
87 // Avoid issue #69971: the binding order should be right to left if there are more
88 // bindings after `@` to please the borrow checker
90 // struct NonCopyStruct {
94 // fn foo1(x: NonCopyStruct) {
95 // let y @ NonCopyStruct { copy_field: z } = x;
96 // // the above should turn into
97 // let z = x.copy_field;
100 candidate
.bindings
.extend_from_slice(&new_bindings
);
101 mem
::swap(&mut candidate
.bindings
, &mut new_bindings
);
102 candidate
.bindings
.clear();
105 existing_bindings
.extend_from_slice(&new_bindings
);
106 mem
::swap(&mut candidate
.bindings
, &mut existing_bindings
);
107 // Move or-patterns to the end, because they can result in us
108 // creating additional candidates, so we want to test them as
112 .sort_by_key(|pair
| matches
!(pair
.pattern
.kind
, PatKind
::Or { .. }
));
113 debug
!(simplified
= ?candidate
, "simplify_candidate");
114 return false; // if we were not able to simplify any, done.
119 /// Given `candidate` that has a single or-pattern for its match-pairs,
120 /// creates a fresh candidate for each of its input subpatterns passed via
122 fn create_or_subcandidates
<'pat
>(
124 candidate
: &Candidate
<'pat
, 'tcx
>,
125 place
: &PlaceBuilder
<'tcx
>,
126 pats
: &'pat
[Box
<Pat
<'tcx
>>],
127 ) -> Vec
<Candidate
<'pat
, 'tcx
>> {
130 let mut candidate
= Candidate
::new(place
.clone(), pat
, candidate
.has_guard
, self);
131 self.simplify_candidate(&mut candidate
);
137 /// Tries to simplify `match_pair`, returning `Ok(())` if
138 /// successful. If successful, new match pairs and bindings will
139 /// have been pushed into the candidate. If no simplification is
140 /// possible, `Err` is returned and no changes are made to
142 fn simplify_match_pair
<'pat
>(
144 match_pair
: MatchPair
<'pat
, 'tcx
>,
145 candidate
: &mut Candidate
<'pat
, 'tcx
>,
146 ) -> Result
<(), MatchPair
<'pat
, 'tcx
>> {
147 match match_pair
.pattern
.kind
{
148 PatKind
::AscribeUserType
{
150 ascription
: thir
::Ascription { ref annotation, variance }
,
152 // Apply the type ascription to the value at `match_pair.place`, which is the
153 if let Some(source
) = match_pair
.place
.try_to_place(self) {
154 candidate
.ascriptions
.push(Ascription
{
155 annotation
: annotation
.clone(),
161 candidate
.match_pairs
.push(MatchPair
::new(match_pair
.place
, subpattern
, self));
166 PatKind
::Wild
| PatKind
::Error(_
) => {
167 // nothing left to do
180 if let Some(source
) = match_pair
.place
.try_to_place(self) {
181 candidate
.bindings
.push(Binding
{
182 span
: match_pair
.pattern
.span
,
189 if let Some(subpattern
) = subpattern
.as_ref() {
190 // this is the `x @ P` case; have to keep matching against `P` now
191 candidate
.match_pairs
.push(MatchPair
::new(match_pair
.place
, subpattern
, self));
197 PatKind
::Constant { .. }
=> {
198 // FIXME normalize patterns when possible
202 PatKind
::InlineConstant { subpattern: ref pattern, def: _ }
=> {
203 candidate
.match_pairs
.push(MatchPair
::new(match_pair
.place
, pattern
, self));
208 PatKind
::Range(ref range
) => {
209 if let Some(true) = range
.is_full_range(self.tcx
) {
210 // Irrefutable pattern match.
216 PatKind
::Slice { ref prefix, ref slice, ref suffix }
=> {
217 if prefix
.is_empty() && slice
.is_some() && suffix
.is_empty() {
219 self.prefix_slice_suffix(
220 &mut candidate
.match_pairs
,
232 PatKind
::Variant { adt_def, args, variant_index, ref subpatterns }
=> {
233 let irrefutable
= adt_def
.variants().iter_enumerated().all(|(i
, v
)| {
234 i
== variant_index
|| {
235 self.tcx
.features().exhaustive_patterns
237 .inhabited_predicate(self.tcx
, adt_def
)
238 .instantiate(self.tcx
, args
)
239 .apply_ignore_module(self.tcx
, self.param_env
)
241 }) && (adt_def
.did().is_local()
242 || !adt_def
.is_variant_list_non_exhaustive());
244 let place_builder
= match_pair
.place
.downcast(adt_def
, variant_index
);
247 .extend(self.field_match_pairs(place_builder
, subpatterns
));
254 PatKind
::Array { ref prefix, ref slice, ref suffix }
=> {
255 self.prefix_slice_suffix(
256 &mut candidate
.match_pairs
,
265 PatKind
::Leaf { ref subpatterns }
=> {
266 // tuple struct, match subpats (if any)
267 candidate
.match_pairs
.extend(self.field_match_pairs(match_pair
.place
, subpatterns
));
271 PatKind
::Deref { ref subpattern }
=> {
272 let place_builder
= match_pair
.place
.deref();
273 candidate
.match_pairs
.push(MatchPair
::new(place_builder
, subpattern
, self));
277 PatKind
::Or { .. }
=> Err(match_pair
),