Skip to content

Commit 9ca42a5

Browse files
committed
or-patterns: liveness: generalize + remove top_pats_hack.
1 parent cc5fe6d commit 9ca42a5

File tree

2 files changed

+141
-175
lines changed

2 files changed

+141
-175
lines changed

src/librustc/hir/pat_util.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ impl hir::Pat {
7777
});
7878
}
7979

80+
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
81+
/// `match foo() { Some(a) => (), None => () }`.
82+
///
83+
/// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
84+
pub fn each_binding_or_first<F>(&self, c: &mut F)
85+
where F: FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
86+
{
87+
match &self.node {
88+
PatKind::Binding(bm, _, ident, sub) => {
89+
c(*bm, self.hir_id, self.span, *ident);
90+
sub.iter().for_each(|p| p.each_binding_or_first(c));
91+
}
92+
PatKind::Or(ps) => ps[0].each_binding_or_first(c),
93+
PatKind::Struct(_, fs, _) => fs.iter().for_each(|f| f.pat.each_binding_or_first(c)),
94+
PatKind::TupleStruct(_, ps, _) | PatKind::Tuple(ps, _) => {
95+
ps.iter().for_each(|p| p.each_binding_or_first(c));
96+
}
97+
PatKind::Box(p) | PatKind::Ref(p, _) => p.each_binding_or_first(c),
98+
PatKind::Slice(before, slice, after) => {
99+
before.iter()
100+
.chain(slice.iter())
101+
.chain(after.iter())
102+
.for_each(|p| p.each_binding_or_first(c));
103+
}
104+
PatKind::Wild | PatKind::Lit(_) | PatKind::Range(..) | PatKind::Path(_) => {}
105+
}
106+
}
107+
80108
/// Checks if the pattern contains any patterns that bind something to
81109
/// an ident, e.g., `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
82110
pub fn contains_bindings(&self) -> bool {

0 commit comments

Comments
 (0)