1 use crate::def::{CtorOf, DefKind, Res};
2 use crate::def_id::DefId;
3 use crate::hir::{self, HirId, PatKind};
7 use std::iter::{Enumerate, ExactSizeIterator};
9 pub struct EnumerateAndAdjust<I> {
10 enumerate: Enumerate<I>,
15 impl<I> Iterator for EnumerateAndAdjust<I>
19 type Item = (usize, <I as Iterator>::Item);
21 fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
24 .map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem))
27 fn size_hint(&self) -> (usize, Option<usize>) {
28 self.enumerate.size_hint()
32 pub trait EnumerateAndAdjustIterator {
33 fn enumerate_and_adjust(
36 gap_pos: Option<usize>,
37 ) -> EnumerateAndAdjust<Self>
42 impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
43 fn enumerate_and_adjust(
46 gap_pos: Option<usize>,
47 ) -> EnumerateAndAdjust<Self>
51 let actual_len = self.len();
53 enumerate: self.enumerate(),
54 gap_pos: gap_pos.unwrap_or(expected_len),
55 gap_len: expected_len - actual_len,
61 pub fn is_refutable(&self) -> bool {
65 | PatKind::Path(hir::QPath::Resolved(Some(..), _))
66 | PatKind::Path(hir::QPath::TypeRelative(..)) => true,
68 PatKind::Path(hir::QPath::Resolved(_, ref path))
69 | PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..)
70 | PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => match path.res {
71 Res::Def(DefKind::Variant, _) => true,
74 PatKind::Slice(..) => true,
79 /// Call `f` on every "binding" in a pattern, e.g., on `a` in
80 /// `match foo() { Some(a) => (), None => () }`
81 pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident)) {
82 self.walk_always(|p| {
83 if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
84 f(binding_mode, p.hir_id, p.span, ident);
89 /// Call `f` on every "binding" in a pattern, e.g., on `a` in
90 /// `match foo() { Some(a) => (), None => () }`.
92 /// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
93 pub fn each_binding_or_first(
95 f: &mut impl FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
97 self.walk(|p| match &p.kind {
99 ps[0].each_binding_or_first(f);
102 PatKind::Binding(bm, _, ident, _) => {
103 f(*bm, p.hir_id, p.span, *ident);
110 /// Checks if the pattern contains any patterns that bind something to
111 /// an ident, e.g., `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
112 pub fn contains_bindings(&self) -> bool {
113 self.satisfies(|p| match p.kind {
114 PatKind::Binding(..) => true,
119 /// Checks if the pattern contains any patterns that bind something to
120 /// an ident or wildcard, e.g., `foo`, or `Foo(_)`, `foo @ Bar(..)`,
121 pub fn contains_bindings_or_wild(&self) -> bool {
122 self.satisfies(|p| match p.kind {
123 PatKind::Binding(..) | PatKind::Wild => true,
128 /// Checks if the pattern satisfies the given predicate on some sub-pattern.
129 fn satisfies(&self, pred: impl Fn(&hir::Pat<'_>) -> bool) -> bool {
130 let mut satisfies = false;
131 self.walk_short(|p| {
134 false // Found one, can short circuit now.
142 pub fn simple_ident(&self) -> Option<ast::Ident> {
144 PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None)
145 | PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
150 /// Returns variants that are necessary to exist for the pattern to match.
151 pub fn necessary_variants(&self) -> Vec<DefId> {
152 let mut variants = vec![];
153 self.walk(|p| match &p.kind {
154 PatKind::Or(_) => false,
155 PatKind::Path(hir::QPath::Resolved(_, path))
156 | PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
157 | PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
158 if let Res::Def(DefKind::Variant, id)
159 | Res::Def(DefKind::Ctor(CtorOf::Variant, ..), id) = path.res
172 /// Checks if the pattern contains any `ref` or `ref mut` bindings, and if
173 /// yes whether it contains mutable or just immutables ones.
175 // FIXME(tschottdorf): this is problematic as the HIR is being scraped, but
176 // ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
177 pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
178 let mut result = None;
179 self.each_binding(|annotation, _, _, _| match annotation {
180 hir::BindingAnnotation::Ref => match result {
181 None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
184 hir::BindingAnnotation::RefMut => result = Some(hir::Mutability::Mut),