Skip to content

Commit 7e1489c

Browse files
authored
Rollup merge of rust-lang#126932 - Zalathar:flat-pat, r=Nadrieril
Tweak `FlatPat::new` to avoid a temporarily-invalid state It was somewhat confusing that the old constructor would create a `FlatPat` in a (possibly) non-simplified state, and then simplify its contents in-place. So instead we now create its fields as local variables, perform simplification, and then create the struct afterwards. This doesn't affect correctness, but is less confusing. --- I've also included some semi-related comments that I made while trying to navigate this code.
2 parents 3795c56 + c2f1072 commit 7e1489c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

+22-11
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,12 @@ impl<'tcx> PatternExtraData<'tcx> {
10341034
}
10351035

10361036
/// A pattern in a form suitable for generating code.
1037+
///
1038+
/// Here, "flat" indicates that the pattern's match pairs have been recursively
1039+
/// simplified by [`Builder::simplify_match_pairs`]. They are not necessarily
1040+
/// flat in an absolute sense.
1041+
///
1042+
/// Will typically be incorporated into a [`Candidate`].
10371043
#[derive(Debug, Clone)]
10381044
struct FlatPat<'pat, 'tcx> {
10391045
/// To match the pattern, all of these must be satisfied...
@@ -1045,23 +1051,25 @@ struct FlatPat<'pat, 'tcx> {
10451051
}
10461052

10471053
impl<'tcx, 'pat> FlatPat<'pat, 'tcx> {
1054+
/// Creates a `FlatPat` containing a simplified [`MatchPair`] list/forest
1055+
/// for the given pattern.
10481056
fn new(
10491057
place: PlaceBuilder<'tcx>,
10501058
pattern: &'pat Pat<'tcx>,
10511059
cx: &mut Builder<'_, 'tcx>,
10521060
) -> Self {
1053-
let is_never = pattern.is_never_pattern();
1054-
let mut flat_pat = FlatPat {
1055-
match_pairs: vec![MatchPair::new(place, pattern, cx)],
1056-
extra_data: PatternExtraData {
1057-
span: pattern.span,
1058-
bindings: Vec::new(),
1059-
ascriptions: Vec::new(),
1060-
is_never,
1061-
},
1061+
// First, recursively build a tree of match pairs for the given pattern.
1062+
let mut match_pairs = vec![MatchPair::new(place, pattern, cx)];
1063+
let mut extra_data = PatternExtraData {
1064+
span: pattern.span,
1065+
bindings: Vec::new(),
1066+
ascriptions: Vec::new(),
1067+
is_never: pattern.is_never_pattern(),
10621068
};
1063-
cx.simplify_match_pairs(&mut flat_pat.match_pairs, &mut flat_pat.extra_data);
1064-
flat_pat
1069+
// Partly-flatten and sort the match pairs, while recording extra data.
1070+
cx.simplify_match_pairs(&mut match_pairs, &mut extra_data);
1071+
1072+
Self { match_pairs, extra_data }
10651073
}
10661074
}
10671075

@@ -1107,9 +1115,12 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11071115
has_guard: bool,
11081116
cx: &mut Builder<'_, 'tcx>,
11091117
) -> Self {
1118+
// Use `FlatPat` to build simplified match pairs, then immediately
1119+
// incorporate them into a new candidate.
11101120
Self::from_flat_pat(FlatPat::new(place, pattern, cx), has_guard)
11111121
}
11121122

1123+
/// Incorporates an already-simplified [`FlatPat`] into a new candidate.
11131124
fn from_flat_pat(flat_pat: FlatPat<'pat, 'tcx>, has_guard: bool) -> Self {
11141125
Candidate {
11151126
match_pairs: flat_pat.match_pairs,

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

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9595
}
9696

9797
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
98+
/// Recursively builds a `MatchPair` tree for the given pattern and its
99+
/// subpatterns.
98100
pub(in crate::build) fn new(
99101
mut place_builder: PlaceBuilder<'tcx>,
100102
pattern: &'pat Pat<'tcx>,

0 commit comments

Comments
 (0)