2016-09-24 10:24:34 -05:00
|
|
|
|
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
use self::Constructor::*;
|
|
|
|
|
use self::Usefulness::*;
|
|
|
|
|
use self::WitnessPreference::*;
|
|
|
|
|
|
|
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-09-25 18:53:26 -05:00
|
|
|
|
use eval::{compare_const_vals};
|
|
|
|
|
|
2016-10-26 14:38:22 -05:00
|
|
|
|
use rustc_const_math::ConstInt;
|
|
|
|
|
|
2017-02-09 10:52:51 -06:00
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2016-09-25 18:53:26 -05:00
|
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
|
|
|
|
|
|
use pattern::{FieldPattern, Pattern, PatternKind};
|
|
|
|
|
use pattern::{PatternFoldable, PatternFolder};
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-11-25 05:21:19 -06:00
|
|
|
|
use rustc::hir::def_id::DefId;
|
2017-01-10 15:13:53 -06:00
|
|
|
|
use rustc::hir::RangeEnd;
|
2017-09-07 15:39:15 -05:00
|
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
use rustc::mir::Field;
|
2016-09-24 10:24:34 -05:00
|
|
|
|
use rustc::util::common::ErrorReported;
|
|
|
|
|
|
|
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
use arena::TypedArena;
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-11-05 06:32:35 -05:00
|
|
|
|
use std::cmp::{self, Ordering};
|
2016-09-25 18:53:26 -05:00
|
|
|
|
use std::fmt;
|
|
|
|
|
use std::iter::{FromIterator, IntoIterator, repeat};
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-10-03 13:39:21 -05:00
|
|
|
|
pub fn expand_pattern<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>, pat: Pattern<'tcx>)
|
|
|
|
|
-> &'a Pattern<'tcx>
|
2016-09-25 18:53:26 -05:00
|
|
|
|
{
|
2016-10-03 13:39:21 -05:00
|
|
|
|
cx.pattern_arena.alloc(LiteralExpander.fold_pattern(&pat))
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
struct LiteralExpander;
|
|
|
|
|
impl<'tcx> PatternFolder<'tcx> for LiteralExpander {
|
|
|
|
|
fn fold_pattern(&mut self, pat: &Pattern<'tcx>) -> Pattern<'tcx> {
|
|
|
|
|
match (&pat.ty.sty, &*pat.kind) {
|
|
|
|
|
(&ty::TyRef(_, mt), &PatternKind::Constant { ref value }) => {
|
|
|
|
|
Pattern {
|
|
|
|
|
ty: pat.ty,
|
|
|
|
|
span: pat.span,
|
|
|
|
|
kind: box PatternKind::Deref {
|
|
|
|
|
subpattern: Pattern {
|
|
|
|
|
ty: mt.ty,
|
|
|
|
|
span: pat.span,
|
|
|
|
|
kind: box PatternKind::Constant { value: value.clone() },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(_, &PatternKind::Binding { subpattern: Some(ref s), .. }) => {
|
|
|
|
|
s.fold_with(self)
|
|
|
|
|
}
|
|
|
|
|
_ => pat.super_fold_with(self)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
impl<'tcx> Pattern<'tcx> {
|
|
|
|
|
fn is_wildcard(&self) -> bool {
|
|
|
|
|
match *self.kind {
|
|
|
|
|
PatternKind::Binding { subpattern: None, .. } | PatternKind::Wild =>
|
|
|
|
|
true,
|
|
|
|
|
_ => false
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
pub struct Matrix<'a, 'tcx: 'a>(Vec<Vec<&'a Pattern<'tcx>>>);
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Matrix<'a, 'tcx> {
|
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
|
Matrix(vec![])
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
pub fn push(&mut self, row: Vec<&'a Pattern<'tcx>>) {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
self.0.push(row)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pretty-printer for matrices of patterns, example:
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
/// + _ + [] +
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
/// + true + [First] +
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
/// + true + [Second(true)] +
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
/// + false + [_] +
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
/// + _ + [_, _, ..tail] +
|
|
|
|
|
/// ++++++++++++++++++++++++++
|
|
|
|
|
impl<'a, 'tcx> fmt::Debug for Matrix<'a, 'tcx> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(f, "\n")?;
|
|
|
|
|
|
|
|
|
|
let &Matrix(ref m) = self;
|
|
|
|
|
let pretty_printed_matrix: Vec<Vec<String>> = m.iter().map(|row| {
|
|
|
|
|
row.iter().map(|pat| format!("{:?}", pat)).collect()
|
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
|
|
let column_count = m.iter().map(|row| row.len()).max().unwrap_or(0);
|
|
|
|
|
assert!(m.iter().all(|row| row.len() == column_count));
|
|
|
|
|
let column_widths: Vec<usize> = (0..column_count).map(|col| {
|
|
|
|
|
pretty_printed_matrix.iter().map(|row| row[col].len()).max().unwrap_or(0)
|
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
|
|
let total_width = column_widths.iter().cloned().sum::<usize>() + column_count * 3 + 1;
|
|
|
|
|
let br = repeat('+').take(total_width).collect::<String>();
|
|
|
|
|
write!(f, "{}\n", br)?;
|
|
|
|
|
for row in pretty_printed_matrix {
|
|
|
|
|
write!(f, "+")?;
|
|
|
|
|
for (column, pat_str) in row.into_iter().enumerate() {
|
|
|
|
|
write!(f, " ")?;
|
|
|
|
|
write!(f, "{:1$}", pat_str, column_widths[column])?;
|
|
|
|
|
write!(f, " +")?;
|
|
|
|
|
}
|
|
|
|
|
write!(f, "\n")?;
|
|
|
|
|
write!(f, "{}\n", br)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
impl<'a, 'tcx> FromIterator<Vec<&'a Pattern<'tcx>>> for Matrix<'a, 'tcx> {
|
|
|
|
|
fn from_iter<T: IntoIterator<Item=Vec<&'a Pattern<'tcx>>>>(iter: T) -> Self
|
2016-09-24 10:24:34 -05:00
|
|
|
|
{
|
|
|
|
|
Matrix(iter.into_iter().collect())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//NOTE: appears to be the only place other then InferCtxt to contain a ParamEnv
|
|
|
|
|
pub struct MatchCheckCtxt<'a, 'tcx: 'a> {
|
|
|
|
|
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-12-29 03:08:33 -06:00
|
|
|
|
/// The module in which the match occurs. This is necessary for
|
2016-11-29 01:10:26 -06:00
|
|
|
|
/// checking inhabited-ness of types because whether a type is (visibly)
|
|
|
|
|
/// inhabited can depend on whether it was defined in the current module or
|
2016-12-29 03:08:33 -06:00
|
|
|
|
/// not. eg. `struct Foo { _private: ! }` cannot be seen to be empty
|
|
|
|
|
/// outside it's module and should not be matchable with an empty match
|
|
|
|
|
/// statement.
|
|
|
|
|
pub module: DefId,
|
2016-09-25 18:53:26 -05:00
|
|
|
|
pub pattern_arena: &'a TypedArena<Pattern<'tcx>>,
|
2016-11-07 21:02:55 -06:00
|
|
|
|
pub byte_array_map: FxHashMap<*const Pattern<'tcx>, Vec<&'a Pattern<'tcx>>>,
|
2016-09-25 18:53:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
|
|
|
|
|
pub fn create_and_enter<F, R>(
|
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-12-29 03:08:33 -06:00
|
|
|
|
module: DefId,
|
2016-09-25 18:53:26 -05:00
|
|
|
|
f: F) -> R
|
|
|
|
|
where F: for<'b> FnOnce(MatchCheckCtxt<'b, 'tcx>) -> R
|
|
|
|
|
{
|
|
|
|
|
let pattern_arena = TypedArena::new();
|
|
|
|
|
|
|
|
|
|
f(MatchCheckCtxt {
|
2017-08-07 00:54:09 -05:00
|
|
|
|
tcx,
|
|
|
|
|
module,
|
2016-09-25 18:53:26 -05:00
|
|
|
|
pattern_arena: &pattern_arena,
|
2016-11-07 21:02:55 -06:00
|
|
|
|
byte_array_map: FxHashMap(),
|
2016-09-25 18:53:26 -05:00
|
|
|
|
})
|
|
|
|
|
}
|
2016-10-26 14:38:22 -05:00
|
|
|
|
|
|
|
|
|
// convert a byte-string pattern to a list of u8 patterns.
|
2016-11-30 11:12:03 -06:00
|
|
|
|
fn lower_byte_str_pattern<'p>(&mut self, pat: &'p Pattern<'tcx>) -> Vec<&'p Pattern<'tcx>>
|
|
|
|
|
where 'a: 'p
|
|
|
|
|
{
|
2016-10-26 14:38:22 -05:00
|
|
|
|
let pattern_arena = &*self.pattern_arena;
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
self.byte_array_map.entry(pat).or_insert_with(|| {
|
|
|
|
|
match pat.kind {
|
|
|
|
|
box PatternKind::Constant {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
value: &ty::Const { val: ConstVal::ByteStr(b), .. }
|
2016-10-26 14:38:22 -05:00
|
|
|
|
} => {
|
2017-08-03 16:41:44 -05:00
|
|
|
|
b.data.iter().map(|&b| &*pattern_arena.alloc(Pattern {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
ty: tcx.types.u8,
|
|
|
|
|
span: pat.span,
|
|
|
|
|
kind: box PatternKind::Constant {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
value: tcx.mk_const(ty::Const {
|
|
|
|
|
val: ConstVal::Integral(ConstInt::U8(b)),
|
|
|
|
|
ty: tcx.types.u8
|
|
|
|
|
})
|
2016-10-26 14:38:22 -05:00
|
|
|
|
}
|
|
|
|
|
})).collect()
|
|
|
|
|
}
|
|
|
|
|
_ => span_bug!(pat.span, "unexpected byte array pattern {:?}", pat)
|
|
|
|
|
}
|
|
|
|
|
}).clone()
|
|
|
|
|
}
|
2017-02-20 11:18:31 -06:00
|
|
|
|
|
|
|
|
|
fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
|
|
|
|
|
if self.tcx.sess.features.borrow().never_type {
|
2017-09-07 15:39:15 -05:00
|
|
|
|
self.tcx.is_ty_uninhabited_from(self.module, ty)
|
2017-02-20 11:18:31 -06:00
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 14:17:54 -05:00
|
|
|
|
fn is_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
|
|
|
|
|
match ty.sty {
|
|
|
|
|
ty::TyAdt(adt_def, ..) => adt_def.is_enum() && adt_def.is_non_exhaustive(),
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_local(&self, ty: Ty<'tcx>) -> bool {
|
|
|
|
|
match ty.sty {
|
|
|
|
|
ty::TyAdt(adt_def, ..) => adt_def.did.is_local(),
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-20 11:18:31 -06:00
|
|
|
|
fn is_variant_uninhabited(&self,
|
|
|
|
|
variant: &'tcx ty::VariantDef,
|
2017-09-07 15:39:15 -05:00
|
|
|
|
substs: &'tcx ty::subst::Substs<'tcx>)
|
|
|
|
|
-> bool
|
2017-02-20 11:18:31 -06:00
|
|
|
|
{
|
|
|
|
|
if self.tcx.sess.features.borrow().never_type {
|
2017-09-07 15:39:15 -05:00
|
|
|
|
self.tcx.is_enum_variant_uninhabited_from(self.module, variant, substs)
|
2017-02-20 11:18:31 -06:00
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2017-02-15 07:00:20 -06:00
|
|
|
|
pub enum Constructor<'tcx> {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
/// The constructor of all patterns that don't vary by constructor,
|
|
|
|
|
/// e.g. struct patterns and fixed-length arrays.
|
|
|
|
|
Single,
|
|
|
|
|
/// Enum variants.
|
|
|
|
|
Variant(DefId),
|
|
|
|
|
/// Literal values.
|
2017-08-04 03:25:13 -05:00
|
|
|
|
ConstantValue(&'tcx ty::Const<'tcx>),
|
2017-01-10 15:13:53 -06:00
|
|
|
|
/// Ranges of literal values (`2...5` and `2..5`).
|
2017-08-04 03:25:13 -05:00
|
|
|
|
ConstantRange(&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>, RangeEnd),
|
2016-09-24 10:24:34 -05:00
|
|
|
|
/// Array patterns of length n.
|
2017-08-05 04:27:28 -05:00
|
|
|
|
Slice(u64),
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 07:00:20 -06:00
|
|
|
|
impl<'tcx> Constructor<'tcx> {
|
2017-01-01 12:57:21 -06:00
|
|
|
|
fn variant_index_for_adt(&self, adt: &'tcx ty::AdtDef) -> usize {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
match self {
|
2017-01-01 12:57:21 -06:00
|
|
|
|
&Variant(vid) => adt.variant_index_with_id(vid),
|
2016-09-25 18:53:26 -05:00
|
|
|
|
&Single => {
|
2017-11-18 12:24:54 -06:00
|
|
|
|
assert!(!adt.is_enum());
|
2017-01-01 12:57:21 -06:00
|
|
|
|
0
|
2016-09-25 18:53:26 -05:00
|
|
|
|
}
|
|
|
|
|
_ => bug!("bad constructor {:?} for adt {:?}", self, adt)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub enum Usefulness<'tcx> {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
Useful,
|
2017-01-01 12:57:21 -06:00
|
|
|
|
UsefulWithWitness(Vec<Witness<'tcx>>),
|
2016-09-24 10:24:34 -05:00
|
|
|
|
NotUseful
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
impl<'tcx> Usefulness<'tcx> {
|
|
|
|
|
fn is_useful(&self) -> bool {
|
|
|
|
|
match *self {
|
|
|
|
|
NotUseful => false,
|
|
|
|
|
_ => true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-24 10:24:34 -05:00
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub enum WitnessPreference {
|
|
|
|
|
ConstructWitness,
|
|
|
|
|
LeaveOutWitness
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-24 12:45:59 -05:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
struct PatternContext<'tcx> {
|
|
|
|
|
ty: Ty<'tcx>,
|
2017-08-05 04:27:28 -05:00
|
|
|
|
max_slice_length: u64,
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A stack of patterns in reverse order of construction
|
2017-01-01 12:57:21 -06:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct Witness<'tcx>(Vec<Pattern<'tcx>>);
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
impl<'tcx> Witness<'tcx> {
|
|
|
|
|
pub fn single_pattern(&self) -> &Pattern<'tcx> {
|
2016-09-24 12:45:59 -05:00
|
|
|
|
assert_eq!(self.0.len(), 1);
|
|
|
|
|
&self.0[0]
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
fn push_wild_constructor<'a>(
|
2016-09-24 12:45:59 -05:00
|
|
|
|
mut self,
|
|
|
|
|
cx: &MatchCheckCtxt<'a, 'tcx>,
|
2017-02-15 07:00:20 -06:00
|
|
|
|
ctor: &Constructor<'tcx>,
|
2016-09-24 12:45:59 -05:00
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
|
-> Self
|
|
|
|
|
{
|
2017-01-03 21:07:32 -06:00
|
|
|
|
let sub_pattern_tys = constructor_sub_pattern_tys(cx, ctor, ty);
|
|
|
|
|
self.0.extend(sub_pattern_tys.into_iter().map(|ty| {
|
|
|
|
|
Pattern {
|
2017-08-07 00:54:09 -05:00
|
|
|
|
ty,
|
2017-01-03 21:07:32 -06:00
|
|
|
|
span: DUMMY_SP,
|
|
|
|
|
kind: box PatternKind::Wild,
|
|
|
|
|
}
|
|
|
|
|
}));
|
2016-09-24 12:45:59 -05:00
|
|
|
|
self.apply_constructor(cx, ctor, ty)
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
|
|
|
|
|
2016-09-24 12:45:59 -05:00
|
|
|
|
/// Constructs a partial witness for a pattern given a list of
|
|
|
|
|
/// patterns expanded by the specialization step.
|
|
|
|
|
///
|
|
|
|
|
/// When a pattern P is discovered to be useful, this function is used bottom-up
|
|
|
|
|
/// to reconstruct a complete witness, e.g. a pattern P' that covers a subset
|
|
|
|
|
/// of values, V, where each value in that set is not covered by any previously
|
|
|
|
|
/// used patterns and is covered by the pattern P'. Examples:
|
|
|
|
|
///
|
|
|
|
|
/// left_ty: tuple of 3 elements
|
|
|
|
|
/// pats: [10, 20, _] => (10, 20, _)
|
|
|
|
|
///
|
|
|
|
|
/// left_ty: struct X { a: (bool, &'static str), b: usize}
|
|
|
|
|
/// pats: [(false, "foo"), 42] => X { a: (false, "foo"), b: 42 }
|
2017-01-01 12:57:21 -06:00
|
|
|
|
fn apply_constructor<'a>(
|
2016-09-24 12:45:59 -05:00
|
|
|
|
mut self,
|
|
|
|
|
cx: &MatchCheckCtxt<'a,'tcx>,
|
2017-02-15 07:00:20 -06:00
|
|
|
|
ctor: &Constructor<'tcx>,
|
2016-09-24 12:45:59 -05:00
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
|
-> Self
|
|
|
|
|
{
|
|
|
|
|
let arity = constructor_arity(cx, ctor, ty);
|
|
|
|
|
let pat = {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
let len = self.0.len() as u64;
|
|
|
|
|
let mut pats = self.0.drain((len-arity) as usize..).rev();
|
2016-09-24 12:45:59 -05:00
|
|
|
|
|
|
|
|
|
match ty.sty {
|
2017-01-01 12:57:21 -06:00
|
|
|
|
ty::TyAdt(..) |
|
|
|
|
|
ty::TyTuple(..) => {
|
|
|
|
|
let pats = pats.enumerate().map(|(i, p)| {
|
|
|
|
|
FieldPattern {
|
|
|
|
|
field: Field::new(i),
|
|
|
|
|
pattern: p
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
2017-01-01 12:57:21 -06:00
|
|
|
|
}).collect();
|
|
|
|
|
|
2017-01-03 21:07:32 -06:00
|
|
|
|
if let ty::TyAdt(adt, substs) = ty.sty {
|
2017-11-18 12:24:54 -06:00
|
|
|
|
if adt.is_enum() {
|
2017-01-01 12:57:21 -06:00
|
|
|
|
PatternKind::Variant {
|
|
|
|
|
adt_def: adt,
|
2017-08-07 00:54:09 -05:00
|
|
|
|
substs,
|
2017-01-01 12:57:21 -06:00
|
|
|
|
variant_index: ctor.variant_index_for_adt(adt),
|
|
|
|
|
subpatterns: pats
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
PatternKind::Leaf { subpatterns: pats }
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
2017-01-01 12:57:21 -06:00
|
|
|
|
} else {
|
|
|
|
|
PatternKind::Leaf { subpatterns: pats }
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
ty::TyRef(..) => {
|
|
|
|
|
PatternKind::Deref { subpattern: pats.nth(0).unwrap() }
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ty::TySlice(_) | ty::TyArray(..) => {
|
2017-01-01 12:57:21 -06:00
|
|
|
|
PatternKind::Slice {
|
|
|
|
|
prefix: pats.collect(),
|
|
|
|
|
slice: None,
|
|
|
|
|
suffix: vec![]
|
|
|
|
|
}
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-09-24 12:45:59 -05:00
|
|
|
|
_ => {
|
|
|
|
|
match *ctor {
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ConstantValue(value) => PatternKind::Constant { value },
|
2017-01-01 12:57:21 -06:00
|
|
|
|
_ => PatternKind::Wild,
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
2016-09-24 12:45:59 -05:00
|
|
|
|
};
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2017-01-01 12:57:21 -06:00
|
|
|
|
self.0.push(Pattern {
|
2017-08-07 00:54:09 -05:00
|
|
|
|
ty,
|
2017-01-01 12:57:21 -06:00
|
|
|
|
span: DUMMY_SP,
|
|
|
|
|
kind: Box::new(pat),
|
|
|
|
|
});
|
2016-09-24 12:45:59 -05:00
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This determines the set of all possible constructors of a pattern matching
|
|
|
|
|
/// values of type `left_ty`. For vectors, this would normally be an infinite set
|
2016-12-30 09:51:32 -06:00
|
|
|
|
/// but is instead bounded by the maximum fixed length of slice patterns in
|
|
|
|
|
/// the column of patterns being analyzed.
|
2016-10-26 14:38:22 -05:00
|
|
|
|
///
|
|
|
|
|
/// This intentionally does not list ConstantValue specializations for
|
|
|
|
|
/// non-booleans, because we currently assume that there is always a
|
|
|
|
|
/// "non-standard constant" that matches. See issue #12483.
|
|
|
|
|
///
|
2016-11-29 01:10:26 -06:00
|
|
|
|
/// We make sure to omit constructors that are statically impossible. eg for
|
|
|
|
|
/// Option<!> we do not include Some(_) in the returned list of constructors.
|
|
|
|
|
fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
2017-02-15 07:00:20 -06:00
|
|
|
|
pcx: PatternContext<'tcx>)
|
|
|
|
|
-> Vec<Constructor<'tcx>>
|
2016-11-29 01:10:26 -06:00
|
|
|
|
{
|
2016-11-30 11:12:03 -06:00
|
|
|
|
debug!("all_constructors({:?})", pcx.ty);
|
2016-09-24 12:45:59 -05:00
|
|
|
|
match pcx.ty.sty {
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ty::TyBool => {
|
|
|
|
|
[true, false].iter().map(|&b| {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
ConstantValue(cx.tcx.mk_const(ty::Const {
|
|
|
|
|
val: ConstVal::Bool(b),
|
|
|
|
|
ty: cx.tcx.types.bool
|
|
|
|
|
}))
|
2017-08-03 16:41:44 -05:00
|
|
|
|
}).collect()
|
|
|
|
|
}
|
2017-08-07 00:08:53 -05:00
|
|
|
|
ty::TyArray(ref sub_ty, len) if len.val.to_const_int().is_some() => {
|
2017-08-05 08:11:24 -05:00
|
|
|
|
let len = len.val.to_const_int().unwrap().to_u64().unwrap();
|
|
|
|
|
if len != 0 && cx.is_uninhabited(sub_ty) {
|
2016-11-29 01:10:26 -06:00
|
|
|
|
vec![]
|
2017-02-20 11:18:31 -06:00
|
|
|
|
} else {
|
2017-08-05 08:11:24 -05:00
|
|
|
|
vec![Slice(len)]
|
2016-11-29 01:10:26 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-07 00:08:53 -05:00
|
|
|
|
// Treat arrays of a constant but unknown length like slices.
|
|
|
|
|
ty::TyArray(ref sub_ty, _) |
|
|
|
|
|
ty::TySlice(ref sub_ty) => {
|
|
|
|
|
if cx.is_uninhabited(sub_ty) {
|
|
|
|
|
vec![Slice(0)]
|
|
|
|
|
} else {
|
|
|
|
|
(0..pcx.max_slice_length+1).map(|length| Slice(length)).collect()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-18 12:24:54 -06:00
|
|
|
|
ty::TyAdt(def, substs) if def.is_enum() => {
|
2017-02-20 11:18:31 -06:00
|
|
|
|
def.variants.iter()
|
|
|
|
|
.filter(|v| !cx.is_variant_uninhabited(v, substs))
|
|
|
|
|
.map(|v| Variant(v.did))
|
|
|
|
|
.collect()
|
2016-11-29 01:10:26 -06:00
|
|
|
|
}
|
|
|
|
|
_ => {
|
2017-02-20 11:18:31 -06:00
|
|
|
|
if cx.is_uninhabited(pcx.ty) {
|
2016-11-29 01:10:26 -06:00
|
|
|
|
vec![]
|
|
|
|
|
} else {
|
|
|
|
|
vec![Single]
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
|
2016-11-05 06:32:35 -05:00
|
|
|
|
_cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
2017-08-05 04:27:28 -05:00
|
|
|
|
patterns: I) -> u64
|
2016-11-30 11:12:03 -06:00
|
|
|
|
where I: Iterator<Item=&'p Pattern<'tcx>>
|
2016-11-05 06:32:35 -05:00
|
|
|
|
{
|
|
|
|
|
// The exhaustiveness-checking paper does not include any details on
|
|
|
|
|
// checking variable-length slice patterns. However, they are matched
|
|
|
|
|
// by an infinite collection of fixed-length array patterns.
|
|
|
|
|
//
|
2016-11-08 14:55:57 -06:00
|
|
|
|
// Checking the infinite set directly would take an infinite amount
|
|
|
|
|
// of time. However, it turns out that for each finite set of
|
|
|
|
|
// patterns `P`, all sufficiently large array lengths are equivalent:
|
2016-11-05 06:32:35 -05:00
|
|
|
|
//
|
2016-11-08 14:55:57 -06:00
|
|
|
|
// Each slice `s` with a "sufficiently-large" length `l ≥ L` that applies
|
|
|
|
|
// to exactly the subset `Pₜ` of `P` can be transformed to a slice
|
|
|
|
|
// `sₘ` for each sufficiently-large length `m` that applies to exactly
|
|
|
|
|
// the same subset of `P`.
|
2016-11-05 06:32:35 -05:00
|
|
|
|
//
|
2016-11-08 14:55:57 -06:00
|
|
|
|
// Because of that, each witness for reachability-checking from one
|
|
|
|
|
// of the sufficiently-large lengths can be transformed to an
|
|
|
|
|
// equally-valid witness from any other length, so we only have
|
|
|
|
|
// to check slice lengths from the "minimal sufficiently-large length"
|
|
|
|
|
// and below.
|
|
|
|
|
//
|
|
|
|
|
// Note that the fact that there is a *single* `sₘ` for each `m`
|
|
|
|
|
// not depending on the specific pattern in `P` is important: if
|
|
|
|
|
// you look at the pair of patterns
|
|
|
|
|
// `[true, ..]`
|
|
|
|
|
// `[.., false]`
|
|
|
|
|
// Then any slice of length ≥1 that matches one of these two
|
|
|
|
|
// patterns can be be trivially turned to a slice of any
|
|
|
|
|
// other length ≥1 that matches them and vice-versa - for
|
|
|
|
|
// but the slice from length 2 `[false, true]` that matches neither
|
|
|
|
|
// of these patterns can't be turned to a slice from length 1 that
|
|
|
|
|
// matches neither of these patterns, so we have to consider
|
|
|
|
|
// slices from length 2 there.
|
|
|
|
|
//
|
|
|
|
|
// Now, to see that that length exists and find it, observe that slice
|
|
|
|
|
// patterns are either "fixed-length" patterns (`[_, _, _]`) or
|
|
|
|
|
// "variable-length" patterns (`[_, .., _]`).
|
|
|
|
|
//
|
|
|
|
|
// For fixed-length patterns, all slices with lengths *longer* than
|
|
|
|
|
// the pattern's length have the same outcome (of not matching), so
|
|
|
|
|
// as long as `L` is greater than the pattern's length we can pick
|
|
|
|
|
// any `sₘ` from that length and get the same result.
|
|
|
|
|
//
|
|
|
|
|
// For variable-length patterns, the situation is more complicated,
|
|
|
|
|
// because as seen above the precise value of `sₘ` matters.
|
|
|
|
|
//
|
|
|
|
|
// However, for each variable-length pattern `p` with a prefix of length
|
|
|
|
|
// `plₚ` and suffix of length `slₚ`, only the first `plₚ` and the last
|
|
|
|
|
// `slₚ` elements are examined.
|
|
|
|
|
//
|
|
|
|
|
// Therefore, as long as `L` is positive (to avoid concerns about empty
|
|
|
|
|
// types), all elements after the maximum prefix length and before
|
|
|
|
|
// the maximum suffix length are not examined by any variable-length
|
|
|
|
|
// pattern, and therefore can be added/removed without affecting
|
|
|
|
|
// them - creating equivalent patterns from any sufficiently-large
|
|
|
|
|
// length.
|
|
|
|
|
//
|
|
|
|
|
// Of course, if fixed-length patterns exist, we must be sure
|
|
|
|
|
// that our length is large enough to miss them all, so
|
|
|
|
|
// we can pick `L = max(FIXED_LEN+1 ∪ {max(PREFIX_LEN) + max(SUFFIX_LEN)})`
|
|
|
|
|
//
|
|
|
|
|
// for example, with the above pair of patterns, all elements
|
|
|
|
|
// but the first and last can be added/removed, so any
|
|
|
|
|
// witness of length ≥2 (say, `[false, false, true]`) can be
|
|
|
|
|
// turned to a witness from any other length ≥2.
|
2016-11-05 06:32:35 -05:00
|
|
|
|
|
|
|
|
|
let mut max_prefix_len = 0;
|
|
|
|
|
let mut max_suffix_len = 0;
|
|
|
|
|
let mut max_fixed_len = 0;
|
|
|
|
|
|
|
|
|
|
for row in patterns {
|
2016-11-08 14:55:57 -06:00
|
|
|
|
match *row.kind {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
PatternKind::Constant { value: &ty::Const { val: ConstVal::ByteStr(b), .. } } => {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
max_fixed_len = cmp::max(max_fixed_len, b.data.len() as u64);
|
2016-11-05 06:32:35 -05:00
|
|
|
|
}
|
|
|
|
|
PatternKind::Slice { ref prefix, slice: None, ref suffix } => {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
let fixed_len = prefix.len() as u64 + suffix.len() as u64;
|
2016-11-05 06:32:35 -05:00
|
|
|
|
max_fixed_len = cmp::max(max_fixed_len, fixed_len);
|
|
|
|
|
}
|
|
|
|
|
PatternKind::Slice { ref prefix, slice: Some(_), ref suffix } => {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
max_prefix_len = cmp::max(max_prefix_len, prefix.len() as u64);
|
|
|
|
|
max_suffix_len = cmp::max(max_suffix_len, suffix.len() as u64);
|
2016-11-05 06:32:35 -05:00
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmp::max(max_fixed_len + 1, max_prefix_len + max_suffix_len)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
/// Algorithm from http://moscova.inria.fr/~maranget/papers/warn/index.html
|
2016-11-30 11:12:03 -06:00
|
|
|
|
/// The algorithm from the paper has been modified to correctly handle empty
|
|
|
|
|
/// types. The changes are:
|
|
|
|
|
/// (0) We don't exit early if the pattern matrix has zero rows. We just
|
|
|
|
|
/// continue to recurse over columns.
|
|
|
|
|
/// (1) all_constructors will only return constructors that are statically
|
|
|
|
|
/// possible. eg. it will only return Ok for Result<T, !>
|
2016-09-25 18:53:26 -05:00
|
|
|
|
///
|
2017-12-25 10:14:50 -06:00
|
|
|
|
/// This finds whether a (row) vector `v` of patterns is 'useful' in relation
|
2018-01-13 15:41:11 -06:00
|
|
|
|
/// to a set of such vectors `m` - this is defined as there being a set of
|
|
|
|
|
/// inputs that will match `v` but not any of the sets in `m`.
|
2017-12-25 10:14:50 -06:00
|
|
|
|
///
|
|
|
|
|
/// All the patterns at each column of the `matrix ++ v` matrix must
|
|
|
|
|
/// have the same type, except that wildcard (PatternKind::Wild) patterns
|
|
|
|
|
/// with type TyErr are also allowed, even if the "type of the column"
|
|
|
|
|
/// is not TyErr. That is used to represent private fields, as using their
|
|
|
|
|
/// real type would assert that they are inhabited.
|
2016-09-25 18:53:26 -05:00
|
|
|
|
///
|
|
|
|
|
/// This is used both for reachability checking (if a pattern isn't useful in
|
|
|
|
|
/// relation to preceding patterns, it is not reachable) and exhaustiveness
|
|
|
|
|
/// checking (if a wildcard pattern is useful in relation to a matrix, the
|
|
|
|
|
/// matrix isn't exhaustive).
|
2016-11-30 11:12:03 -06:00
|
|
|
|
pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
2017-12-25 10:14:50 -06:00
|
|
|
|
matrix: &Matrix<'p, 'tcx>,
|
|
|
|
|
v: &[&'p Pattern<'tcx>],
|
|
|
|
|
witness: WitnessPreference)
|
|
|
|
|
-> Usefulness<'tcx> {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
let &Matrix(ref rows) = matrix;
|
|
|
|
|
debug!("is_useful({:?}, {:?})", matrix, v);
|
2016-09-24 12:45:59 -05:00
|
|
|
|
|
2016-11-28 04:38:27 -06:00
|
|
|
|
// The base case. We are pattern-matching on () and the return value is
|
|
|
|
|
// based on whether our matrix has a row or not.
|
|
|
|
|
// NOTE: This could potentially be optimized by checking rows.is_empty()
|
|
|
|
|
// first and then, if v is non-empty, the return value is based on whether
|
|
|
|
|
// the type of the tuple we're checking is inhabited or not.
|
|
|
|
|
if v.is_empty() {
|
|
|
|
|
return if rows.is_empty() {
|
|
|
|
|
match witness {
|
|
|
|
|
ConstructWitness => UsefulWithWitness(vec![Witness(vec![])]),
|
|
|
|
|
LeaveOutWitness => Useful,
|
|
|
|
|
}
|
2016-11-30 21:56:55 -06:00
|
|
|
|
} else {
|
2016-11-28 04:38:27 -06:00
|
|
|
|
NotUseful
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-26 14:38:22 -05:00
|
|
|
|
assert!(rows.iter().all(|r| r.len() == v.len()));
|
2016-11-05 06:32:35 -05:00
|
|
|
|
|
2016-09-24 12:45:59 -05:00
|
|
|
|
let pcx = PatternContext {
|
2018-01-13 15:41:11 -06:00
|
|
|
|
// TyErr is used to represent the type of wildcard patterns matching
|
|
|
|
|
// against inaccessible (private) fields of structs, so that we won't
|
|
|
|
|
// be able to observe whether the types of the struct's fields are
|
|
|
|
|
// inhabited.
|
|
|
|
|
//
|
|
|
|
|
// If the field is truely inaccessible, then all the patterns
|
|
|
|
|
// matching against it must be wildcard patterns, so its type
|
|
|
|
|
// does not matter.
|
|
|
|
|
//
|
|
|
|
|
// However, if we are matching against non-wildcard patterns, we
|
|
|
|
|
// need to know the real type of the field so we can specialize
|
|
|
|
|
// against it. This primarily occurs through constants - they
|
|
|
|
|
// can include contents for fields that are inaccessible at the
|
|
|
|
|
// location of the match. In that case, the field's type is
|
|
|
|
|
// inhabited - by the constant - so we can just use it.
|
|
|
|
|
//
|
|
|
|
|
// FIXME: this might lead to "unstable" behavior with macro hygiene
|
|
|
|
|
// introducing uninhabited patterns for inaccessible fields. We
|
|
|
|
|
// need to figure out how to model that.
|
2016-09-25 18:53:26 -05:00
|
|
|
|
ty: rows.iter().map(|r| r[0].ty).find(|ty| !ty.references_error())
|
|
|
|
|
.unwrap_or(v[0].ty),
|
2016-11-08 14:55:57 -06:00
|
|
|
|
max_slice_length: max_slice_length(cx, rows.iter().map(|r| r[0]).chain(Some(v[0])))
|
2016-09-24 10:24:34 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-10-26 14:38:22 -05:00
|
|
|
|
debug!("is_useful_expand_first_col: pcx={:?}, expanding {:?}", pcx, v[0]);
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-09-24 12:45:59 -05:00
|
|
|
|
if let Some(constructors) = pat_constructors(cx, v[0], pcx) {
|
|
|
|
|
debug!("is_useful - expanding constructors: {:?}", constructors);
|
|
|
|
|
constructors.into_iter().map(|c|
|
|
|
|
|
is_useful_specialized(cx, matrix, v, c.clone(), pcx.ty, witness)
|
2017-01-01 12:57:21 -06:00
|
|
|
|
).find(|result| result.is_useful()).unwrap_or(NotUseful)
|
2016-09-24 12:45:59 -05:00
|
|
|
|
} else {
|
|
|
|
|
debug!("is_useful - expanding wildcard");
|
2016-11-30 21:37:03 -06:00
|
|
|
|
|
|
|
|
|
let used_ctors: Vec<Constructor> = rows.iter().flat_map(|row| {
|
|
|
|
|
pat_constructors(cx, row[0], pcx).unwrap_or(vec![])
|
|
|
|
|
}).collect();
|
|
|
|
|
debug!("used_ctors = {:?}", used_ctors);
|
|
|
|
|
let all_ctors = all_constructors(cx, pcx);
|
|
|
|
|
debug!("all_ctors = {:?}", all_ctors);
|
|
|
|
|
let missing_ctors: Vec<Constructor> = all_ctors.iter().filter(|c| {
|
|
|
|
|
!used_ctors.contains(*c)
|
|
|
|
|
}).cloned().collect();
|
|
|
|
|
|
|
|
|
|
// `missing_ctors` is the set of constructors from the same type as the
|
|
|
|
|
// first column of `matrix` that are matched only by wildcard patterns
|
|
|
|
|
// from the first column.
|
|
|
|
|
//
|
|
|
|
|
// Therefore, if there is some pattern that is unmatched by `matrix`,
|
|
|
|
|
// it will still be unmatched if the first constructor is replaced by
|
|
|
|
|
// any of the constructors in `missing_ctors`
|
2017-02-20 11:18:31 -06:00
|
|
|
|
//
|
|
|
|
|
// However, if our scrutinee is *privately* an empty enum, we
|
|
|
|
|
// must treat it as though it had an "unknown" constructor (in
|
|
|
|
|
// that case, all other patterns obviously can't be variants)
|
|
|
|
|
// to avoid exposing its emptyness. See the `match_privately_empty`
|
|
|
|
|
// test for details.
|
|
|
|
|
//
|
|
|
|
|
// FIXME: currently the only way I know of something can
|
|
|
|
|
// be a privately-empty enum is when the never_type
|
|
|
|
|
// feature flag is not present, so this is only
|
|
|
|
|
// needed for that case.
|
|
|
|
|
|
|
|
|
|
let is_privately_empty =
|
|
|
|
|
all_ctors.is_empty() && !cx.is_uninhabited(pcx.ty);
|
2017-11-03 14:17:54 -05:00
|
|
|
|
let is_declared_nonexhaustive =
|
|
|
|
|
cx.is_non_exhaustive_enum(pcx.ty) && !cx.is_local(pcx.ty);
|
|
|
|
|
debug!("missing_ctors={:?} is_privately_empty={:?} is_declared_nonexhaustive={:?}",
|
|
|
|
|
missing_ctors, is_privately_empty, is_declared_nonexhaustive);
|
|
|
|
|
|
|
|
|
|
// For privately empty and non-exhaustive enums, we work as if there were an "extra"
|
|
|
|
|
// `_` constructor for the type, so we can never match over all constructors.
|
|
|
|
|
let is_non_exhaustive = is_privately_empty || is_declared_nonexhaustive;
|
|
|
|
|
|
|
|
|
|
if missing_ctors.is_empty() && !is_non_exhaustive {
|
2016-11-30 21:37:03 -06:00
|
|
|
|
all_ctors.into_iter().map(|c| {
|
2016-09-24 12:45:59 -05:00
|
|
|
|
is_useful_specialized(cx, matrix, v, c.clone(), pcx.ty, witness)
|
2017-01-01 12:57:21 -06:00
|
|
|
|
}).find(|result| result.is_useful()).unwrap_or(NotUseful)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
} else {
|
|
|
|
|
let matrix = rows.iter().filter_map(|r| {
|
2016-09-25 18:53:26 -05:00
|
|
|
|
if r[0].is_wildcard() {
|
|
|
|
|
Some(r[1..].to_vec())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}).collect();
|
|
|
|
|
match is_useful(cx, &matrix, &v[1..], witness) {
|
|
|
|
|
UsefulWithWitness(pats) => {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
let cx = &*cx;
|
2017-11-03 14:17:54 -05:00
|
|
|
|
// In this case, there's at least one "free"
|
|
|
|
|
// constructor that is only matched against by
|
|
|
|
|
// wildcard patterns.
|
|
|
|
|
//
|
|
|
|
|
// There are 2 ways we can report a witness here.
|
|
|
|
|
// Commonly, we can report all the "free"
|
|
|
|
|
// constructors as witnesses, e.g. if we have:
|
|
|
|
|
//
|
|
|
|
|
// ```
|
|
|
|
|
// enum Direction { N, S, E, W }
|
|
|
|
|
// let Direction::N = ...;
|
|
|
|
|
// ```
|
|
|
|
|
//
|
|
|
|
|
// we can report 3 witnesses: `S`, `E`, and `W`.
|
|
|
|
|
//
|
|
|
|
|
// However, there are 2 cases where we don't want
|
|
|
|
|
// to do this and instead report a single `_` witness:
|
|
|
|
|
//
|
|
|
|
|
// 1) If the user is matching against a non-exhaustive
|
|
|
|
|
// enum, there is no point in enumerating all possible
|
|
|
|
|
// variants, because the user can't actually match
|
|
|
|
|
// against them himself, e.g. in an example like:
|
|
|
|
|
// ```
|
|
|
|
|
// let err: io::ErrorKind = ...;
|
|
|
|
|
// match err {
|
|
|
|
|
// io::ErrorKind::NotFound => {},
|
|
|
|
|
// }
|
|
|
|
|
// ```
|
|
|
|
|
// we don't want to show every possible IO error,
|
|
|
|
|
// but instead have `_` as the witness (this is
|
|
|
|
|
// actually *required* if the user specified *all*
|
|
|
|
|
// IO errors, but is probably what we want in every
|
|
|
|
|
// case).
|
|
|
|
|
//
|
|
|
|
|
// 2) If the user didn't actually specify a constructor
|
|
|
|
|
// in this arm, e.g. in
|
|
|
|
|
// ```
|
|
|
|
|
// let x: (Direction, Direction, bool) = ...;
|
|
|
|
|
// let (_, _, false) = x;
|
|
|
|
|
// ```
|
|
|
|
|
// we don't want to show all 16 possible witnesses
|
|
|
|
|
// `(<direction-1>, <direction-2>, true)` - we are
|
|
|
|
|
// satisfied with `(_, _, true)`. In this case,
|
|
|
|
|
// `used_ctors` is empty.
|
|
|
|
|
let new_witnesses = if is_non_exhaustive || used_ctors.is_empty() {
|
2016-11-30 21:37:03 -06:00
|
|
|
|
// All constructors are unused. Add wild patterns
|
|
|
|
|
// rather than each individual constructor
|
|
|
|
|
pats.into_iter().map(|mut witness| {
|
2017-01-03 21:07:32 -06:00
|
|
|
|
witness.0.push(Pattern {
|
|
|
|
|
ty: pcx.ty,
|
2016-11-30 21:37:03 -06:00
|
|
|
|
span: DUMMY_SP,
|
2017-01-03 21:07:32 -06:00
|
|
|
|
kind: box PatternKind::Wild,
|
|
|
|
|
});
|
2016-11-30 21:37:03 -06:00
|
|
|
|
witness
|
|
|
|
|
}).collect()
|
|
|
|
|
} else {
|
|
|
|
|
pats.into_iter().flat_map(|witness| {
|
|
|
|
|
missing_ctors.iter().map(move |ctor| {
|
|
|
|
|
witness.clone().push_wild_constructor(cx, ctor, pcx.ty)
|
|
|
|
|
})
|
|
|
|
|
}).collect()
|
|
|
|
|
};
|
|
|
|
|
UsefulWithWitness(new_witnesses)
|
2016-09-24 12:45:59 -05:00
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
result => result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
fn is_useful_specialized<'p, 'a:'p, 'tcx: 'a>(
|
2016-10-26 14:38:22 -05:00
|
|
|
|
cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
2016-11-30 11:12:03 -06:00
|
|
|
|
&Matrix(ref m): &Matrix<'p, 'tcx>,
|
|
|
|
|
v: &[&'p Pattern<'tcx>],
|
2017-02-15 07:00:20 -06:00
|
|
|
|
ctor: Constructor<'tcx>,
|
2016-09-24 10:24:34 -05:00
|
|
|
|
lty: Ty<'tcx>,
|
2017-01-01 12:57:21 -06:00
|
|
|
|
witness: WitnessPreference) -> Usefulness<'tcx>
|
2016-09-24 10:24:34 -05:00
|
|
|
|
{
|
2017-02-20 11:18:31 -06:00
|
|
|
|
debug!("is_useful_specialized({:?}, {:?}, {:?})", v, ctor, lty);
|
2016-11-30 11:12:03 -06:00
|
|
|
|
let sub_pat_tys = constructor_sub_pattern_tys(cx, &ctor, lty);
|
|
|
|
|
let wild_patterns_owned: Vec<_> = sub_pat_tys.iter().map(|ty| {
|
|
|
|
|
Pattern {
|
2017-08-07 00:54:09 -05:00
|
|
|
|
ty,
|
2016-11-30 11:12:03 -06:00
|
|
|
|
span: DUMMY_SP,
|
|
|
|
|
kind: box PatternKind::Wild,
|
|
|
|
|
}
|
|
|
|
|
}).collect();
|
|
|
|
|
let wild_patterns: Vec<_> = wild_patterns_owned.iter().collect();
|
2016-10-26 14:38:22 -05:00
|
|
|
|
let matrix = Matrix(m.iter().flat_map(|r| {
|
2017-03-24 03:31:26 -05:00
|
|
|
|
specialize(cx, &r, &ctor, &wild_patterns)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}).collect());
|
2016-11-30 11:12:03 -06:00
|
|
|
|
match specialize(cx, v, &ctor, &wild_patterns) {
|
2017-03-24 03:31:26 -05:00
|
|
|
|
Some(v) => match is_useful(cx, &matrix, &v, witness) {
|
2016-09-24 12:45:59 -05:00
|
|
|
|
UsefulWithWitness(witnesses) => UsefulWithWitness(
|
|
|
|
|
witnesses.into_iter()
|
|
|
|
|
.map(|witness| witness.apply_constructor(cx, &ctor, lty))
|
|
|
|
|
.collect()
|
|
|
|
|
),
|
|
|
|
|
result => result
|
|
|
|
|
},
|
2016-09-24 10:24:34 -05:00
|
|
|
|
None => NotUseful
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determines the constructors that the given pattern can be specialized to.
|
|
|
|
|
///
|
|
|
|
|
/// In most cases, there's only one constructor that a specific pattern
|
|
|
|
|
/// represents, such as a specific enum variant or a specific literal value.
|
|
|
|
|
/// Slice patterns, however, can match slices of different lengths. For instance,
|
|
|
|
|
/// `[a, b, ..tail]` can match a slice of length 2, 3, 4 and so on.
|
|
|
|
|
///
|
2016-09-24 12:45:59 -05:00
|
|
|
|
/// Returns None in case of a catch-all, which can't be specialized.
|
2017-02-15 07:00:20 -06:00
|
|
|
|
fn pat_constructors<'tcx>(_cx: &mut MatchCheckCtxt,
|
|
|
|
|
pat: &Pattern<'tcx>,
|
|
|
|
|
pcx: PatternContext)
|
|
|
|
|
-> Option<Vec<Constructor<'tcx>>>
|
2016-09-24 12:45:59 -05:00
|
|
|
|
{
|
2016-09-25 18:53:26 -05:00
|
|
|
|
match *pat.kind {
|
|
|
|
|
PatternKind::Binding { .. } | PatternKind::Wild =>
|
2016-09-24 12:45:59 -05:00
|
|
|
|
None,
|
2016-10-26 14:38:22 -05:00
|
|
|
|
PatternKind::Leaf { .. } | PatternKind::Deref { .. } =>
|
2016-09-25 18:53:26 -05:00
|
|
|
|
Some(vec![Single]),
|
|
|
|
|
PatternKind::Variant { adt_def, variant_index, .. } =>
|
|
|
|
|
Some(vec![Variant(adt_def.variants[variant_index].did)]),
|
2017-08-03 16:41:44 -05:00
|
|
|
|
PatternKind::Constant { value } =>
|
|
|
|
|
Some(vec![ConstantValue(value)]),
|
|
|
|
|
PatternKind::Range { lo, hi, end } =>
|
|
|
|
|
Some(vec![ConstantRange(lo, hi, end)]),
|
2016-10-26 14:38:22 -05:00
|
|
|
|
PatternKind::Array { .. } => match pcx.ty.sty {
|
2017-08-05 08:11:24 -05:00
|
|
|
|
ty::TyArray(_, length) => Some(vec![
|
|
|
|
|
Slice(length.val.to_const_int().unwrap().to_u64().unwrap())
|
|
|
|
|
]),
|
2016-10-26 14:38:22 -05:00
|
|
|
|
_ => span_bug!(pat.span, "bad ty {:?} for array pattern", pcx.ty)
|
|
|
|
|
},
|
2016-09-25 18:53:26 -05:00
|
|
|
|
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
let pat_len = prefix.len() as u64 + suffix.len() as u64;
|
2016-09-25 18:53:26 -05:00
|
|
|
|
if slice.is_some() {
|
|
|
|
|
Some((pat_len..pcx.max_slice_length+1).map(Slice).collect())
|
|
|
|
|
} else {
|
|
|
|
|
Some(vec![Slice(pat_len)])
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This computes the arity of a constructor. The arity of a constructor
|
|
|
|
|
/// is how many subpattern patterns of that constructor should be expanded to.
|
|
|
|
|
///
|
|
|
|
|
/// For instance, a tuple pattern (_, 42, Some([])) has the arity of 3.
|
|
|
|
|
/// A struct pattern's arity is the number of fields it contains, etc.
|
2017-08-05 04:27:28 -05:00
|
|
|
|
fn constructor_arity(_cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> u64 {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
debug!("constructor_arity({:?}, {:?})", ctor, ty);
|
|
|
|
|
match ty.sty {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
ty::TyTuple(ref fs, _) => fs.len() as u64,
|
2016-10-26 14:38:22 -05:00
|
|
|
|
ty::TySlice(..) | ty::TyArray(..) => match *ctor {
|
2016-09-24 10:24:34 -05:00
|
|
|
|
Slice(length) => length,
|
2016-10-26 14:38:22 -05:00
|
|
|
|
ConstantValue(_) => 0,
|
2016-09-24 10:24:34 -05:00
|
|
|
|
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty)
|
|
|
|
|
},
|
|
|
|
|
ty::TyRef(..) => 1,
|
|
|
|
|
ty::TyAdt(adt, _) => {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
adt.variants[ctor.variant_index_for_adt(adt)].fields.len() as u64
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
_ => 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
/// This computes the types of the sub patterns that a constructor should be
|
|
|
|
|
/// expanded to.
|
|
|
|
|
///
|
|
|
|
|
/// For instance, a tuple pattern (43u32, 'a') has sub pattern types [u32, char].
|
|
|
|
|
fn constructor_sub_pattern_tys<'a, 'tcx: 'a>(cx: &MatchCheckCtxt<'a, 'tcx>,
|
|
|
|
|
ctor: &Constructor,
|
|
|
|
|
ty: Ty<'tcx>) -> Vec<Ty<'tcx>>
|
|
|
|
|
{
|
|
|
|
|
debug!("constructor_sub_pattern_tys({:?}, {:?})", ctor, ty);
|
|
|
|
|
match ty.sty {
|
2017-01-11 01:58:37 -06:00
|
|
|
|
ty::TyTuple(ref fs, _) => fs.into_iter().map(|t| *t).collect(),
|
2016-11-30 11:12:03 -06:00
|
|
|
|
ty::TySlice(ty) | ty::TyArray(ty, _) => match *ctor {
|
2017-08-05 04:27:28 -05:00
|
|
|
|
Slice(length) => (0..length).map(|_| ty).collect(),
|
2016-11-30 11:12:03 -06:00
|
|
|
|
ConstantValue(_) => vec![],
|
|
|
|
|
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty)
|
|
|
|
|
},
|
|
|
|
|
ty::TyRef(_, ref ty_and_mut) => vec![ty_and_mut.ty],
|
|
|
|
|
ty::TyAdt(adt, substs) => {
|
2017-06-18 02:07:26 -05:00
|
|
|
|
if adt.is_box() {
|
|
|
|
|
// Use T as the sub pattern type of Box<T>.
|
|
|
|
|
vec![substs[0].as_type().unwrap()]
|
|
|
|
|
} else {
|
|
|
|
|
adt.variants[ctor.variant_index_for_adt(adt)].fields.iter().map(|field| {
|
|
|
|
|
let is_visible = adt.is_enum()
|
|
|
|
|
|| field.vis.is_accessible_from(cx.module, cx.tcx);
|
|
|
|
|
if is_visible {
|
|
|
|
|
field.ty(cx.tcx, substs)
|
|
|
|
|
} else {
|
2017-12-25 10:14:50 -06:00
|
|
|
|
// Treat all non-visible fields as TyErr. They
|
2017-06-18 02:07:26 -05:00
|
|
|
|
// can't appear in any other pattern from
|
|
|
|
|
// this match (because they are private),
|
|
|
|
|
// so their type does not matter - but
|
|
|
|
|
// we don't want to know they are
|
|
|
|
|
// uninhabited.
|
2017-12-25 10:14:50 -06:00
|
|
|
|
cx.tcx.types.err
|
2017-06-18 02:07:26 -05:00
|
|
|
|
}
|
|
|
|
|
}).collect()
|
|
|
|
|
}
|
2016-11-30 11:12:03 -06:00
|
|
|
|
}
|
|
|
|
|
_ => vec![],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 14:38:22 -05:00
|
|
|
|
fn slice_pat_covered_by_constructor(_tcx: TyCtxt, _span: Span,
|
|
|
|
|
ctor: &Constructor,
|
|
|
|
|
prefix: &[Pattern],
|
|
|
|
|
slice: &Option<Pattern>,
|
|
|
|
|
suffix: &[Pattern])
|
|
|
|
|
-> Result<bool, ErrorReported> {
|
|
|
|
|
let data = match *ctor {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
ConstantValue(&ty::Const { val: ConstVal::ByteStr(b), .. }) => b.data,
|
2016-10-26 14:38:22 -05:00
|
|
|
|
_ => bug!()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let pat_len = prefix.len() + suffix.len();
|
|
|
|
|
if data.len() < pat_len || (slice.is_none() && data.len() > pat_len) {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (ch, pat) in
|
|
|
|
|
data[..prefix.len()].iter().zip(prefix).chain(
|
|
|
|
|
data[data.len()-suffix.len()..].iter().zip(suffix))
|
|
|
|
|
{
|
|
|
|
|
match pat.kind {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
box PatternKind::Constant { value } => match value.val {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
ConstVal::Integral(ConstInt::U8(u)) => {
|
|
|
|
|
if u != *ch {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => span_bug!(pat.span, "bad const u8 {:?}", value)
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-16 12:05:03 -05:00
|
|
|
|
fn constructor_covered_by_range(tcx: TyCtxt, span: Span,
|
2016-09-24 10:24:34 -05:00
|
|
|
|
ctor: &Constructor,
|
2017-01-10 15:13:53 -06:00
|
|
|
|
from: &ConstVal, to: &ConstVal,
|
|
|
|
|
end: RangeEnd)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
-> Result<bool, ErrorReported> {
|
2017-01-10 15:13:53 -06:00
|
|
|
|
let cmp_from = |c_from| Ok(compare_const_vals(tcx, span, c_from, from)? != Ordering::Less);
|
|
|
|
|
let cmp_to = |c_to| compare_const_vals(tcx, span, c_to, to);
|
|
|
|
|
match *ctor {
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ConstantValue(value) => {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
let to = cmp_to(&value.val)?;
|
2017-07-16 11:34:09 -05:00
|
|
|
|
let end = (to == Ordering::Less) ||
|
|
|
|
|
(end == RangeEnd::Included && to == Ordering::Equal);
|
2017-08-04 03:25:13 -05:00
|
|
|
|
Ok(cmp_from(&value.val)? && end)
|
2017-01-10 15:13:53 -06:00
|
|
|
|
},
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ConstantRange(from, to, RangeEnd::Included) => {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
let to = cmp_to(&to.val)?;
|
2017-07-16 11:34:09 -05:00
|
|
|
|
let end = (to == Ordering::Less) ||
|
|
|
|
|
(end == RangeEnd::Included && to == Ordering::Equal);
|
2017-08-04 03:25:13 -05:00
|
|
|
|
Ok(cmp_from(&from.val)? && end)
|
2017-01-10 15:13:53 -06:00
|
|
|
|
},
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ConstantRange(from, to, RangeEnd::Excluded) => {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
let to = cmp_to(&to.val)?;
|
2017-01-10 15:13:53 -06:00
|
|
|
|
let end = (to == Ordering::Less) ||
|
|
|
|
|
(end == RangeEnd::Excluded && to == Ordering::Equal);
|
2017-08-04 03:25:13 -05:00
|
|
|
|
Ok(cmp_from(&from.val)? && end)
|
2017-01-10 15:13:53 -06:00
|
|
|
|
}
|
|
|
|
|
Single => Ok(true),
|
|
|
|
|
_ => bug!(),
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
fn patterns_for_variant<'p, 'a: 'p, 'tcx: 'a>(
|
|
|
|
|
subpatterns: &'p [FieldPattern<'tcx>],
|
|
|
|
|
wild_patterns: &[&'p Pattern<'tcx>])
|
|
|
|
|
-> Vec<&'p Pattern<'tcx>>
|
2016-09-24 10:24:34 -05:00
|
|
|
|
{
|
2016-11-30 11:12:03 -06:00
|
|
|
|
let mut result = wild_patterns.to_owned();
|
2016-09-25 18:53:26 -05:00
|
|
|
|
|
|
|
|
|
for subpat in subpatterns {
|
|
|
|
|
result[subpat.field.index()] = &subpat.pattern;
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
2016-09-25 18:53:26 -05:00
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
debug!("patterns_for_variant({:?}, {:?}) = {:?}", subpatterns, wild_patterns, result);
|
2016-09-25 18:53:26 -05:00
|
|
|
|
result
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This is the main specialization step. It expands the first pattern in the given row
|
|
|
|
|
/// into `arity` patterns based on the constructor. For most patterns, the step is trivial,
|
|
|
|
|
/// for instance tuple patterns are flattened and box patterns expand into their inner pattern.
|
|
|
|
|
///
|
|
|
|
|
/// OTOH, slice patterns with a subslice pattern (..tail) can be expanded into multiple
|
|
|
|
|
/// different patterns.
|
|
|
|
|
/// Structure patterns with a partial wild pattern (Foo { a: 42, .. }) have their missing
|
|
|
|
|
/// fields filled with wild patterns.
|
2016-11-30 11:12:03 -06:00
|
|
|
|
fn specialize<'p, 'a: 'p, 'tcx: 'a>(
|
2016-10-26 14:38:22 -05:00
|
|
|
|
cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
2016-11-30 11:12:03 -06:00
|
|
|
|
r: &[&'p Pattern<'tcx>],
|
|
|
|
|
constructor: &Constructor,
|
|
|
|
|
wild_patterns: &[&'p Pattern<'tcx>])
|
|
|
|
|
-> Option<Vec<&'p Pattern<'tcx>>>
|
2016-09-24 10:24:34 -05:00
|
|
|
|
{
|
2016-11-30 11:12:03 -06:00
|
|
|
|
let pat = &r[0];
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-09-25 18:53:26 -05:00
|
|
|
|
let head: Option<Vec<&Pattern>> = match *pat.kind {
|
2016-11-30 11:12:03 -06:00
|
|
|
|
PatternKind::Binding { .. } | PatternKind::Wild => {
|
|
|
|
|
Some(wild_patterns.to_owned())
|
|
|
|
|
},
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
PatternKind::Variant { adt_def, variant_index, ref subpatterns, .. } => {
|
2016-09-25 18:53:26 -05:00
|
|
|
|
let ref variant = adt_def.variants[variant_index];
|
|
|
|
|
if *constructor == Variant(variant.did) {
|
2016-11-30 11:12:03 -06:00
|
|
|
|
Some(patterns_for_variant(subpatterns, wild_patterns))
|
2016-09-24 10:24:34 -05:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 11:12:03 -06:00
|
|
|
|
PatternKind::Leaf { ref subpatterns } => {
|
|
|
|
|
Some(patterns_for_variant(subpatterns, wild_patterns))
|
|
|
|
|
}
|
|
|
|
|
PatternKind::Deref { ref subpattern } => {
|
|
|
|
|
Some(vec![subpattern])
|
|
|
|
|
}
|
2016-09-25 18:53:26 -05:00
|
|
|
|
|
2017-08-03 16:41:44 -05:00
|
|
|
|
PatternKind::Constant { value } => {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
match *constructor {
|
2017-08-04 03:25:13 -05:00
|
|
|
|
Slice(..) => match value.val {
|
2017-08-03 16:41:44 -05:00
|
|
|
|
ConstVal::ByteStr(b) => {
|
|
|
|
|
if wild_patterns.len() == b.data.len() {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
Some(cx.lower_byte_str_pattern(pat))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => span_bug!(pat.span,
|
|
|
|
|
"unexpected const-val {:?} with ctor {:?}", value, constructor)
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
2017-07-16 12:05:03 -05:00
|
|
|
|
match constructor_covered_by_range(
|
2017-08-04 03:25:13 -05:00
|
|
|
|
cx.tcx, pat.span, constructor, &value.val, &value.val, RangeEnd::Included
|
2016-10-26 14:38:22 -05:00
|
|
|
|
) {
|
|
|
|
|
Ok(true) => Some(vec![]),
|
|
|
|
|
Ok(false) => None,
|
|
|
|
|
Err(ErrorReported) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 03:25:13 -05:00
|
|
|
|
PatternKind::Range { lo, hi, ref end } => {
|
2017-07-16 12:05:03 -05:00
|
|
|
|
match constructor_covered_by_range(
|
2017-08-04 03:25:13 -05:00
|
|
|
|
cx.tcx, pat.span, constructor, &lo.val, &hi.val, end.clone()
|
2016-09-24 10:24:34 -05:00
|
|
|
|
) {
|
|
|
|
|
Ok(true) => Some(vec![]),
|
|
|
|
|
Ok(false) => None,
|
|
|
|
|
Err(ErrorReported) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 14:38:22 -05:00
|
|
|
|
PatternKind::Array { ref prefix, ref slice, ref suffix } |
|
2016-09-25 18:53:26 -05:00
|
|
|
|
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
match *constructor {
|
|
|
|
|
Slice(..) => {
|
|
|
|
|
let pat_len = prefix.len() + suffix.len();
|
2016-11-30 11:12:03 -06:00
|
|
|
|
if let Some(slice_count) = wild_patterns.len().checked_sub(pat_len) {
|
2016-10-26 14:38:22 -05:00
|
|
|
|
if slice_count == 0 || slice.is_some() {
|
|
|
|
|
Some(
|
|
|
|
|
prefix.iter().chain(
|
2016-11-30 11:12:03 -06:00
|
|
|
|
wild_patterns.iter().map(|p| *p)
|
|
|
|
|
.skip(prefix.len())
|
|
|
|
|
.take(slice_count)
|
|
|
|
|
.chain(
|
2016-10-26 14:38:22 -05:00
|
|
|
|
suffix.iter()
|
|
|
|
|
)).collect())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
2016-10-26 14:38:22 -05:00
|
|
|
|
ConstantValue(..) => {
|
|
|
|
|
match slice_pat_covered_by_constructor(
|
|
|
|
|
cx.tcx, pat.span, constructor, prefix, slice, suffix
|
|
|
|
|
) {
|
|
|
|
|
Ok(true) => Some(vec![]),
|
|
|
|
|
Ok(false) => None,
|
|
|
|
|
Err(ErrorReported) => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => span_bug!(pat.span,
|
|
|
|
|
"unexpected ctor {:?} for slice pat", constructor)
|
2016-09-24 10:24:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-30 11:12:03 -06:00
|
|
|
|
debug!("specialize({:?}, {:?}) = {:?}", r[0], wild_patterns, head);
|
2016-09-24 10:24:34 -05:00
|
|
|
|
|
|
|
|
|
head.map(|mut head| {
|
2016-11-30 11:12:03 -06:00
|
|
|
|
head.extend_from_slice(&r[1 ..]);
|
2016-09-24 10:24:34 -05:00
|
|
|
|
head
|
|
|
|
|
})
|
|
|
|
|
}
|