1 use crate::hir::def::Def;
2 use crate::hir::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> where I: Iterator {
16 type Item = (usize, <I as Iterator>::Item);
18 fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
19 self.enumerate.next().map(|(i, elem)| {
20 (if i < self.gap_pos { i } else { i + self.gap_len }, elem)
24 fn size_hint(&self) -> (usize, Option<usize>) {
25 self.enumerate.size_hint()
29 pub trait EnumerateAndAdjustIterator {
30 fn enumerate_and_adjust(self, expected_len: usize, gap_pos: Option<usize>)
31 -> EnumerateAndAdjust<Self> where Self: Sized;
34 impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
35 fn enumerate_and_adjust(self, expected_len: usize, gap_pos: Option<usize>)
36 -> EnumerateAndAdjust<Self> where Self: Sized {
37 let actual_len = self.len();
39 enumerate: self.enumerate(),
40 gap_pos: gap_pos.unwrap_or(expected_len),
41 gap_len: expected_len - actual_len,
47 pub fn is_refutable(&self) -> bool {
51 PatKind::Path(hir::QPath::Resolved(Some(..), _)) |
52 PatKind::Path(hir::QPath::TypeRelative(..)) => true,
54 PatKind::Path(hir::QPath::Resolved(_, ref path)) |
55 PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
56 PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
58 Def::Variant(..) | Def::VariantCtor(..) => true,
62 PatKind::Slice(..) => true,
67 /// Call `f` on every "binding" in a pattern, e.g., on `a` in
68 /// `match foo() { Some(a) => (), None => () }`
69 pub fn each_binding<F>(&self, mut f: F)
70 where F: FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
73 if let PatKind::Binding(binding_mode, _, ident, _) = p.node {
74 f(binding_mode, p.hir_id, p.span, ident);
80 /// Checks if the pattern contains any patterns that bind something to
81 /// an ident, e.g., `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
82 pub fn contains_bindings(&self) -> bool {
83 let mut contains_bindings = false;
85 if let PatKind::Binding(..) = p.node {
86 contains_bindings = true;
87 false // there's at least one binding, can short circuit now.
95 /// Checks if the pattern contains any patterns that bind something to
96 /// an ident or wildcard, e.g., `foo`, or `Foo(_)`, `foo @ Bar(..)`,
97 pub fn contains_bindings_or_wild(&self) -> bool {
98 let mut contains_bindings = false;
101 PatKind::Binding(..) | PatKind::Wild => {
102 contains_bindings = true;
103 false // there's at least one binding/wildcard, can short circuit now.
111 pub fn simple_ident(&self) -> Option<ast::Ident> {
113 PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None) |
114 PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
119 /// Returns variants that are necessary to exist for the pattern to match.
120 pub fn necessary_variants(&self) -> Vec<DefId> {
121 let mut variants = vec![];
124 PatKind::Path(hir::QPath::Resolved(_, ref path)) |
125 PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
126 PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
129 Def::VariantCtor(id, ..) => variants.push(id),
142 /// Checks if the pattern contains any `ref` or `ref mut` bindings, and if
143 /// yes whether it contains mutable or just immutables ones.
145 // FIXME(tschottdorf): this is problematic as the HIR is being scraped, but
146 // ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
147 pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
148 let mut result = None;
149 self.each_binding(|annotation, _, _, _| {
151 hir::BindingAnnotation::Ref => {
153 None | Some(hir::MutImmutable) => result = Some(hir::MutImmutable),
157 hir::BindingAnnotation::RefMut => result = Some(hir::MutMutable),
166 /// Checks if the patterns for this arm contain any `ref` or `ref mut`
167 /// bindings, and if yes whether its containing mutable ones or just immutables ones.
168 pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
169 // FIXME(tschottdorf): contains_explicit_ref_binding() must be removed
170 // for #42640 (default match binding modes).
174 .filter_map(|pat| pat.contains_explicit_ref_binding())
175 .max_by_key(|m| match *m {
176 hir::MutMutable => 1,
177 hir::MutImmutable => 0,