2020-01-02 04:18:45 +00:00
|
|
|
use crate::def::{CtorOf, DefKind, Res};
|
|
|
|
use crate::def_id::DefId;
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::hir::{self, HirId, PatKind};
|
2021-03-28 04:51:31 +00:00
|
|
|
use rustc_data_structures::stable_set::FxHashSet;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2012-01-15 00:05:07 +00:00
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
use std::iter::{Enumerate, ExactSizeIterator};
|
2015-11-04 06:02:22 +00:00
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
pub struct EnumerateAndAdjust<I> {
|
|
|
|
enumerate: Enumerate<I>,
|
2016-03-06 12:54:44 +00:00
|
|
|
gap_pos: usize,
|
|
|
|
gap_len: usize,
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
impl<I> Iterator for EnumerateAndAdjust<I>
|
|
|
|
where
|
|
|
|
I: Iterator,
|
|
|
|
{
|
2016-03-06 12:54:44 +00:00
|
|
|
type Item = (usize, <I as Iterator>::Item);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.enumerate
|
|
|
|
.next()
|
|
|
|
.map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem))
|
2016-03-06 12:54:44 +00:00
|
|
|
}
|
2018-03-20 09:33:59 +00:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.enumerate.size_hint()
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:54:44 +00:00
|
|
|
pub trait EnumerateAndAdjustIterator {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn enumerate_and_adjust(
|
|
|
|
self,
|
|
|
|
expected_len: usize,
|
|
|
|
gap_pos: Option<usize>,
|
|
|
|
) -> EnumerateAndAdjust<Self>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
2016-03-06 12:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn enumerate_and_adjust(
|
|
|
|
self,
|
|
|
|
expected_len: usize,
|
|
|
|
gap_pos: Option<usize>,
|
|
|
|
) -> EnumerateAndAdjust<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2016-03-06 12:54:44 +00:00
|
|
|
let actual_len = self.len();
|
|
|
|
EnumerateAndAdjust {
|
|
|
|
enumerate: self.enumerate(),
|
2018-04-02 23:45:06 +00:00
|
|
|
gap_pos: gap_pos.unwrap_or(expected_len),
|
2016-03-06 12:54:44 +00:00
|
|
|
gap_len: expected_len - actual_len,
|
|
|
|
}
|
2016-03-06 12:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
impl hir::Pat<'_> {
|
2016-11-25 11:21:19 +00:00
|
|
|
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
|
|
|
|
/// `match foo() { Some(a) => (), None => () }`
|
2020-04-19 11:00:18 +00:00
|
|
|
pub fn each_binding(&self, mut f: impl FnMut(hir::BindingAnnotation, HirId, Span, Ident)) {
|
2019-12-14 22:43:21 +00:00
|
|
|
self.walk_always(|p| {
|
2019-09-26 15:18:31 +00:00
|
|
|
if let PatKind::Binding(binding_mode, _, ident, _) = p.kind {
|
2018-06-10 16:33:30 +00:00
|
|
|
f(binding_mode, p.hir_id, p.span, ident);
|
2016-11-25 11:21:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-01-15 19:39:08 +00:00
|
|
|
|
2019-09-15 01:41:21 +00:00
|
|
|
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
|
|
|
|
/// `match foo() { Some(a) => (), None => () }`.
|
|
|
|
///
|
|
|
|
/// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
|
2019-09-21 13:49:15 +00:00
|
|
|
pub fn each_binding_or_first(
|
|
|
|
&self,
|
2020-04-19 11:00:18 +00:00
|
|
|
f: &mut impl FnMut(hir::BindingAnnotation, HirId, Span, Ident),
|
2019-09-21 13:49:15 +00:00
|
|
|
) {
|
2019-09-26 15:18:31 +00:00
|
|
|
self.walk(|p| match &p.kind {
|
2019-09-21 13:49:15 +00:00
|
|
|
PatKind::Or(ps) => {
|
|
|
|
ps[0].each_binding_or_first(f);
|
|
|
|
false
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
PatKind::Binding(bm, _, ident, _) => {
|
2019-09-21 13:49:15 +00:00
|
|
|
f(*bm, p.hir_id, p.span, *ident);
|
|
|
|
true
|
2019-09-15 01:41:21 +00:00
|
|
|
}
|
2019-09-21 13:49:15 +00:00
|
|
|
_ => true,
|
|
|
|
})
|
2019-09-15 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
pub fn simple_ident(&self) -> Option<Ident> {
|
2019-09-26 15:18:31 +00:00
|
|
|
match self.kind {
|
2020-04-17 00:38:52 +00:00
|
|
|
PatKind::Binding(
|
|
|
|
hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Mutable,
|
|
|
|
_,
|
|
|
|
ident,
|
|
|
|
None,
|
|
|
|
) => Some(ident),
|
2018-05-11 14:24:04 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns variants that are necessary to exist for the pattern to match.
|
2016-11-25 11:21:19 +00:00
|
|
|
pub fn necessary_variants(&self) -> Vec<DefId> {
|
|
|
|
let mut variants = vec![];
|
2019-09-26 15:18:31 +00:00
|
|
|
self.walk(|p| match &p.kind {
|
2019-09-21 13:49:15 +00:00
|
|
|
PatKind::Or(_) => false,
|
2019-12-22 22:42:04 +00:00
|
|
|
PatKind::Path(hir::QPath::Resolved(_, path))
|
|
|
|
| PatKind::TupleStruct(hir::QPath::Resolved(_, path), ..)
|
|
|
|
| PatKind::Struct(hir::QPath::Resolved(_, path), ..) => {
|
2020-04-17 00:38:52 +00:00
|
|
|
if let Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), id) =
|
|
|
|
path.res
|
2019-09-21 13:49:15 +00:00
|
|
|
{
|
|
|
|
variants.push(id);
|
2016-11-25 11:21:19 +00:00
|
|
|
}
|
2019-09-21 13:49:15 +00:00
|
|
|
true
|
2016-11-25 11:21:19 +00:00
|
|
|
}
|
2019-09-21 13:49:15 +00:00
|
|
|
_ => true,
|
2016-11-25 11:21:19 +00:00
|
|
|
});
|
2021-03-28 04:51:31 +00:00
|
|
|
// We remove duplicates by inserting into a `FxHashSet` to avoid re-ordering
|
|
|
|
// the bounds
|
|
|
|
let mut duplicates = FxHashSet::default();
|
|
|
|
variants.retain(|def_id| duplicates.insert(*def_id));
|
2016-11-25 11:21:19 +00:00
|
|
|
variants
|
|
|
|
}
|
2015-06-02 11:31:40 +00:00
|
|
|
|
2017-10-06 20:30:23 +00:00
|
|
|
/// Checks if the pattern contains any `ref` or `ref mut` bindings, and if
|
|
|
|
/// yes whether it contains mutable or just immutables ones.
|
2019-02-08 13:53:55 +00:00
|
|
|
//
|
|
|
|
// FIXME(tschottdorf): this is problematic as the HIR is being scraped, but
|
|
|
|
// ref bindings are be implicit after #42640 (default match binding modes). See issue #44848.
|
2017-07-21 23:29:43 +00:00
|
|
|
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
|
2016-11-25 11:21:19 +00:00
|
|
|
let mut result = None;
|
2019-12-22 22:42:04 +00:00
|
|
|
self.each_binding(|annotation, _, _, _| match annotation {
|
|
|
|
hir::BindingAnnotation::Ref => match result {
|
|
|
|
None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
|
2019-09-21 13:49:15 +00:00
|
|
|
_ => {}
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
hir::BindingAnnotation::RefMut => result = Some(hir::Mutability::Mut),
|
|
|
|
_ => {}
|
2016-11-25 11:21:19 +00:00
|
|
|
});
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|