Skip to content

Commit

Permalink
Auto merge of rust-lang#120904 - Nadrieril:match-lowering-intermediat…
Browse files Browse the repository at this point in the history
…e-repr, r=<try>

match lowering: eagerly simplify match pairs

This removes one important complication from match lowering. Before this, match pair simplification (which includes collecting bindings and type ascriptions) was intertwined with the whole match lowering algorithm.

I'm avoiding this by storing in each `MatchPair` the sub-`MatchPair`s that correspond to its subfields. This makes it possible to simplify everything (except or-patterns) in `Candidate::new()`.

This should open up further simplifications. It will also give us proper control over the order of bindings.

r? `@matthewjasper`
  • Loading branch information
bors committed Feb 11, 2024
2 parents 42752cb + 45ef29c commit d39daec
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 245 deletions.
51 changes: 41 additions & 10 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, Pos, Span};
use rustc_target::abi::VariantIdx;
use smallvec::{smallvec, SmallVec};

// helper functions, broken out by category:
mod simplify;
mod test;
Expand Down Expand Up @@ -951,12 +949,16 @@ struct Candidate<'pat, 'tcx> {
has_guard: bool,

/// All of these must be satisfied...
match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
// Invariant: all the `MatchPair`s are recursively simplified.
// Invariant: or-patterns must be sorted at the end.
match_pairs: Vec<MatchPair<'pat, 'tcx>>,

/// ...these bindings established...
// Invariant: not mutated outside `Candidate::new()`.
bindings: Vec<Binding<'tcx>>,

/// ...and these types asserted...
// Invariant: not mutated outside `Candidate::new()`.
ascriptions: Vec<Ascription<'tcx>>,

/// ...and if this is non-empty, one of these subcandidates also has to match...
Expand All @@ -976,19 +978,27 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
place: PlaceBuilder<'tcx>,
pattern: &'pat Pat<'tcx>,
has_guard: bool,
cx: &Builder<'_, 'tcx>,
cx: &mut Builder<'_, 'tcx>,
) -> Self {
Candidate {
let mut candidate = Candidate {
span: pattern.span,
has_guard,
match_pairs: smallvec![MatchPair::new(place, pattern, cx)],
match_pairs: vec![MatchPair::new(place, pattern, cx)],
bindings: Vec::new(),
ascriptions: Vec::new(),
subcandidates: Vec::new(),
otherwise_block: None,
pre_binding_block: None,
next_candidate_pre_binding_block: None,
}
};

cx.simplify_match_pairs(
&mut candidate.match_pairs,
&mut candidate.bindings,
&mut candidate.ascriptions,
);

candidate
}

/// Visit the leaf candidates (those with no subcandidates) contained in
Expand Down Expand Up @@ -1044,13 +1054,18 @@ struct Ascription<'tcx> {
variance: ty::Variance,
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub(crate) struct MatchPair<'pat, 'tcx> {
// this place...
// This place...
place: PlaceBuilder<'tcx>,

// ... must match this pattern.
// Invariant: after creation and simplification in `Candidate::new()`, all match pairs must be
// simplified, i.e. require a test.
pattern: &'pat Pat<'tcx>,

/// Precomputed sub-match pairs of `pattern`.
subpairs: Vec<Self>,
}

/// See [`Test`] for more.
Expand Down Expand Up @@ -1172,7 +1187,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// be a switch or pattern comparison.
let mut split_or_candidate = false;
for candidate in &mut *candidates {
split_or_candidate |= self.simplify_candidate(candidate);
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
&*candidate.match_pairs
{
// Split a candidate in which the only match-pair is an or-pattern into multiple
// candidates. This is so that
//
// match x {
// 0 | 1 => { ... },
// 2 | 3 => { ... },
// }
//
// only generates a single switch.
candidate.subcandidates =
self.create_or_subcandidates(place, pats, candidate.has_guard);
candidate.match_pairs.pop();
split_or_candidate = true;
}
}

ensure_sufficient_stack(|| {
Expand Down
Loading

0 comments on commit d39daec

Please sign in to comment.