]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/pat_util.rs
c0aa54beac2759239443c23d882259eed9c33a0b
[rust.git] / src / librustc / hir / pat_util.rs
1 use crate::hir::def::{CtorOf, DefKind, Res};
2 use crate::hir::def_id::DefId;
3 use crate::hir::{self, HirId, PatKind};
4 use syntax::ast;
5 use syntax_pos::Span;
6
7 use std::iter::{Enumerate, ExactSizeIterator};
8
9 pub struct EnumerateAndAdjust<I> {
10     enumerate: Enumerate<I>,
11     gap_pos: usize,
12     gap_len: usize,
13 }
14
15 impl<I> Iterator for EnumerateAndAdjust<I>
16 where
17     I: Iterator,
18 {
19     type Item = (usize, <I as Iterator>::Item);
20
21     fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
22         self.enumerate
23             .next()
24             .map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem))
25     }
26
27     fn size_hint(&self) -> (usize, Option<usize>) {
28         self.enumerate.size_hint()
29     }
30 }
31
32 pub trait EnumerateAndAdjustIterator {
33     fn enumerate_and_adjust(
34         self,
35         expected_len: usize,
36         gap_pos: Option<usize>,
37     ) -> EnumerateAndAdjust<Self>
38     where
39         Self: Sized;
40 }
41
42 impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
43     fn enumerate_and_adjust(
44         self,
45         expected_len: usize,
46         gap_pos: Option<usize>,
47     ) -> EnumerateAndAdjust<Self>
48     where
49         Self: Sized,
50     {
51         let actual_len = self.len();
52         EnumerateAndAdjust {
53             enumerate: self.enumerate(),
54             gap_pos: gap_pos.unwrap_or(expected_len),
55             gap_len: expected_len - actual_len,
56         }
57     }
58 }
59
60 impl hir::Pat {
61     pub fn is_refutable(&self) -> bool {
62         match self.kind {
63             PatKind::Lit(_)
64             | PatKind::Range(..)
65             | PatKind::Path(hir::QPath::Resolved(Some(..), _))
66             | PatKind::Path(hir::QPath::TypeRelative(..)) => true,
67
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,
72                 _ => false,
73             },
74             PatKind::Slice(..) => true,
75             _ => false,
76         }
77     }
78
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(|p| {
83             if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
84                 f(binding_mode, p.hir_id, p.span, ident);
85             }
86             true
87         });
88     }
89
90     /// Call `f` on every "binding" in a pattern, e.g., on `a` in
91     /// `match foo() { Some(a) => (), None => () }`.
92     ///
93     /// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
94     pub fn each_binding_or_first(
95         &self,
96         f: &mut impl FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
97     ) {
98         self.walk(|p| match &p.kind {
99             PatKind::Or(ps) => {
100                 ps[0].each_binding_or_first(f);
101                 false
102             }
103             PatKind::Binding(bm, _, ident, _) => {
104                 f(*bm, p.hir_id, p.span, *ident);
105                 true
106             }
107             _ => true,
108         })
109     }
110
111     /// Checks if the pattern contains any patterns that bind something to
112     /// an ident, e.g., `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
113     pub fn contains_bindings(&self) -> bool {
114         self.satisfies(|p| match p.kind {
115             PatKind::Binding(..) => true,
116             _ => false,
117         })
118     }
119
120     /// Checks if the pattern contains any patterns that bind something to
121     /// an ident or wildcard, e.g., `foo`, or `Foo(_)`, `foo @ Bar(..)`,
122     pub fn contains_bindings_or_wild(&self) -> bool {
123         self.satisfies(|p| match p.kind {
124             PatKind::Binding(..) | PatKind::Wild => true,
125             _ => false,
126         })
127     }
128
129     /// Checks if the pattern satisfies the given predicate on some sub-pattern.
130     fn satisfies(&self, pred: impl Fn(&Self) -> bool) -> bool {
131         let mut satisfies = false;
132         self.walk_short(|p| {
133             if pred(p) {
134                 satisfies = true;
135                 false // Found one, can short circuit now.
136             } else {
137                 true
138             }
139         });
140         satisfies
141     }
142
143     pub fn simple_ident(&self) -> Option<ast::Ident> {
144         match self.kind {
145             PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None)
146             | PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
147             _ => None,
148         }
149     }
150
151     /// Returns variants that are necessary to exist for the pattern to match.
152     pub fn necessary_variants(&self) -> Vec<DefId> {
153         let mut variants = vec![];
154         self.walk(|p| match &p.kind {
155             PatKind::Or(_) => false,
156             PatKind::Path(hir::QPath::Resolved(_, path))
157             | PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
158             | PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
159                 if let Res::Def(DefKind::Variant, id)
160                 | Res::Def(DefKind::Ctor(CtorOf::Variant, ..), id) = path.res
161                 {
162                     variants.push(id);
163                 }
164                 true
165             }
166             _ => true,
167         });
168         variants.sort();
169         variants.dedup();
170         variants
171     }
172
173     /// Checks if the pattern contains any `ref` or `ref mut` bindings, and if
174     /// yes whether it contains mutable or just immutables ones.
175     //
176     // FIXME(tschottdorf): this is problematic as the HIR is being scraped, but
177     // ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
178     pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
179         let mut result = None;
180         self.each_binding(|annotation, _, _, _| match annotation {
181             hir::BindingAnnotation::Ref => match result {
182                 None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
183                 _ => {}
184             },
185             hir::BindingAnnotation::RefMut => result = Some(hir::Mutability::Mut),
186             _ => {}
187         });
188         result
189     }
190 }