Skip to content

Commit b9aba74

Browse files
committed
improve robustness of pat walkers
1 parent 5a8baa2 commit b9aba74

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/librustc/hir/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,16 @@ impl Pat {
921921
pub fn walk(&self, mut it: impl FnMut(&Pat) -> bool) {
922922
self.walk_(&mut it)
923923
}
924+
925+
/// Walk the pattern in left-to-right order.
926+
///
927+
/// If you always want to recurse, prefer this method over `walk`.
928+
pub fn walk_always(&self, mut it: impl FnMut(&Pat)) {
929+
self.walk(|p| {
930+
it(p);
931+
true
932+
})
933+
}
924934
}
925935

926936
/// A single field in a struct pattern.

src/librustc/hir/pat_util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ impl hir::Pat {
7979
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
8080
/// `match foo() { Some(a) => (), None => () }`
8181
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident)) {
82-
self.walk(|p| {
82+
self.walk_always(|p| {
8383
if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
8484
f(binding_mode, p.hir_id, p.span, ident);
8585
}
86-
true
8786
});
8887
}
8988

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, pa
269269
}
270270

271271
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
272-
pat.walk(|p| {
272+
pat.walk_always(|p| {
273273
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
274274
if let Some(ty::BindByValue(hir::Mutability::Not)) =
275275
cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
@@ -303,7 +303,6 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
303303
}
304304
}
305305
}
306-
true
307306
});
308307
}
309308

@@ -602,7 +601,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
602601
by_move_spans.push(p.span);
603602
}
604603
};
605-
pat.walk(|p| {
604+
pat.walk_always(|p| {
606605
if let hir::PatKind::Binding(.., sub) = &p.kind {
607606
if let Some(ty::BindByValue(_)) = tables.extract_binding_mode(sess, p.hir_id, p.span) {
608607
let pat_ty = tables.node_type(p.hir_id);
@@ -611,7 +610,6 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
611610
}
612611
}
613612
}
614-
true
615613
});
616614

617615
// Found some bad by-move spans, error!
@@ -640,16 +638,16 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
640638
ty::BindByValue(_) => None,
641639
ty::BindByReference(m) => Some(m),
642640
};
643-
pat.walk(|pat| {
641+
pat.walk_always(|pat| {
644642
// Extract `sub` in `binding @ sub`.
645643
let (name, sub) = match &pat.kind {
646644
hir::PatKind::Binding(.., name, Some(sub)) => (*name, sub),
647-
_ => return true,
645+
_ => return,
648646
};
649647

650648
// Extract the mutability.
651649
let mut_outer = match extract_binding_mut(pat.hir_id, pat.span) {
652-
None => return true,
650+
None => return,
653651
Some(m) => m,
654652
};
655653

@@ -698,8 +696,6 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
698696
}
699697
err.emit();
700698
}
701-
702-
true
703699
});
704700
}
705701

0 commit comments

Comments
 (0)