Skip to content

Commit 4d5ba0d

Browse files
authored
Rollup merge of rust-lang#127858 - Zalathar:pair-tree, r=Nadrieril
match lowering: Rename `MatchPair` to `MatchPairTree` In rust-lang#120904, `MatchPair` became able to store other match pairs as children, forming a tree. That has made the old name confusing, so this patch renames the type to `MatchPairTree`. This PR also includes a patch renaming the `test` method to `pick_test_for_match_pair`, since it would conflict with the main change. r? `@Nadrieril`
2 parents 50a90e3 + 411fcb6 commit 4d5ba0d

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

compiler/rustc_mir_build/src/build/matches/match_pair.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ use rustc_middle::thir::{self, *};
33
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
44

55
use crate::build::expr::as_place::{PlaceBase, PlaceBuilder};
6-
use crate::build::matches::{FlatPat, MatchPair, TestCase};
6+
use crate::build::matches::{FlatPat, MatchPairTree, TestCase};
77
use crate::build::Builder;
88

99
impl<'a, 'tcx> Builder<'a, 'tcx> {
10-
/// Builds and returns [`MatchPair`] trees, one for each pattern in
10+
/// Builds and returns [`MatchPairTree`] subtrees, one for each pattern in
1111
/// `subpatterns`, representing the fields of a [`PatKind::Variant`] or
1212
/// [`PatKind::Leaf`].
1313
///
14-
/// Used internally by [`MatchPair::new`].
14+
/// Used internally by [`MatchPairTree::for_pattern`].
1515
fn field_match_pairs<'pat>(
1616
&mut self,
1717
place: PlaceBuilder<'tcx>,
1818
subpatterns: &'pat [FieldPat<'tcx>],
19-
) -> Vec<MatchPair<'pat, 'tcx>> {
19+
) -> Vec<MatchPairTree<'pat, 'tcx>> {
2020
subpatterns
2121
.iter()
2222
.map(|fieldpat| {
2323
let place =
2424
place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
25-
MatchPair::new(place, &fieldpat.pattern, self)
25+
MatchPairTree::for_pattern(place, &fieldpat.pattern, self)
2626
})
2727
.collect()
2828
}
2929

30-
/// Builds [`MatchPair`] trees for the prefix/middle/suffix parts of an
30+
/// Builds [`MatchPairTree`] subtrees for the prefix/middle/suffix parts of an
3131
/// array pattern or slice pattern, and adds those trees to `match_pairs`.
3232
///
33-
/// Used internally by [`MatchPair::new`].
33+
/// Used internally by [`MatchPairTree::for_pattern`].
3434
fn prefix_slice_suffix<'pat>(
3535
&mut self,
36-
match_pairs: &mut Vec<MatchPair<'pat, 'tcx>>,
36+
match_pairs: &mut Vec<MatchPairTree<'pat, 'tcx>>,
3737
place: &PlaceBuilder<'tcx>,
3838
prefix: &'pat [Box<Pat<'tcx>>],
3939
opt_slice: &'pat Option<Box<Pat<'tcx>>>,
@@ -52,7 +52,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5252
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
5353
let elem =
5454
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
55-
MatchPair::new(place.clone_project(elem), subpattern, self)
55+
MatchPairTree::for_pattern(place.clone_project(elem), subpattern, self)
5656
}));
5757

5858
if let Some(subslice_pat) = opt_slice {
@@ -62,7 +62,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6262
to: if exact_size { min_length - suffix_len } else { suffix_len },
6363
from_end: !exact_size,
6464
});
65-
match_pairs.push(MatchPair::new(subslice, subslice_pat, self));
65+
match_pairs.push(MatchPairTree::for_pattern(subslice, subslice_pat, self));
6666
}
6767

6868
match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
@@ -73,19 +73,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7373
from_end: !exact_size,
7474
};
7575
let place = place.clone_project(elem);
76-
MatchPair::new(place, subpattern, self)
76+
MatchPairTree::for_pattern(place, subpattern, self)
7777
}));
7878
}
7979
}
8080

81-
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
82-
/// Recursively builds a `MatchPair` tree for the given pattern and its
81+
impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
82+
/// Recursively builds a match pair tree for the given pattern and its
8383
/// subpatterns.
84-
pub(in crate::build) fn new(
84+
pub(in crate::build) fn for_pattern(
8585
mut place_builder: PlaceBuilder<'tcx>,
8686
pattern: &'pat Pat<'tcx>,
8787
cx: &mut Builder<'_, 'tcx>,
88-
) -> MatchPair<'pat, 'tcx> {
88+
) -> MatchPairTree<'pat, 'tcx> {
8989
// Force the place type to the pattern's type.
9090
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
9191
if let Some(resolved) = place_builder.resolve_upvar(cx) {
@@ -138,7 +138,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
138138
variance,
139139
});
140140

141-
subpairs.push(MatchPair::new(place_builder, subpattern, cx));
141+
subpairs.push(MatchPairTree::for_pattern(place_builder, subpattern, cx));
142142
TestCase::Irrefutable { ascription, binding: None }
143143
}
144144

@@ -152,7 +152,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
152152

153153
if let Some(subpattern) = subpattern.as_ref() {
154154
// this is the `x @ P` case; have to keep matching against `P` now
155-
subpairs.push(MatchPair::new(place_builder, subpattern, cx));
155+
subpairs.push(MatchPairTree::for_pattern(place_builder, subpattern, cx));
156156
}
157157
TestCase::Irrefutable { ascription: None, binding }
158158
}
@@ -182,7 +182,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
182182
super::Ascription { annotation, source, variance: ty::Contravariant }
183183
});
184184

185-
subpairs.push(MatchPair::new(place_builder, pattern, cx));
185+
subpairs.push(MatchPairTree::for_pattern(place_builder, pattern, cx));
186186
TestCase::Irrefutable { ascription, binding: None }
187187
}
188188

@@ -231,7 +231,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
231231
}
232232

233233
PatKind::Deref { ref subpattern } => {
234-
subpairs.push(MatchPair::new(place_builder.deref(), subpattern, cx));
234+
subpairs.push(MatchPairTree::for_pattern(place_builder.deref(), subpattern, cx));
235235
default_irrefutable()
236236
}
237237

@@ -242,13 +242,17 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
242242
Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, subpattern.ty, mutability),
243243
pattern.span,
244244
);
245-
subpairs.push(MatchPair::new(PlaceBuilder::from(temp).deref(), subpattern, cx));
245+
subpairs.push(MatchPairTree::for_pattern(
246+
PlaceBuilder::from(temp).deref(),
247+
subpattern,
248+
cx,
249+
));
246250
TestCase::Deref { temp, mutability }
247251
}
248252

249253
PatKind::Never => TestCase::Never,
250254
};
251255

252-
MatchPair { place, test_case, subpairs, pattern }
256+
MatchPairTree { place, test_case, subpairs, pattern }
253257
}
254258
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1032,23 +1032,23 @@ impl<'tcx> PatternExtraData<'tcx> {
10321032
#[derive(Debug, Clone)]
10331033
struct FlatPat<'pat, 'tcx> {
10341034
/// To match the pattern, all of these must be satisfied...
1035-
// Invariant: all the `MatchPair`s are recursively simplified.
1035+
// Invariant: all the match pairs are recursively simplified.
10361036
// Invariant: or-patterns must be sorted to the end.
1037-
match_pairs: Vec<MatchPair<'pat, 'tcx>>,
1037+
match_pairs: Vec<MatchPairTree<'pat, 'tcx>>,
10381038

10391039
extra_data: PatternExtraData<'tcx>,
10401040
}
10411041

10421042
impl<'tcx, 'pat> FlatPat<'pat, 'tcx> {
1043-
/// Creates a `FlatPat` containing a simplified [`MatchPair`] list/forest
1043+
/// Creates a `FlatPat` containing a simplified [`MatchPairTree`] list/forest
10441044
/// for the given pattern.
10451045
fn new(
10461046
place: PlaceBuilder<'tcx>,
10471047
pattern: &'pat Pat<'tcx>,
10481048
cx: &mut Builder<'_, 'tcx>,
10491049
) -> Self {
10501050
// First, recursively build a tree of match pairs for the given pattern.
1051-
let mut match_pairs = vec![MatchPair::new(place, pattern, cx)];
1051+
let mut match_pairs = vec![MatchPairTree::for_pattern(place, pattern, cx)];
10521052
let mut extra_data = PatternExtraData {
10531053
span: pattern.span,
10541054
bindings: Vec::new(),
@@ -1065,9 +1065,9 @@ impl<'tcx, 'pat> FlatPat<'pat, 'tcx> {
10651065
#[derive(Debug)]
10661066
struct Candidate<'pat, 'tcx> {
10671067
/// For the candidate to match, all of these must be satisfied...
1068-
// Invariant: all the `MatchPair`s are recursively simplified.
1068+
// Invariant: all the match pairs are recursively simplified.
10691069
// Invariant: or-patterns must be sorted at the end.
1070-
match_pairs: Vec<MatchPair<'pat, 'tcx>>,
1070+
match_pairs: Vec<MatchPairTree<'pat, 'tcx>>,
10711071

10721072
/// ...and if this is non-empty, one of these subcandidates also has to match...
10731073
// Invariant: at the end of the algorithm, this must never contain a `is_never` candidate
@@ -1126,7 +1126,7 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11261126

11271127
/// Returns whether the first match pair of this candidate is an or-pattern.
11281128
fn starts_with_or_pattern(&self) -> bool {
1129-
matches!(&*self.match_pairs, [MatchPair { test_case: TestCase::Or { .. }, .. }, ..])
1129+
matches!(&*self.match_pairs, [MatchPairTree { test_case: TestCase::Or { .. }, .. }, ..])
11301130
}
11311131

11321132
/// Visit the leaf candidates (those with no subcandidates) contained in
@@ -1206,7 +1206,7 @@ impl<'pat, 'tcx> TestCase<'pat, 'tcx> {
12061206
/// Each node also has a list of subpairs (possibly empty) that must also match,
12071207
/// and a reference to the THIR pattern it represents.
12081208
#[derive(Debug, Clone)]
1209-
pub(crate) struct MatchPair<'pat, 'tcx> {
1209+
pub(crate) struct MatchPairTree<'pat, 'tcx> {
12101210
/// This place...
12111211
///
12121212
/// ---
@@ -1629,7 +1629,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16291629
fn create_or_subcandidates<'pat>(
16301630
&mut self,
16311631
candidate: &mut Candidate<'pat, 'tcx>,
1632-
match_pair: MatchPair<'pat, 'tcx>,
1632+
match_pair: MatchPairTree<'pat, 'tcx>,
16331633
) {
16341634
let TestCase::Or { pats } = match_pair.test_case else { bug!() };
16351635
debug!("expanding or-pattern: candidate={:#?}\npats={:#?}", candidate, pats);
@@ -1813,8 +1813,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18131813
/// [`Range`]: TestKind::Range
18141814
fn pick_test(&mut self, candidates: &[&mut Candidate<'_, 'tcx>]) -> (Place<'tcx>, Test<'tcx>) {
18151815
// Extract the match-pair from the highest priority candidate
1816-
let match_pair = &candidates.first().unwrap().match_pairs[0];
1817-
let test = self.test(match_pair);
1816+
let match_pair = &candidates[0].match_pairs[0];
1817+
let test = self.pick_test_for_match_pair(match_pair);
18181818
// Unwrap is ok after simplification.
18191819
let match_place = match_pair.place.unwrap();
18201820
debug!(?test, ?match_pair);

compiler/rustc_mir_build/src/build/matches/simplify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! sort of test: for example, testing which variant an enum is, or
1313
//! testing a value against a constant.
1414
15-
use crate::build::matches::{MatchPair, PatternExtraData, TestCase};
15+
use crate::build::matches::{MatchPairTree, PatternExtraData, TestCase};
1616
use crate::build::Builder;
1717
use tracing::{debug, instrument};
1818

@@ -24,7 +24,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2424
#[instrument(skip(self), level = "debug")]
2525
pub(super) fn simplify_match_pairs<'pat>(
2626
&mut self,
27-
match_pairs: &mut Vec<MatchPair<'pat, 'tcx>>,
27+
match_pairs: &mut Vec<MatchPairTree<'pat, 'tcx>>,
2828
extra_data: &mut PatternExtraData<'tcx>,
2929
) {
3030
// In order to please the borrow checker, in a pattern like `x @ pat` we must lower the

compiler/rustc_mir_build/src/build/matches/test.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// identify what tests are needed, perform the tests, and then filter
66
// the candidates based on the result.
77

8-
use crate::build::matches::{Candidate, MatchPair, Test, TestBranch, TestCase, TestKind};
8+
use crate::build::matches::{Candidate, MatchPairTree, Test, TestBranch, TestCase, TestKind};
99
use crate::build::Builder;
1010
use rustc_data_structures::fx::FxIndexMap;
1111
use rustc_hir::{LangItem, RangeEnd};
@@ -26,7 +26,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2626
/// Identifies what test is needed to decide if `match_pair` is applicable.
2727
///
2828
/// It is a bug to call this with a not-fully-simplified pattern.
29-
pub(super) fn test<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> Test<'tcx> {
29+
pub(super) fn pick_test_for_match_pair<'pat>(
30+
&mut self,
31+
match_pair: &MatchPairTree<'pat, 'tcx>,
32+
) -> Test<'tcx> {
3033
let kind = match match_pair.test_case {
3134
TestCase::Variant { adt_def, variant_index: _ } => TestKind::Switch { adt_def },
3235

compiler/rustc_mir_build/src/build/matches/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::marker::PhantomData;
22

33
use crate::build::expr::as_place::PlaceBase;
4-
use crate::build::matches::{Binding, Candidate, FlatPat, MatchPair, TestCase};
4+
use crate::build::matches::{Binding, Candidate, FlatPat, MatchPairTree, TestCase};
55
use crate::build::Builder;
66
use rustc_data_structures::fx::FxIndexMap;
77
use rustc_middle::mir::*;
@@ -152,7 +152,7 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
152152
}
153153
}
154154

155-
fn visit_match_pair(&mut self, match_pair: &MatchPair<'_, 'tcx>) {
155+
fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'_, 'tcx>) {
156156
if let TestCase::Or { pats, .. } = &match_pair.test_case {
157157
for flat_pat in pats.iter() {
158158
self.visit_flat_pat(flat_pat)
@@ -260,7 +260,7 @@ where
260260
}
261261
}
262262

263-
fn visit_match_pair(&mut self, match_pair: &MatchPair<'_, 'tcx>) {
263+
fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'_, 'tcx>) {
264264
if let TestCase::Or { pats, .. } = &match_pair.test_case {
265265
// All the or-alternatives should bind the same locals, so we only visit the first one.
266266
self.visit_flat_pat(&pats[0])

0 commit comments

Comments
 (0)