Skip to content

Commit b3823ef

Browse files
committed
Visiting bindings is straightforward now
1 parent bdc477c commit b3823ef

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 match_pair;
@@ -696,7 +695,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
696695
initializer: PlaceBuilder<'tcx>,
697696
set_match_place: bool,
698697
) -> BlockAnd<()> {
699-
let mut candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
698+
let candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
699+
let built_tree = self.lower_match_tree(
700+
block,
701+
irrefutable_pat.span,
702+
&initializer,
703+
irrefutable_pat.span,
704+
vec![candidate],
705+
false,
706+
);
707+
let [branch] = built_tree.branches.try_into().unwrap();
700708

701709
// For matches and function arguments, the place that is being matched
702710
// can be set when creating the variables. But the place for
@@ -717,7 +725,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
717725
// };
718726
// ```
719727
if let Some(place) = initializer.try_to_place(self) {
720-
visit_bindings(&[&mut candidate], |binding: &Binding<'_>| {
728+
// Because or-alternatives bind the same variables, we only explore the first one.
729+
let first_sub_branch = branch.sub_branches.first().unwrap();
730+
for binding in &first_sub_branch.bindings {
721731
let local = self.var_local_id(binding.var_id, OutsideGuard);
722732
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
723733
opt_match_place: Some((ref mut match_place, _)),
@@ -728,20 +738,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
728738
} else {
729739
bug!("Let binding to non-user variable.")
730740
};
731-
});
741+
}
732742
}
733743
}
734744

735-
let built_tree = self.lower_match_tree(
736-
block,
737-
irrefutable_pat.span,
738-
&initializer,
739-
irrefutable_pat.span,
740-
vec![candidate],
741-
false,
742-
);
743-
let [branch] = built_tree.branches.try_into().unwrap();
744-
745745
self.bind_pattern(
746746
self.source_info(irrefutable_pat.span),
747747
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;
42
use crate::build::matches::{Binding, Candidate, FlatPat, MatchPairTree, TestCase};
53
use crate::build::Builder;
@@ -220,57 +218,6 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
220218
}
221219
}
222220

223-
/// Visit all the bindings of these candidates. Because or-alternatives bind the same variables, we
224-
/// only explore the first one of each or-pattern.
225-
pub(super) fn visit_bindings<'tcx>(
226-
candidates: &[&mut Candidate<'_, 'tcx>],
227-
f: impl FnMut(&Binding<'tcx>),
228-
) {
229-
let mut visitor = BindingsVisitor { f, phantom: PhantomData };
230-
for candidate in candidates.iter() {
231-
visitor.visit_candidate(candidate);
232-
}
233-
}
234-
235-
pub(super) struct BindingsVisitor<'tcx, F> {
236-
f: F,
237-
phantom: PhantomData<&'tcx ()>,
238-
}
239-
240-
impl<'tcx, F> BindingsVisitor<'tcx, F>
241-
where
242-
F: FnMut(&Binding<'tcx>),
243-
{
244-
fn visit_candidate(&mut self, candidate: &Candidate<'_, 'tcx>) {
245-
for binding in &candidate.extra_data.bindings {
246-
(self.f)(binding)
247-
}
248-
for match_pair in &candidate.match_pairs {
249-
self.visit_match_pair(match_pair);
250-
}
251-
}
252-
253-
fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'_, 'tcx>) {
254-
for binding in &flat_pat.extra_data.bindings {
255-
(self.f)(binding)
256-
}
257-
for match_pair in &flat_pat.match_pairs {
258-
self.visit_match_pair(match_pair);
259-
}
260-
}
261-
262-
fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'_, 'tcx>) {
263-
if let TestCase::Or { pats, .. } = &match_pair.test_case {
264-
// All the or-alternatives should bind the same locals, so we only visit the first one.
265-
self.visit_flat_pat(&pats[0])
266-
} else {
267-
for subpair in &match_pair.subpairs {
268-
self.visit_match_pair(subpair);
269-
}
270-
}
271-
}
272-
}
273-
274221
#[must_use]
275222
pub(crate) fn ref_pat_borrow_kind(ref_mutability: Mutability) -> BorrowKind {
276223
match ref_mutability {

0 commit comments

Comments
 (0)