Avoid PatOrWild
glob import
This commit is contained in:
parent
1a3edc169b
commit
5c65e9fdaf
1 changed files with 18 additions and 18 deletions
|
@ -10,7 +10,6 @@ use crate::usefulness::PlaceCtxt;
|
||||||
use crate::{Captures, TypeCx};
|
use crate::{Captures, TypeCx};
|
||||||
|
|
||||||
use self::Constructor::*;
|
use self::Constructor::*;
|
||||||
use self::PatOrWild::*;
|
|
||||||
|
|
||||||
/// Values and patterns can be represented as a constructor applied to some fields. This represents
|
/// Values and patterns can be represented as a constructor applied to some fields. This represents
|
||||||
/// a pattern in this form.
|
/// a pattern in this form.
|
||||||
|
@ -75,7 +74,7 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
|
||||||
other_ctor: &Constructor<Cx>,
|
other_ctor: &Constructor<Cx>,
|
||||||
ctor_arity: usize,
|
ctor_arity: usize,
|
||||||
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
|
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
|
||||||
let wildcard_sub_tys = || (0..ctor_arity).map(|_| Wild).collect();
|
let wildcard_sub_tys = || (0..ctor_arity).map(|_| PatOrWild::Wild).collect();
|
||||||
match (&self.ctor, other_ctor) {
|
match (&self.ctor, other_ctor) {
|
||||||
// Return a wildcard for each field of `other_ctor`.
|
// Return a wildcard for each field of `other_ctor`.
|
||||||
(Wildcard, _) => wildcard_sub_tys(),
|
(Wildcard, _) => wildcard_sub_tys(),
|
||||||
|
@ -91,14 +90,15 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
|
||||||
// Fill in the fields from both ends.
|
// Fill in the fields from both ends.
|
||||||
let new_arity = fields.len();
|
let new_arity = fields.len();
|
||||||
for i in 0..prefix {
|
for i in 0..prefix {
|
||||||
fields[i] = Pat(&self.fields[i]);
|
fields[i] = PatOrWild::Pat(&self.fields[i]);
|
||||||
}
|
}
|
||||||
for i in 0..suffix {
|
for i in 0..suffix {
|
||||||
fields[new_arity - 1 - i] = Pat(&self.fields[self.fields.len() - 1 - i]);
|
fields[new_arity - 1 - i] =
|
||||||
|
PatOrWild::Pat(&self.fields[self.fields.len() - 1 - i]);
|
||||||
}
|
}
|
||||||
fields
|
fields
|
||||||
}
|
}
|
||||||
_ => self.fields.iter().map(Pat).collect(),
|
_ => self.fields.iter().map(PatOrWild::Pat).collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,29 +162,29 @@ pub(crate) enum PatOrWild<'p, Cx: TypeCx> {
|
||||||
impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
|
impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
|
||||||
pub(crate) fn as_pat(&self) -> Option<&'p DeconstructedPat<'p, Cx>> {
|
pub(crate) fn as_pat(&self) -> Option<&'p DeconstructedPat<'p, Cx>> {
|
||||||
match self {
|
match self {
|
||||||
Wild => None,
|
PatOrWild::Wild => None,
|
||||||
Pat(pat) => Some(pat),
|
PatOrWild::Pat(pat) => Some(pat),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub(crate) fn ctor(self) -> &'p Constructor<Cx> {
|
pub(crate) fn ctor(self) -> &'p Constructor<Cx> {
|
||||||
match self {
|
match self {
|
||||||
Wild => &Wildcard,
|
PatOrWild::Wild => &Wildcard,
|
||||||
Pat(pat) => pat.ctor(),
|
PatOrWild::Pat(pat) => pat.ctor(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_or_pat(&self) -> bool {
|
pub(crate) fn is_or_pat(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Wild => false,
|
PatOrWild::Wild => false,
|
||||||
Pat(pat) => pat.is_or_pat(),
|
PatOrWild::Pat(pat) => pat.is_or_pat(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expand this (possibly-nested) or-pattern into its alternatives.
|
/// Expand this (possibly-nested) or-pattern into its alternatives.
|
||||||
pub(crate) fn flatten_or_pat(self) -> SmallVec<[Self; 1]> {
|
pub(crate) fn flatten_or_pat(self) -> SmallVec<[Self; 1]> {
|
||||||
match self {
|
match self {
|
||||||
Pat(pat) if pat.is_or_pat() => {
|
PatOrWild::Pat(pat) if pat.is_or_pat() => {
|
||||||
pat.iter_fields().flat_map(|p| Pat(p).flatten_or_pat()).collect()
|
pat.iter_fields().flat_map(|p| PatOrWild::Pat(p).flatten_or_pat()).collect()
|
||||||
}
|
}
|
||||||
_ => smallvec![self],
|
_ => smallvec![self],
|
||||||
}
|
}
|
||||||
|
@ -198,13 +198,13 @@ impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
|
||||||
ctor_arity: usize,
|
ctor_arity: usize,
|
||||||
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
|
) -> SmallVec<[PatOrWild<'p, Cx>; 2]> {
|
||||||
match self {
|
match self {
|
||||||
Wild => (0..ctor_arity).map(|_| Wild).collect(),
|
PatOrWild::Wild => (0..ctor_arity).map(|_| PatOrWild::Wild).collect(),
|
||||||
Pat(pat) => pat.specialize(other_ctor, ctor_arity),
|
PatOrWild::Pat(pat) => pat.specialize(other_ctor, ctor_arity),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_useful(&self) {
|
pub(crate) fn set_useful(&self) {
|
||||||
if let Pat(pat) = self {
|
if let PatOrWild::Pat(pat) = self {
|
||||||
pat.set_useful()
|
pat.set_useful()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -213,8 +213,8 @@ impl<'p, Cx: TypeCx> PatOrWild<'p, Cx> {
|
||||||
impl<'p, Cx: TypeCx> fmt::Debug for PatOrWild<'p, Cx> {
|
impl<'p, Cx: TypeCx> fmt::Debug for PatOrWild<'p, Cx> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Wild => write!(f, "_"),
|
PatOrWild::Wild => write!(f, "_"),
|
||||||
Pat(pat) => pat.fmt(f),
|
PatOrWild::Pat(pat) => pat.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue