Skip to content

Commit e06a010

Browse files
committed
Visiting bindings is straightforward now
1 parent cbdacec commit e06a010

File tree

2 files changed

+14
-65
lines changed

2 files changed

+14
-65
lines changed

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_span::symbol::Symbol;
1717
use rustc_span::{BytePos, Pos, Span};
1818
use rustc_target::abi::VariantIdx;
1919
use tracing::{debug, instrument};
20-
use util::visit_bindings;
2120

2221
use crate::build::expr::as_place::PlaceBuilder;
2322
use crate::build::scope::DropKind;
@@ -701,7 +700,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
701700
initializer: PlaceBuilder<'tcx>,
702701
set_match_place: bool,
703702
) -> BlockAnd<()> {
704-
let mut candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
703+
let candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
704+
let built_tree = self.lower_match_tree(
705+
block,
706+
irrefutable_pat.span,
707+
&initializer,
708+
irrefutable_pat.span,
709+
vec![candidate],
710+
false,
711+
);
712+
let [branch] = built_tree.branches.try_into().unwrap();
705713

706714
// For matches and function arguments, the place that is being matched
707715
// can be set when creating the variables. But the place for
@@ -722,7 +730,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
722730
// };
723731
// ```
724732
if let Some(place) = initializer.try_to_place(self) {
725-
visit_bindings(&[&mut candidate], |binding: &Binding<'_>| {
733+
// Because or-alternatives bind the same variables, we only explore the first one.
734+
let first_sub_branch = branch.sub_branches.first().unwrap();
735+
for binding in &first_sub_branch.bindings {
726736
let local = self.var_local_id(binding.var_id, OutsideGuard);
727737
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
728738
opt_match_place: Some((ref mut match_place, _)),
@@ -733,20 +743,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
733743
} else {
734744
bug!("Let binding to non-user variable.")
735745
};
736-
});
746+
}
737747
}
738748
}
739749

740-
let built_tree = self.lower_match_tree(
741-
block,
742-
irrefutable_pat.span,
743-
&initializer,
744-
irrefutable_pat.span,
745-
vec![candidate],
746-
false,
747-
);
748-
let [branch] = built_tree.branches.try_into().unwrap();
749-
750750
self.bind_pattern(
751751
self.source_info(irrefutable_pat.span),
752752
branch,

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

-51
Original file line numberDiff line numberDiff line change
@@ -221,57 +221,6 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
221221
}
222222
}
223223

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

0 commit comments

Comments
 (0)