Skip to content

Commit c2f1072

Browse files
committed
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.
1 parent fc555cd commit c2f1072

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
@@ -1031,6 +1031,12 @@ impl<'tcx> PatternExtraData<'tcx> {
10311031
}
10321032

10331033
/// A pattern in a form suitable for generating code.
1034+
///
1035+
/// Here, "flat" indicates that the pattern's match pairs have been recursively
1036+
/// simplified by [`Builder::simplify_match_pairs`]. They are not necessarily
1037+
/// flat in an absolute sense.
1038+
///
1039+
/// Will typically be incorporated into a [`Candidate`].
10341040
#[derive(Debug, Clone)]
10351041
struct FlatPat<'pat, 'tcx> {
10361042
/// To match the pattern, all of these must be satisfied...
@@ -1042,23 +1048,25 @@ struct FlatPat<'pat, 'tcx> {
10421048
}
10431049

10441050
impl<'tcx, 'pat> FlatPat<'pat, 'tcx> {
1051+
/// Creates a `FlatPat` containing a simplified [`MatchPair`] list/forest
1052+
/// for the given pattern.
10451053
fn new(
10461054
place: PlaceBuilder<'tcx>,
10471055
pattern: &'pat Pat<'tcx>,
10481056
cx: &mut Builder<'_, 'tcx>,
10491057
) -> Self {
1050-
let is_never = pattern.is_never_pattern();
1051-
let mut flat_pat = FlatPat {
1052-
match_pairs: vec![MatchPair::new(place, pattern, cx)],
1053-
extra_data: PatternExtraData {
1054-
span: pattern.span,
1055-
bindings: Vec::new(),
1056-
ascriptions: Vec::new(),
1057-
is_never,
1058-
},
1058+
// First, recursively build a tree of match pairs for the given pattern.
1059+
let mut match_pairs = vec![MatchPair::new(place, pattern, cx)];
1060+
let mut extra_data = PatternExtraData {
1061+
span: pattern.span,
1062+
bindings: Vec::new(),
1063+
ascriptions: Vec::new(),
1064+
is_never: pattern.is_never_pattern(),
10591065
};
1060-
cx.simplify_match_pairs(&mut flat_pat.match_pairs, &mut flat_pat.extra_data);
1061-
flat_pat
1066+
// Partly-flatten and sort the match pairs, while recording extra data.
1067+
cx.simplify_match_pairs(&mut match_pairs, &mut extra_data);
1068+
1069+
Self { match_pairs, extra_data }
10621070
}
10631071
}
10641072

@@ -1104,9 +1112,12 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11041112
has_guard: bool,
11051113
cx: &mut Builder<'_, 'tcx>,
11061114
) -> Self {
1115+
// Use `FlatPat` to build simplified match pairs, then immediately
1116+
// incorporate them into a new candidate.
11071117
Self::from_flat_pat(FlatPat::new(place, pattern, cx), has_guard)
11081118
}
11091119

1120+
/// Incorporates an already-simplified [`FlatPat`] into a new candidate.
11101121
fn from_flat_pat(flat_pat: FlatPat<'pat, 'tcx>, has_guard: bool) -> Self {
11111122
Candidate {
11121123
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)