Skip to content

Commit

Permalink
Factor out constructing a new wildcard pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Nov 6, 2019
1 parent 816aee2 commit 7514c48
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
10 changes: 3 additions & 7 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,7 @@ impl<'tcx> Constructor<'tcx> {
cx: &MatchCheckCtxt<'a, 'tcx>,
ty: Ty<'tcx>,
) -> impl Iterator<Item = Pat<'tcx>> + DoubleEndedIterator {
constructor_sub_pattern_tys(cx, self, ty).into_iter().map(|ty| Pat {
ty,
span: DUMMY_SP,
kind: box PatKind::Wild,
})
constructor_sub_pattern_tys(cx, self, ty).into_iter().map(Pat::wildcard_from_ty)
}

/// This computes the arity of a constructor. The arity of a constructor
Expand Down Expand Up @@ -862,7 +858,7 @@ impl<'tcx> Constructor<'tcx> {
VarLenSlice(prefix_len, _suffix_len) => {
let prefix = subpatterns.by_ref().take(*prefix_len as usize).collect();
let suffix = subpatterns.collect();
let wild = Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) };
let wild = Pat::wildcard_from_ty(ty);
PatKind::Slice { prefix, slice: Some(wild), suffix }
}
_ => bug!("bad slice pattern {:?} {:?}", self, ty),
Expand Down Expand Up @@ -931,7 +927,7 @@ impl<'tcx> Usefulness<'tcx> {
fn apply_wildcard(self, ty: Ty<'tcx>) -> Self {
match self {
UsefulWithWitness(witnesses) => {
let wild = Pat { ty, span: DUMMY_SP, kind: box PatKind::Wild };
let wild = Pat::wildcard_from_ty(ty);
UsefulWithWitness(
witnesses
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc::hir::{self, Pat};

use std::slice;

use syntax_pos::{MultiSpan, Span, DUMMY_SP};
use syntax_pos::{MultiSpan, Span};

crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
let body_id = match tcx.hir().as_local_hir_id(def_id) {
Expand Down Expand Up @@ -491,7 +491,7 @@ fn check_not_useful(
matrix: &Matrix<'_, 'tcx>,
hir_id: HirId,
) -> Result<(), Vec<super::Pat<'tcx>>> {
let wild_pattern = super::Pat { ty, span: DUMMY_SP, kind: box PatKind::Wild };
let wild_pattern = super::Pat::wildcard_from_ty(ty);
match is_useful(cx, matrix, &PatStack::from_pattern(&wild_pattern), ConstructWitness, hir_id) {
NotUseful => Ok(()), // This is good, wildcard pattern isn't reachable.
UsefulWithWitness(pats) => Err(if pats.is_empty() {
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_index::vec::Idx;
use std::cmp::Ordering;
use std::fmt;
use syntax::ast;
use syntax_pos::Span;
use syntax_pos::{Span, DUMMY_SP};

#[derive(Clone, Debug)]
pub enum PatternError {
Expand Down Expand Up @@ -55,6 +55,11 @@ pub struct Pat<'tcx> {
pub kind: Box<PatKind<'tcx>>,
}

impl<'tcx> Pat<'tcx> {
pub(crate) fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
}
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct PatTyProj<'tcx> {
Expand Down

0 comments on commit 7514c48

Please sign in to comment.