rustc_mir_build/builder/matches/buckets.rs
1use std::cmp::Ordering;
2
3use rustc_data_structures::fx::FxIndexMap;
4use rustc_middle::mir::Place;
5use rustc_middle::span_bug;
6use tracing::debug;
7
8use crate::builder::Builder;
9use crate::builder::matches::{
10 Candidate, PatConstKind, SliceLenOp, Test, TestBranch, TestKind, TestableCase,
11};
12
13/// Output of [`Builder::partition_candidates_into_buckets`].
14pub(crate) struct PartitionedCandidates<'tcx, 'b, 'c> {
15 /// For each possible outcome of the test, the candidates that are matched in that outcome.
16 pub(crate) target_candidates: FxIndexMap<TestBranch<'tcx>, Vec<&'b mut Candidate<'tcx>>>,
17 /// The remaining candidates that weren't associated with any test outcome.
18 pub(crate) remaining_candidates: &'b mut [&'c mut Candidate<'tcx>],
19}
20
21impl<'a, 'tcx> Builder<'a, 'tcx> {
22 /// Given a test, we partition the input candidates into several buckets.
23 /// If a candidate matches in exactly one of the branches of `test`
24 /// (and no other branches), we put it into the corresponding bucket.
25 /// If it could match in more than one of the branches of `test`, the test
26 /// doesn't usefully apply to it, and we stop partitioning candidates.
27 ///
28 /// Importantly, we also **mutate** the branched candidates to remove match pairs
29 /// that are entailed by the outcome of the test, and add any sub-pairs of the
30 /// removed pairs.
31 ///
32 /// For example:
33 /// ```
34 /// # let (x, y, z) = (true, true, true);
35 /// match (x, y, z) {
36 /// (true , _ , true ) => true, // (0)
37 /// (false, false, _ ) => false, // (1)
38 /// (_ , true , _ ) => true, // (2)
39 /// (true , _ , false) => false, // (3)
40 /// }
41 /// # ;
42 /// ```
43 ///
44 /// Assume we are testing on `x`. Conceptually, there are 2 overlapping candidate sets:
45 /// - If the outcome is that `x` is true, candidates {0, 2, 3} are possible
46 /// - If the outcome is that `x` is false, candidates {1, 2} are possible
47 ///
48 /// Following our algorithm:
49 /// - Candidate 0 is bucketed into outcome `x == true`
50 /// - Candidate 1 is bucketed into outcome `x == false`
51 /// - Candidate 2 remains unbucketed, because testing `x` has no effect on it
52 /// - Candidate 3 remains unbucketed, because a previous candidate (2) was unbucketed
53 /// - This helps preserve the illusion that candidates are tested "in order"
54 ///
55 /// The bucketed candidates are mutated to remove entailed match pairs:
56 /// - candidate 0 becomes `[z @ true]` since we know that `x` was `true`;
57 /// - candidate 1 becomes `[y @ false]` since we know that `x` was `false`.
58 pub(super) fn partition_candidates_into_buckets<'b, 'c>(
59 &mut self,
60 match_place: Place<'tcx>,
61 test: &Test<'tcx>,
62 mut candidates: &'b mut [&'c mut Candidate<'tcx>],
63 ) -> PartitionedCandidates<'tcx, 'b, 'c> {
64 // For each of the possible outcomes, collect a vector of candidates that apply if the test
65 // has that particular outcome.
66 let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'_>>> = Default::default();
67
68 let total_candidate_count = candidates.len();
69
70 // Partition the candidates into the appropriate vector in `target_candidates`.
71 // Note that at some point we may encounter a candidate where the test is not relevant;
72 // at that point, we stop partitioning.
73 while let Some(candidate) = candidates.first_mut() {
74 let Some(branch) =
75 self.choose_bucket_for_candidate(match_place, test, candidate, &target_candidates)
76 else {
77 break;
78 };
79 let (candidate, rest) = candidates.split_first_mut().unwrap();
80 target_candidates.entry(branch).or_insert_with(Vec::new).push(candidate);
81 candidates = rest;
82 }
83
84 // At least the first candidate ought to be tested
85 assert!(
86 total_candidate_count > candidates.len(),
87 "{total_candidate_count}, {candidates:#?}"
88 );
89 debug!("tested_candidates: {}", total_candidate_count - candidates.len());
90 debug!("untested_candidates: {}", candidates.len());
91
92 PartitionedCandidates { target_candidates, remaining_candidates: candidates }
93 }
94
95 /// Given that we are performing `test` against `test_place`, this job
96 /// sorts out what the status of `candidate` will be after the test. See
97 /// `test_candidates` for the usage of this function. The candidate may
98 /// be modified to update its `match_pairs`.
99 ///
100 /// So, for example, if this candidate is `x @ Some(P0)` and the `Test` is
101 /// a variant test, then we would modify the candidate to be `(x as
102 /// Option).0 @ P0` and return the index corresponding to the variant
103 /// `Some`.
104 ///
105 /// However, in some cases, the test may just not be relevant to candidate.
106 /// For example, suppose we are testing whether `foo.x == 22`, but in one
107 /// match arm we have `Foo { x: _, ... }`... in that case, the test for
108 /// the value of `x` has no particular relevance to this candidate. In
109 /// such cases, this function just returns None without doing anything.
110 /// This is used by the overall `match_candidates` algorithm to structure
111 /// the match as a whole. See `match_candidates` for more details.
112 ///
113 /// FIXME(#29623). In some cases, we have some tricky choices to make. for
114 /// example, if we are testing that `x == 22`, but the candidate is `x @
115 /// 13..55`, what should we do? In the event that the test is true, we know
116 /// that the candidate applies, but in the event of false, we don't know
117 /// that it *doesn't* apply. For now, we return false, indicate that the
118 /// test does not apply to this candidate, but it might be we can get
119 /// tighter match code if we do something a bit different.
120 fn choose_bucket_for_candidate(
121 &mut self,
122 test_place: Place<'tcx>,
123 test: &Test<'tcx>,
124 candidate: &mut Candidate<'tcx>,
125 // Other candidates that have already been partitioned into a bucket for this test, if any
126 prior_candidates: &FxIndexMap<TestBranch<'tcx>, Vec<&mut Candidate<'tcx>>>,
127 ) -> Option<TestBranch<'tcx>> {
128 // Find the match_pair for this place (if any). At present,
129 // afaik, there can be at most one. (In the future, if we
130 // adopted a more general `@` operator, there might be more
131 // than one, but it'd be very unusual to have two sides that
132 // both require tests; you'd expect one side to be simplified
133 // away.)
134 let (match_pair_index, match_pair) = candidate
135 .match_pairs
136 .iter()
137 .enumerate()
138 .find(|&(_, mp)| mp.place == Some(test_place))?;
139
140 // If true, the match pair is completely entailed by its corresponding test
141 // branch, so it can be removed. If false, the match pair is _compatible_
142 // with its test branch, but still needs a more specific test.
143 let fully_matched;
144 let ret = match (&test.kind, &match_pair.testable_case) {
145 // If we are performing a variant switch, then this
146 // informs variant patterns, but nothing else.
147 (
148 &TestKind::Switch { adt_def: tested_adt_def },
149 &TestableCase::Variant { adt_def, variant_index },
150 ) => {
151 assert_eq!(adt_def, tested_adt_def);
152 fully_matched = true;
153 Some(TestBranch::Variant(variant_index))
154 }
155
156 // If we are performing a switch over integers, then this informs integer
157 // equality, but nothing else.
158 //
159 // FIXME(#29623) we could use PatKind::Range to rule
160 // things out here, in some cases.
161 (
162 TestKind::SwitchInt,
163 &TestableCase::Constant { value, kind: PatConstKind::IntOrChar },
164 ) => {
165 // An important invariant of candidate bucketing is that a candidate
166 // must not match in multiple branches. For `SwitchInt` tests, adding
167 // a new value might invalidate that property for range patterns that
168 // have already been partitioned into the failure arm, so we must take care
169 // not to add such values here.
170 let is_covering_range = |testable_case: &TestableCase<'tcx>| {
171 testable_case.as_range().is_some_and(|range| {
172 matches!(range.contains(value, self.tcx), None | Some(true))
173 })
174 };
175 let is_conflicting_candidate = |candidate: &&mut Candidate<'tcx>| {
176 candidate.match_pairs.iter().any(|mp| {
177 mp.place == Some(test_place) && is_covering_range(&mp.testable_case)
178 })
179 };
180 if prior_candidates
181 .get(&TestBranch::Failure)
182 .is_some_and(|candidates| candidates.iter().any(is_conflicting_candidate))
183 {
184 fully_matched = false;
185 None
186 } else {
187 fully_matched = true;
188 Some(TestBranch::Constant(value))
189 }
190 }
191 (TestKind::SwitchInt, TestableCase::Range(range)) => {
192 // When performing a `SwitchInt` test, a range pattern can be
193 // sorted into the failure arm if it doesn't contain _any_ of
194 // the values being tested. (This restricts what values can be
195 // added to the test by subsequent candidates.)
196 fully_matched = false;
197 let not_contained = prior_candidates
198 .keys()
199 .filter_map(|br| br.as_constant())
200 .all(|val| matches!(range.contains(val, self.tcx), Some(false)));
201
202 not_contained.then(|| {
203 // No switch values are contained in the pattern range,
204 // so the pattern can be matched only if this test fails.
205 TestBranch::Failure
206 })
207 }
208
209 (TestKind::If, TestableCase::Constant { value, kind: PatConstKind::Bool }) => {
210 fully_matched = true;
211 let value = value.try_to_bool().unwrap_or_else(|| {
212 span_bug!(test.span, "expected boolean value but got {value:?}")
213 });
214 Some(if value { TestBranch::Success } else { TestBranch::Failure })
215 }
216
217 // Determine how the proposed slice-length test interacts with the
218 // slice pattern we're currently looking at.
219 //
220 // Keep in mind the invariant that a case is not allowed to succeed
221 // on multiple arms of the same test. For example, even though the
222 // test `len == 4` logically implies `len >= 4` on its success arm,
223 // the case `len >= 4` could also succeed on the test's failure arm,
224 // so it can't be included in the success bucket or failure bucket.
225 (
226 &TestKind::SliceLen { len: test_len, op: SliceLenOp::Equal },
227 &TestableCase::Slice { len: pat_len, op: pat_op },
228 ) => {
229 match (test_len.cmp(&pat_len), pat_op) {
230 (Ordering::Equal, SliceLenOp::Equal) => {
231 // E.g. test is `len == 4` and pattern is `len == 4`.
232 // Pattern is fully matched on the success arm.
233 fully_matched = true;
234 Some(TestBranch::Success)
235 }
236 (Ordering::Less, _) => {
237 // E.g. test is `len == 4` and pattern is `len == 5` or `len >= 5`.
238 // Pattern can only succeed on the failure arm, but isn't fully matched.
239 fully_matched = false;
240 Some(TestBranch::Failure)
241 }
242 (Ordering::Equal | Ordering::Greater, SliceLenOp::GreaterOrEqual) => {
243 // E.g. test is `len == 4` and pattern is `len >= 4` or `len >= 3`.
244 // Pattern could succeed on both arms, so it can't be bucketed.
245 fully_matched = false;
246 None
247 }
248 (Ordering::Greater, SliceLenOp::Equal) => {
249 // E.g. test is `len == 4` and pattern is `len == 3`.
250 // Pattern can only succeed on the failure arm, but isn't fully matched.
251 fully_matched = false;
252 Some(TestBranch::Failure)
253 }
254 }
255 }
256 (
257 &TestKind::SliceLen { len: test_len, op: SliceLenOp::GreaterOrEqual },
258 &TestableCase::Slice { len: pat_len, op: pat_op },
259 ) => {
260 match (test_len.cmp(&pat_len), pat_op) {
261 (Ordering::Equal, SliceLenOp::GreaterOrEqual) => {
262 // E.g. test is `len >= 4` and pattern is `len >= 4`.
263 // Pattern is fully matched on the success arm.
264 fully_matched = true;
265 Some(TestBranch::Success)
266 }
267 (Ordering::Less, _) | (Ordering::Equal, SliceLenOp::Equal) => {
268 // E.g. test is `len >= 4` and pattern is `len == 5` or `len >= 5` or `len == 4`.
269 // Pattern can only succeed on the success arm, but isn't fully matched.
270 fully_matched = false;
271 Some(TestBranch::Success)
272 }
273 (Ordering::Greater, SliceLenOp::Equal) => {
274 // E.g. test is `len >= 4` and pattern is `len == 3`.
275 // Pattern can only succeed on the failure arm, but isn't fully matched.
276 fully_matched = false;
277 Some(TestBranch::Failure)
278 }
279 (Ordering::Greater, SliceLenOp::GreaterOrEqual) => {
280 // E.g. test is `len >= 4` and pattern is `len >= 3`.
281 // Pattern could succeed on both arms, so it can't be bucketed.
282 fully_matched = false;
283 None
284 }
285 }
286 }
287
288 (TestKind::Range(test), TestableCase::Range(pat)) => {
289 if test == pat {
290 fully_matched = true;
291 Some(TestBranch::Success)
292 } else {
293 fully_matched = false;
294 // If the testing range does not overlap with pattern range,
295 // the pattern can be matched only if this test fails.
296 if !test.overlaps(pat, self.tcx)? { Some(TestBranch::Failure) } else { None }
297 }
298 }
299 (
300 TestKind::Range(range),
301 &TestableCase::Constant {
302 value,
303 kind: PatConstKind::Bool | PatConstKind::IntOrChar | PatConstKind::Float,
304 },
305 ) => {
306 fully_matched = false;
307 if !range.contains(value, self.tcx)? {
308 // `value` is not contained in the testing range,
309 // so `value` can be matched only if this test fails.
310 Some(TestBranch::Failure)
311 } else {
312 None
313 }
314 }
315
316 (
317 TestKind::StringEq { value: test_val, .. },
318 TestableCase::Constant { value: case_val, kind: PatConstKind::String },
319 )
320 | (
321 TestKind::ScalarEq { value: test_val, .. },
322 TestableCase::Constant {
323 value: case_val,
324 kind: PatConstKind::Float | PatConstKind::Other,
325 },
326 ) => {
327 if test_val == case_val {
328 fully_matched = true;
329 Some(TestBranch::Success)
330 } else {
331 fully_matched = false;
332 Some(TestBranch::Failure)
333 }
334 }
335
336 (TestKind::Deref { temp: test_temp, .. }, TestableCase::Deref { temp, .. })
337 if test_temp == temp =>
338 {
339 fully_matched = true;
340 Some(TestBranch::Success)
341 }
342
343 (TestKind::Never, _) => {
344 fully_matched = true;
345 Some(TestBranch::Success)
346 }
347
348 (
349 TestKind::Switch { .. }
350 | TestKind::SwitchInt { .. }
351 | TestKind::If
352 | TestKind::SliceLen { .. }
353 | TestKind::Range { .. }
354 | TestKind::StringEq { .. }
355 | TestKind::ScalarEq { .. }
356 | TestKind::Deref { .. },
357 _,
358 ) => {
359 fully_matched = false;
360 None
361 }
362 };
363
364 if fully_matched {
365 // Replace the match pair by its sub-pairs.
366 let match_pair = candidate.match_pairs.remove(match_pair_index);
367 candidate.match_pairs.extend(match_pair.subpairs);
368 // Move or-patterns to the end.
369 candidate.sort_match_pairs();
370 }
371
372 ret
373 }
374}