Skip to content

Commit 7514c48

Browse files
committed
Factor out constructing a new wildcard pattern
1 parent 816aee2 commit 7514c48

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/librustc_mir/hair/pattern/_match.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,7 @@ impl<'tcx> Constructor<'tcx> {
780780
cx: &MatchCheckCtxt<'a, 'tcx>,
781781
ty: Ty<'tcx>,
782782
) -> impl Iterator<Item = Pat<'tcx>> + DoubleEndedIterator {
783-
constructor_sub_pattern_tys(cx, self, ty).into_iter().map(|ty| Pat {
784-
ty,
785-
span: DUMMY_SP,
786-
kind: box PatKind::Wild,
787-
})
783+
constructor_sub_pattern_tys(cx, self, ty).into_iter().map(Pat::wildcard_from_ty)
788784
}
789785

790786
/// This computes the arity of a constructor. The arity of a constructor
@@ -862,7 +858,7 @@ impl<'tcx> Constructor<'tcx> {
862858
VarLenSlice(prefix_len, _suffix_len) => {
863859
let prefix = subpatterns.by_ref().take(*prefix_len as usize).collect();
864860
let suffix = subpatterns.collect();
865-
let wild = Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) };
861+
let wild = Pat::wildcard_from_ty(ty);
866862
PatKind::Slice { prefix, slice: Some(wild), suffix }
867863
}
868864
_ => bug!("bad slice pattern {:?} {:?}", self, ty),
@@ -931,7 +927,7 @@ impl<'tcx> Usefulness<'tcx> {
931927
fn apply_wildcard(self, ty: Ty<'tcx>) -> Self {
932928
match self {
933929
UsefulWithWitness(witnesses) => {
934-
let wild = Pat { ty, span: DUMMY_SP, kind: box PatKind::Wild };
930+
let wild = Pat::wildcard_from_ty(ty);
935931
UsefulWithWitness(
936932
witnesses
937933
.into_iter()

src/librustc_mir/hair/pattern/check_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::hir::{self, Pat};
1818

1919
use std::slice;
2020

21-
use syntax_pos::{MultiSpan, Span, DUMMY_SP};
21+
use syntax_pos::{MultiSpan, Span};
2222

2323
crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
2424
let body_id = match tcx.hir().as_local_hir_id(def_id) {
@@ -491,7 +491,7 @@ fn check_not_useful(
491491
matrix: &Matrix<'_, 'tcx>,
492492
hir_id: HirId,
493493
) -> Result<(), Vec<super::Pat<'tcx>>> {
494-
let wild_pattern = super::Pat { ty, span: DUMMY_SP, kind: box PatKind::Wild };
494+
let wild_pattern = super::Pat::wildcard_from_ty(ty);
495495
match is_useful(cx, matrix, &PatStack::from_pattern(&wild_pattern), ConstructWitness, hir_id) {
496496
NotUseful => Ok(()), // This is good, wildcard pattern isn't reachable.
497497
UsefulWithWitness(pats) => Err(if pats.is_empty() {

src/librustc_mir/hair/pattern/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_index::vec::Idx;
2626
use std::cmp::Ordering;
2727
use std::fmt;
2828
use syntax::ast;
29-
use syntax_pos::Span;
29+
use syntax_pos::{Span, DUMMY_SP};
3030

3131
#[derive(Clone, Debug)]
3232
pub enum PatternError {
@@ -55,6 +55,11 @@ pub struct Pat<'tcx> {
5555
pub kind: Box<PatKind<'tcx>>,
5656
}
5757

58+
impl<'tcx> Pat<'tcx> {
59+
pub(crate) fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
60+
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
61+
}
62+
}
5863

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

0 commit comments

Comments
 (0)