Skip to content

Commit

Permalink
Remove the separate simplify step for match-pair trees
Browse files Browse the repository at this point in the history
What remained of this simplification process has been integrated into
construction of the match-pair trees.
  • Loading branch information
Zalathar committed Mar 5, 2025
1 parent 54cc229 commit 621525d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 67 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/builder/matches/match_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ impl<'tcx> MatchPairTree<'tcx> {

if let Some(test_case) = test_case {
// This pattern is refutable, so push a new match-pair node.
sort_match_pairs(&mut subpairs);
match_pairs.push(MatchPairTree {
place,
test_case,
Expand All @@ -333,3 +334,9 @@ impl<'tcx> MatchPairTree<'tcx> {
}
}
}

/// Sorts or-patterns to the end of the list, as required by [`FlatPat`] and
/// its enclosed [`MatchPairTree`] nodes.
pub(super) fn sort_match_pairs(match_pairs: &mut [MatchPairTree<'_>]) {
match_pairs.sort_by_key(|pair| matches!(pair.test_case, TestCase::Or { .. }));
}
15 changes: 8 additions & 7 deletions compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::builder::{

// helper functions, broken out by category:
mod match_pair;
mod simplify;
mod test;
mod util;

Expand Down Expand Up @@ -989,16 +988,16 @@ impl<'tcx> PatternExtraData<'tcx> {
/// A pattern in a form suitable for lowering the match tree, with all irrefutable
/// patterns simplified away, and or-patterns sorted to the end.
///
/// Here, "flat" indicates that the pattern's match pairs have been recursively
/// simplified by [`Builder::simplify_match_pairs`]. They are not necessarily
/// flat in an absolute sense.
/// Here, "flat" indicates that irrefutable nodes in the pattern tree have been
/// recursively replaced with their refutable subpatterns. They are not
/// necessarily flat in an absolute sense.
///
/// Will typically be incorporated into a [`Candidate`].
#[derive(Debug, Clone)]
struct FlatPat<'tcx> {
/// To match the pattern, all of these must be satisfied...
// Invariant: all the match pairs are recursively simplified.
// Invariant: or-patterns must be sorted to the end.
/// ---
/// Invariant: Or-patterns must be sorted to the end.
match_pairs: Vec<MatchPairTree<'tcx>>,

extra_data: PatternExtraData<'tcx>,
Expand All @@ -1017,7 +1016,7 @@ impl<'tcx> FlatPat<'tcx> {
is_never: pattern.is_never_pattern(),
};
MatchPairTree::for_pattern(place, pattern, cx, &mut match_pairs, &mut extra_data);
cx.simplify_match_pairs(&mut match_pairs, &mut extra_data);
match_pair::sort_match_pairs(&mut match_pairs);

Self { match_pairs, extra_data }
}
Expand Down Expand Up @@ -1271,6 +1270,8 @@ pub(crate) struct MatchPairTree<'tcx> {
/// parent has succeeded. For example, the pattern `Some(3)` might have an
/// outer match pair that tests for the variant `Some`, and then a subpair
/// that tests its field for the value `3`.
///
/// Invariant: Or-patterns must be sorted to the end.
subpairs: Vec<Self>,

/// Type field of the pattern this node was created from.
Expand Down
60 changes: 0 additions & 60 deletions compiler/rustc_mir_build/src/builder/matches/simplify.rs

This file was deleted.

0 comments on commit 621525d

Please sign in to comment.