Skip to content

Commit 7dbc49e

Browse files
committed
Visiting bindings is straightforward now
1 parent 4496a61 commit 7dbc49e

File tree

2 files changed

+14
-67
lines changed

2 files changed

+14
-67
lines changed

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_span::symbol::Symbol;
2121
use rustc_span::{BytePos, Pos, Span};
2222
use rustc_target::abi::VariantIdx;
2323
use tracing::{debug, instrument};
24-
use util::visit_bindings;
2524

2625
// helper functions, broken out by category:
2726
mod simplify;
@@ -691,7 +690,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
691690
initializer: PlaceBuilder<'tcx>,
692691
set_match_place: bool,
693692
) -> BlockAnd<()> {
694-
let mut candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
693+
let candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
694+
let built_tree = self.lower_match_tree(
695+
block,
696+
irrefutable_pat.span,
697+
&initializer,
698+
irrefutable_pat.span,
699+
vec![candidate],
700+
false,
701+
);
702+
let [branch] = built_tree.branches.try_into().unwrap();
695703

696704
// For matches and function arguments, the place that is being matched
697705
// can be set when creating the variables. But the place for
@@ -712,7 +720,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
712720
// };
713721
// ```
714722
if let Some(place) = initializer.try_to_place(self) {
715-
visit_bindings(&[&mut candidate], |binding: &Binding<'_>| {
723+
// Because or-alternatives bind the same variables, we only explore the first one.
724+
let first_sub_branch = branch.sub_branches.first().unwrap();
725+
for binding in &first_sub_branch.bindings {
716726
let local = self.var_local_id(binding.var_id, OutsideGuard);
717727
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
718728
opt_match_place: Some((ref mut match_place, _)),
@@ -723,20 +733,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
723733
} else {
724734
bug!("Let binding to non-user variable.")
725735
};
726-
});
736+
}
727737
}
728738
}
729739

730-
let built_tree = self.lower_match_tree(
731-
block,
732-
irrefutable_pat.span,
733-
&initializer,
734-
irrefutable_pat.span,
735-
vec![candidate],
736-
false,
737-
);
738-
let [branch] = built_tree.branches.try_into().unwrap();
739-
740740
self.bind_pattern(
741741
self.source_info(irrefutable_pat.span),
742742
branch,

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

-53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::marker::PhantomData;
2-
31
use crate::build::expr::as_place::{PlaceBase, PlaceBuilder};
42
use crate::build::matches::{Binding, Candidate, FlatPat, MatchPair, TestCase};
53
use crate::build::Builder;
@@ -458,57 +456,6 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
458456
}
459457
}
460458

461-
/// Visit all the bindings of these candidates. Because or-alternatives bind the same variables, we
462-
/// only explore the first one of each or-pattern.
463-
pub(super) fn visit_bindings<'tcx>(
464-
candidates: &[&mut Candidate<'_, 'tcx>],
465-
f: impl FnMut(&Binding<'tcx>),
466-
) {
467-
let mut visitor = BindingsVisitor { f, phantom: PhantomData };
468-
for candidate in candidates.iter() {
469-
visitor.visit_candidate(candidate);
470-
}
471-
}
472-
473-
pub(super) struct BindingsVisitor<'tcx, F> {
474-
f: F,
475-
phantom: PhantomData<&'tcx ()>,
476-
}
477-
478-
impl<'tcx, F> BindingsVisitor<'tcx, F>
479-
where
480-
F: FnMut(&Binding<'tcx>),
481-
{
482-
fn visit_candidate(&mut self, candidate: &Candidate<'_, 'tcx>) {
483-
for binding in &candidate.extra_data.bindings {
484-
(self.f)(binding)
485-
}
486-
for match_pair in &candidate.match_pairs {
487-
self.visit_match_pair(match_pair);
488-
}
489-
}
490-
491-
fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'_, 'tcx>) {
492-
for binding in &flat_pat.extra_data.bindings {
493-
(self.f)(binding)
494-
}
495-
for match_pair in &flat_pat.match_pairs {
496-
self.visit_match_pair(match_pair);
497-
}
498-
}
499-
500-
fn visit_match_pair(&mut self, match_pair: &MatchPair<'_, 'tcx>) {
501-
if let TestCase::Or { pats, .. } = &match_pair.test_case {
502-
// All the or-alternatives should bind the same locals, so we only visit the first one.
503-
self.visit_flat_pat(&pats[0])
504-
} else {
505-
for subpair in &match_pair.subpairs {
506-
self.visit_match_pair(subpair);
507-
}
508-
}
509-
}
510-
}
511-
512459
#[must_use]
513460
pub(crate) fn ref_pat_borrow_kind(ref_mutability: Mutability) -> BorrowKind {
514461
match ref_mutability {

0 commit comments

Comments
 (0)