Auto merge of #36695 - arielb1:literal-match, r=eddyb
Refactor match checking to use HAIR Refactor match checking to use HAIR instead of HIR, fixing quite a few bugs in the process. r? @eddyb
This commit is contained in:
commit
4a584637b0
2
src/Cargo.lock
generated
2
src/Cargo.lock
generated
@ -330,11 +330,13 @@ dependencies = [
|
||||
name = "rustc_const_eval"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"arena 0.0.0",
|
||||
"graphviz 0.0.0",
|
||||
"log 0.0.0",
|
||||
"rustc 0.0.0",
|
||||
"rustc_back 0.0.0",
|
||||
"rustc_const_math 0.0.0",
|
||||
"rustc_data_structures 0.0.0",
|
||||
"rustc_errors 0.0.0",
|
||||
"serialize 0.0.0",
|
||||
"syntax 0.0.0",
|
||||
|
@ -9,11 +9,13 @@ path = "lib.rs"
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies]
|
||||
arena = { path = "../libarena" }
|
||||
log = { path = "../liblog" }
|
||||
serialize = { path = "../libserialize" }
|
||||
rustc = { path = "../librustc" }
|
||||
rustc_back = { path = "../librustc_back" }
|
||||
rustc_const_math = { path = "../librustc_const_math" }
|
||||
rustc_data_structures = { path = "../librustc_data_structures" }
|
||||
rustc_errors = { path = "../librustc_errors" }
|
||||
syntax = { path = "../libsyntax" }
|
||||
graphviz = { path = "../libgraphviz" }
|
||||
|
767
src/librustc_const_eval/_match.rs
Normal file
767
src/librustc_const_eval/_match.rs
Normal file
@ -0,0 +1,767 @@
|
||||
// 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;
|
||||
use eval::{compare_const_vals};
|
||||
|
||||
use rustc_const_math::ConstInt;
|
||||
|
||||
use rustc_data_structures::fnv::FnvHashMap;
|
||||
use rustc_data_structures::indexed_vec::Idx;
|
||||
|
||||
use pattern::{FieldPattern, Pattern, PatternKind};
|
||||
use pattern::{PatternFoldable, PatternFolder};
|
||||
|
||||
use rustc::hir::def_id::{DefId};
|
||||
use rustc::hir::pat_util::def_to_path;
|
||||
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
||||
|
||||
use rustc::hir;
|
||||
use rustc::hir::def::CtorKind;
|
||||
use rustc::hir::{Pat, PatKind};
|
||||
use rustc::util::common::ErrorReported;
|
||||
|
||||
use syntax::ast::{self, DUMMY_NODE_ID};
|
||||
use syntax::codemap::Spanned;
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::{Span, DUMMY_SP};
|
||||
|
||||
use arena::TypedArena;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt;
|
||||
use std::iter::{FromIterator, IntoIterator, repeat};
|
||||
|
||||
pub fn expand_pattern<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>, pat: Pattern<'tcx>)
|
||||
-> &'a Pattern<'tcx>
|
||||
{
|
||||
cx.pattern_arena.alloc(LiteralExpander.fold_pattern(&pat))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const DUMMY_WILD_PAT: &'static Pat = &Pat {
|
||||
id: DUMMY_NODE_ID,
|
||||
node: PatKind::Wild,
|
||||
span: DUMMY_SP
|
||||
};
|
||||
|
||||
impl<'tcx> Pattern<'tcx> {
|
||||
fn is_wildcard(&self) -> bool {
|
||||
match *self.kind {
|
||||
PatternKind::Binding { subpattern: None, .. } | PatternKind::Wild =>
|
||||
true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Matrix<'a, 'tcx: 'a>(Vec<Vec<&'a Pattern<'tcx>>>);
|
||||
|
||||
impl<'a, 'tcx> Matrix<'a, 'tcx> {
|
||||
pub fn empty() -> Self {
|
||||
Matrix(vec![])
|
||||
}
|
||||
|
||||
pub fn push(&mut self, row: Vec<&'a Pattern<'tcx>>) {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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>,
|
||||
/// A wild pattern with an error type - it exists to avoid having to normalize
|
||||
/// associated types to get field types.
|
||||
pub wild_pattern: &'a Pattern<'tcx>,
|
||||
pub pattern_arena: &'a TypedArena<Pattern<'tcx>>,
|
||||
pub byte_array_map: FnvHashMap<*const Pattern<'tcx>, Vec<&'a Pattern<'tcx>>>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
|
||||
pub fn create_and_enter<F, R>(
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
f: F) -> R
|
||||
where F: for<'b> FnOnce(MatchCheckCtxt<'b, 'tcx>) -> R
|
||||
{
|
||||
let wild_pattern = Pattern {
|
||||
ty: tcx.types.err,
|
||||
span: DUMMY_SP,
|
||||
kind: box PatternKind::Wild
|
||||
};
|
||||
|
||||
let pattern_arena = TypedArena::new();
|
||||
|
||||
f(MatchCheckCtxt {
|
||||
tcx: tcx,
|
||||
wild_pattern: &wild_pattern,
|
||||
pattern_arena: &pattern_arena,
|
||||
byte_array_map: FnvHashMap(),
|
||||
})
|
||||
}
|
||||
|
||||
// convert a byte-string pattern to a list of u8 patterns.
|
||||
fn lower_byte_str_pattern(&mut self, pat: &'a Pattern<'tcx>) -> Vec<&'a Pattern<'tcx>> {
|
||||
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 {
|
||||
value: ConstVal::ByteStr(ref data)
|
||||
} => {
|
||||
data.iter().map(|c| &*pattern_arena.alloc(Pattern {
|
||||
ty: tcx.types.u8,
|
||||
span: pat.span,
|
||||
kind: box PatternKind::Constant {
|
||||
value: ConstVal::Integral(ConstInt::U8(*c))
|
||||
}
|
||||
})).collect()
|
||||
}
|
||||
_ => span_bug!(pat.span, "unexpected byte array pattern {:?}", pat)
|
||||
}
|
||||
}).clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Constructor {
|
||||
/// 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.
|
||||
ConstantValue(ConstVal),
|
||||
/// Ranges of literal values (2..5).
|
||||
ConstantRange(ConstVal, ConstVal),
|
||||
/// Array patterns of length n.
|
||||
Slice(usize),
|
||||
}
|
||||
|
||||
impl Constructor {
|
||||
fn variant_for_adt<'tcx, 'container, 'a>(&self,
|
||||
adt: &'a ty::AdtDefData<'tcx, 'container>)
|
||||
-> &'a ty::VariantDefData<'tcx, 'container> {
|
||||
match self {
|
||||
&Variant(vid) => adt.variant_with_id(vid),
|
||||
&Single => {
|
||||
assert_eq!(adt.variants.len(), 1);
|
||||
&adt.variants[0]
|
||||
}
|
||||
_ => bug!("bad constructor {:?} for adt {:?}", self, adt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Usefulness {
|
||||
Useful,
|
||||
UsefulWithWitness(Vec<Witness>),
|
||||
NotUseful
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum WitnessPreference {
|
||||
ConstructWitness,
|
||||
LeaveOutWitness
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct PatternContext<'tcx> {
|
||||
ty: Ty<'tcx>,
|
||||
max_slice_length: usize,
|
||||
}
|
||||
|
||||
|
||||
fn const_val_to_expr(value: &ConstVal) -> P<hir::Expr> {
|
||||
let node = match value {
|
||||
&ConstVal::Bool(b) => ast::LitKind::Bool(b),
|
||||
_ => bug!()
|
||||
};
|
||||
P(hir::Expr {
|
||||
id: DUMMY_NODE_ID,
|
||||
node: hir::ExprLit(P(Spanned { node: node, span: DUMMY_SP })),
|
||||
span: DUMMY_SP,
|
||||
attrs: ast::ThinVec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// A stack of patterns in reverse order of construction
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Witness(Vec<P<Pat>>);
|
||||
|
||||
impl Witness {
|
||||
pub fn single_pattern(&self) -> &Pat {
|
||||
assert_eq!(self.0.len(), 1);
|
||||
&self.0[0]
|
||||
}
|
||||
|
||||
fn push_wild_constructor<'a, 'tcx>(
|
||||
mut self,
|
||||
cx: &MatchCheckCtxt<'a, 'tcx>,
|
||||
ctor: &Constructor,
|
||||
ty: Ty<'tcx>)
|
||||
-> Self
|
||||
{
|
||||
let arity = constructor_arity(cx, ctor, ty);
|
||||
self.0.extend(repeat(DUMMY_WILD_PAT).take(arity).map(|p| P(p.clone())));
|
||||
self.apply_constructor(cx, ctor, ty)
|
||||
}
|
||||
|
||||
|
||||
/// 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 }
|
||||
fn apply_constructor<'a, 'tcx>(
|
||||
mut self,
|
||||
cx: &MatchCheckCtxt<'a,'tcx>,
|
||||
ctor: &Constructor,
|
||||
ty: Ty<'tcx>)
|
||||
-> Self
|
||||
{
|
||||
let arity = constructor_arity(cx, ctor, ty);
|
||||
let pat = {
|
||||
let len = self.0.len();
|
||||
let mut pats = self.0.drain(len-arity..).rev();
|
||||
|
||||
match ty.sty {
|
||||
ty::TyTuple(..) => PatKind::Tuple(pats.collect(), None),
|
||||
|
||||
ty::TyAdt(adt, _) => {
|
||||
let v = ctor.variant_for_adt(adt);
|
||||
match v.ctor_kind {
|
||||
CtorKind::Fictive => {
|
||||
let field_pats: hir::HirVec<_> = v.fields.iter()
|
||||
.zip(pats)
|
||||
.filter(|&(_, ref pat)| pat.node != PatKind::Wild)
|
||||
.map(|(field, pat)| Spanned {
|
||||
span: DUMMY_SP,
|
||||
node: hir::FieldPat {
|
||||
name: field.name,
|
||||
pat: pat,
|
||||
is_shorthand: false,
|
||||
}
|
||||
}).collect();
|
||||
let has_more_fields = field_pats.len() < arity;
|
||||
PatKind::Struct(
|
||||
def_to_path(cx.tcx, v.did), field_pats, has_more_fields)
|
||||
}
|
||||
CtorKind::Fn => {
|
||||
PatKind::TupleStruct(
|
||||
def_to_path(cx.tcx, v.did), pats.collect(), None)
|
||||
}
|
||||
CtorKind::Const => {
|
||||
PatKind::Path(None, def_to_path(cx.tcx, v.did))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ty::TyRef(_, ty::TypeAndMut { mutbl, .. }) => {
|
||||
PatKind::Ref(pats.nth(0).unwrap(), mutbl)
|
||||
}
|
||||
|
||||
ty::TySlice(_) | ty::TyArray(..) => {
|
||||
PatKind::Slice(pats.collect(), None, hir::HirVec::new())
|
||||
}
|
||||
|
||||
_ => {
|
||||
match *ctor {
|
||||
ConstantValue(ref v) => PatKind::Lit(const_val_to_expr(v)),
|
||||
_ => PatKind::Wild,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.0.push(P(hir::Pat {
|
||||
id: DUMMY_NODE_ID,
|
||||
node: pat,
|
||||
span: DUMMY_SP
|
||||
}));
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the set of constructors from the same type as the first column of `matrix`,
|
||||
/// that are matched only by wildcard patterns from that 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 the return value.
|
||||
fn missing_constructors(cx: &mut MatchCheckCtxt,
|
||||
matrix: &Matrix,
|
||||
pcx: PatternContext) -> Vec<Constructor> {
|
||||
let used_constructors: Vec<Constructor> =
|
||||
matrix.0.iter()
|
||||
.flat_map(|row| pat_constructors(cx, row[0], pcx).unwrap_or(vec![]))
|
||||
.collect();
|
||||
debug!("used_constructors = {:?}", used_constructors);
|
||||
all_constructors(cx, pcx).into_iter()
|
||||
.filter(|c| !used_constructors.contains(c))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 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
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// but is instead bounded by the maximum fixed length of slice patterns in
|
||||
/// the column of patterns being analyzed.
|
||||
fn all_constructors(_cx: &mut MatchCheckCtxt, pcx: PatternContext) -> Vec<Constructor> {
|
||||
match pcx.ty.sty {
|
||||
ty::TyBool =>
|
||||
[true, false].iter().map(|b| ConstantValue(ConstVal::Bool(*b))).collect(),
|
||||
ty::TySlice(_) =>
|
||||
(0..pcx.max_slice_length+1).map(|length| Slice(length)).collect(),
|
||||
ty::TyArray(_, length) => vec![Slice(length)],
|
||||
ty::TyAdt(def, _) if def.is_enum() && def.variants.len() > 1 =>
|
||||
def.variants.iter().map(|v| Variant(v.did)).collect(),
|
||||
_ => vec![Single]
|
||||
}
|
||||
}
|
||||
|
||||
/// Algorithm from http://moscova.inria.fr/~maranget/papers/warn/index.html
|
||||
///
|
||||
/// Whether a vector `v` of patterns is 'useful' in relation to a set of such
|
||||
/// vectors `m` is defined as there being a set of inputs that will match `v`
|
||||
/// but not any of the sets in `m`.
|
||||
///
|
||||
/// 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).
|
||||
///
|
||||
/// Note: is_useful doesn't work on empty types, as the paper notes.
|
||||
/// So it assumes that v is non-empty.
|
||||
pub fn is_useful<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
||||
matrix: &Matrix<'a, 'tcx>,
|
||||
v: &[&'a Pattern<'tcx>],
|
||||
witness: WitnessPreference)
|
||||
-> Usefulness {
|
||||
let &Matrix(ref rows) = matrix;
|
||||
debug!("is_useful({:?}, {:?})", matrix, v);
|
||||
if rows.is_empty() {
|
||||
return match witness {
|
||||
ConstructWitness => UsefulWithWitness(vec![Witness(
|
||||
repeat(DUMMY_WILD_PAT).take(v.len()).map(|p| P(p.clone())).collect()
|
||||
)]),
|
||||
LeaveOutWitness => Useful
|
||||
};
|
||||
}
|
||||
if rows[0].is_empty() {
|
||||
return NotUseful;
|
||||
}
|
||||
|
||||
let &Matrix(ref rows) = matrix;
|
||||
assert!(rows.iter().all(|r| r.len() == v.len()));
|
||||
let pcx = PatternContext {
|
||||
ty: rows.iter().map(|r| r[0].ty).find(|ty| !ty.references_error())
|
||||
.unwrap_or(v[0].ty),
|
||||
max_slice_length: rows.iter().filter_map(|row| match *row[0].kind {
|
||||
PatternKind::Slice { ref prefix, slice: _, ref suffix } =>
|
||||
Some(prefix.len() + suffix.len()),
|
||||
PatternKind::Constant { value: ConstVal::ByteStr(ref data) } =>
|
||||
Some(data.len()),
|
||||
_ => None
|
||||
}).max().map_or(0, |v| v + 1)
|
||||
};
|
||||
|
||||
debug!("is_useful_expand_first_col: pcx={:?}, expanding {:?}", pcx, v[0]);
|
||||
|
||||
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)
|
||||
).find(|result| result != &NotUseful).unwrap_or(NotUseful)
|
||||
} else {
|
||||
debug!("is_useful - expanding wildcard");
|
||||
let constructors = missing_constructors(cx, matrix, pcx);
|
||||
debug!("is_useful - missing_constructors = {:?}", constructors);
|
||||
if constructors.is_empty() {
|
||||
all_constructors(cx, pcx).into_iter().map(|c| {
|
||||
is_useful_specialized(cx, matrix, v, c.clone(), pcx.ty, witness)
|
||||
}).find(|result| result != &NotUseful).unwrap_or(NotUseful)
|
||||
} else {
|
||||
let matrix = rows.iter().filter_map(|r| {
|
||||
if r[0].is_wildcard() {
|
||||
Some(r[1..].to_vec())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).collect();
|
||||
match is_useful(cx, &matrix, &v[1..], witness) {
|
||||
UsefulWithWitness(pats) => {
|
||||
let cx = &*cx;
|
||||
UsefulWithWitness(pats.into_iter().flat_map(|witness| {
|
||||
constructors.iter().map(move |ctor| {
|
||||
witness.clone().push_wild_constructor(cx, ctor, pcx.ty)
|
||||
})
|
||||
}).collect())
|
||||
}
|
||||
result => result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_useful_specialized<'a, 'tcx>(
|
||||
cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
||||
&Matrix(ref m): &Matrix<'a, 'tcx>,
|
||||
v: &[&'a Pattern<'tcx>],
|
||||
ctor: Constructor,
|
||||
lty: Ty<'tcx>,
|
||||
witness: WitnessPreference) -> Usefulness
|
||||
{
|
||||
let arity = constructor_arity(cx, &ctor, lty);
|
||||
let matrix = Matrix(m.iter().flat_map(|r| {
|
||||
specialize(cx, &r[..], &ctor, 0, arity)
|
||||
}).collect());
|
||||
match specialize(cx, v, &ctor, 0, arity) {
|
||||
Some(v) => match is_useful(cx, &matrix, &v[..], witness) {
|
||||
UsefulWithWitness(witnesses) => UsefulWithWitness(
|
||||
witnesses.into_iter()
|
||||
.map(|witness| witness.apply_constructor(cx, &ctor, lty))
|
||||
.collect()
|
||||
),
|
||||
result => result
|
||||
},
|
||||
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.
|
||||
///
|
||||
/// Returns None in case of a catch-all, which can't be specialized.
|
||||
fn pat_constructors(_cx: &mut MatchCheckCtxt,
|
||||
pat: &Pattern,
|
||||
pcx: PatternContext)
|
||||
-> Option<Vec<Constructor>>
|
||||
{
|
||||
match *pat.kind {
|
||||
PatternKind::Binding { .. } | PatternKind::Wild =>
|
||||
None,
|
||||
PatternKind::Leaf { .. } | PatternKind::Deref { .. } =>
|
||||
Some(vec![Single]),
|
||||
PatternKind::Variant { adt_def, variant_index, .. } =>
|
||||
Some(vec![Variant(adt_def.variants[variant_index].did)]),
|
||||
PatternKind::Constant { ref value } =>
|
||||
Some(vec![ConstantValue(value.clone())]),
|
||||
PatternKind::Range { ref lo, ref hi } =>
|
||||
Some(vec![ConstantRange(lo.clone(), hi.clone())]),
|
||||
PatternKind::Array { .. } => match pcx.ty.sty {
|
||||
ty::TyArray(_, length) => Some(vec![Slice(length)]),
|
||||
_ => span_bug!(pat.span, "bad ty {:?} for array pattern", pcx.ty)
|
||||
},
|
||||
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
|
||||
let pat_len = prefix.len() + suffix.len();
|
||||
if slice.is_some() {
|
||||
Some((pat_len..pcx.max_slice_length+1).map(Slice).collect())
|
||||
} else {
|
||||
Some(vec![Slice(pat_len)])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn constructor_arity(_cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> usize {
|
||||
debug!("constructor_arity({:?}, {:?})", ctor, ty);
|
||||
match ty.sty {
|
||||
ty::TyTuple(ref fs) => fs.len(),
|
||||
ty::TyBox(_) => 1,
|
||||
ty::TySlice(..) | ty::TyArray(..) => match *ctor {
|
||||
Slice(length) => length,
|
||||
ConstantValue(_) => 0,
|
||||
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty)
|
||||
},
|
||||
ty::TyRef(..) => 1,
|
||||
ty::TyAdt(adt, _) => {
|
||||
ctor.variant_for_adt(adt).fields.len()
|
||||
}
|
||||
_ => 0
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
ConstantValue(ConstVal::ByteStr(ref data)) => data,
|
||||
_ => 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 {
|
||||
box PatternKind::Constant { ref value } => match *value {
|
||||
ConstVal::Integral(ConstInt::U8(u)) => {
|
||||
if u != *ch {
|
||||
return Ok(false);
|
||||
}
|
||||
},
|
||||
_ => span_bug!(pat.span, "bad const u8 {:?}", value)
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn range_covered_by_constructor(tcx: TyCtxt, span: Span,
|
||||
ctor: &Constructor,
|
||||
from: &ConstVal, to: &ConstVal)
|
||||
-> Result<bool, ErrorReported> {
|
||||
let (c_from, c_to) = match *ctor {
|
||||
ConstantValue(ref value) => (value, value),
|
||||
ConstantRange(ref from, ref to) => (from, to),
|
||||
Single => return Ok(true),
|
||||
_ => bug!()
|
||||
};
|
||||
let cmp_from = compare_const_vals(tcx, span, c_from, from)?;
|
||||
let cmp_to = compare_const_vals(tcx, span, c_to, to)?;
|
||||
Ok(cmp_from != Ordering::Less && cmp_to != Ordering::Greater)
|
||||
}
|
||||
|
||||
fn patterns_for_variant<'a, 'tcx>(
|
||||
cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
||||
subpatterns: &'a [FieldPattern<'tcx>],
|
||||
arity: usize)
|
||||
-> Vec<&'a Pattern<'tcx>>
|
||||
{
|
||||
let mut result = vec![cx.wild_pattern; arity];
|
||||
|
||||
for subpat in subpatterns {
|
||||
result[subpat.field.index()] = &subpat.pattern;
|
||||
}
|
||||
|
||||
debug!("patterns_for_variant({:?}, {:?}) = {:?}", subpatterns, arity, result);
|
||||
result
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn specialize<'a, 'tcx>(
|
||||
cx: &mut MatchCheckCtxt<'a, 'tcx>,
|
||||
r: &[&'a Pattern<'tcx>],
|
||||
constructor: &Constructor, col: usize, arity: usize)
|
||||
-> Option<Vec<&'a Pattern<'tcx>>>
|
||||
{
|
||||
let pat = &r[col];
|
||||
|
||||
let head: Option<Vec<&Pattern>> = match *pat.kind {
|
||||
PatternKind::Binding { .. } | PatternKind::Wild =>
|
||||
Some(vec![cx.wild_pattern; arity]),
|
||||
|
||||
PatternKind::Variant { adt_def, variant_index, ref subpatterns } => {
|
||||
let ref variant = adt_def.variants[variant_index];
|
||||
if *constructor == Variant(variant.did) {
|
||||
Some(patterns_for_variant(cx, subpatterns, arity))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Leaf { ref subpatterns } => Some(patterns_for_variant(cx, subpatterns, arity)),
|
||||
PatternKind::Deref { ref subpattern } => Some(vec![subpattern]),
|
||||
|
||||
PatternKind::Constant { ref value } => {
|
||||
match *constructor {
|
||||
Slice(..) => match *value {
|
||||
ConstVal::ByteStr(ref data) => {
|
||||
if arity == data.len() {
|
||||
Some(cx.lower_byte_str_pattern(pat))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => span_bug!(pat.span,
|
||||
"unexpected const-val {:?} with ctor {:?}", value, constructor)
|
||||
},
|
||||
_ => {
|
||||
match range_covered_by_constructor(
|
||||
cx.tcx, pat.span, constructor, value, value
|
||||
) {
|
||||
Ok(true) => Some(vec![]),
|
||||
Ok(false) => None,
|
||||
Err(ErrorReported) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Range { ref lo, ref hi } => {
|
||||
match range_covered_by_constructor(
|
||||
cx.tcx, pat.span, constructor, lo, hi
|
||||
) {
|
||||
Ok(true) => Some(vec![]),
|
||||
Ok(false) => None,
|
||||
Err(ErrorReported) => None,
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Array { ref prefix, ref slice, ref suffix } |
|
||||
PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
|
||||
match *constructor {
|
||||
Slice(..) => {
|
||||
let pat_len = prefix.len() + suffix.len();
|
||||
if let Some(slice_count) = arity.checked_sub(pat_len) {
|
||||
if slice_count == 0 || slice.is_some() {
|
||||
Some(
|
||||
prefix.iter().chain(
|
||||
repeat(cx.wild_pattern).take(slice_count).chain(
|
||||
suffix.iter()
|
||||
)).collect())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
};
|
||||
debug!("specialize({:?}, {:?}) = {:?}", r[col], arity, head);
|
||||
|
||||
head.map(|mut head| {
|
||||
head.extend_from_slice(&r[..col]);
|
||||
head.extend_from_slice(&r[col + 1..]);
|
||||
head
|
||||
})
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -40,7 +40,7 @@ Ensure the ordering of the match arm is correct and remove any superfluous
|
||||
arms.
|
||||
"##,
|
||||
|
||||
E0002: r##"
|
||||
/*E0002: r##"
|
||||
This error indicates that an empty match expression is invalid because the type
|
||||
it is matching on is non-empty (there exist values of this type). In safe code
|
||||
it is impossible to create an instance of an empty type, so empty match
|
||||
@ -68,10 +68,10 @@ fn foo(x: Option<String>) {
|
||||
}
|
||||
}
|
||||
```
|
||||
"##,
|
||||
"##,*/
|
||||
|
||||
|
||||
E0003: r##"
|
||||
/*E0003: r##"
|
||||
Not-a-Number (NaN) values cannot be compared for equality and hence can never
|
||||
match the input to a match expression. So, the following will not compile:
|
||||
|
||||
@ -100,7 +100,7 @@ match number {
|
||||
}
|
||||
```
|
||||
"##,
|
||||
|
||||
*/
|
||||
|
||||
E0004: r##"
|
||||
This error indicates that the compiler cannot guarantee a matching pattern for
|
||||
|
@ -392,7 +392,7 @@ pub fn note_const_eval_err<'a, 'tcx>(
|
||||
|
||||
pub fn eval_const_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
e: &Expr) -> ConstVal {
|
||||
match eval_const_expr_partial(tcx, e, ExprTypeChecked, None) {
|
||||
match eval_const_expr_checked(tcx, e) {
|
||||
Ok(r) => r,
|
||||
// non-const path still needs to be a fatal error, because enums are funky
|
||||
Err(s) => {
|
||||
@ -407,15 +407,21 @@ pub fn eval_const_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_const_expr_checked<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
e: &Expr) -> EvalResult
|
||||
{
|
||||
eval_const_expr_partial(tcx, e, ExprTypeChecked, None)
|
||||
}
|
||||
|
||||
pub type FnArgMap<'a> = Option<&'a NodeMap<ConstVal>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ConstEvalErr {
|
||||
pub span: Span,
|
||||
pub kind: ErrKind,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ErrKind {
|
||||
CannotCast,
|
||||
CannotCastTo(&'static str),
|
||||
|
@ -31,11 +31,13 @@
|
||||
#![feature(box_patterns)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
extern crate arena;
|
||||
#[macro_use] extern crate syntax;
|
||||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate rustc;
|
||||
extern crate rustc_back;
|
||||
extern crate rustc_const_math;
|
||||
extern crate rustc_data_structures;
|
||||
extern crate rustc_errors;
|
||||
extern crate graphviz;
|
||||
extern crate syntax_pos;
|
||||
@ -46,7 +48,9 @@ extern crate serialize as rustc_serialize; // used by deriving
|
||||
pub mod diagnostics;
|
||||
|
||||
mod eval;
|
||||
mod _match;
|
||||
pub mod check_match;
|
||||
pub mod pattern;
|
||||
|
||||
pub use eval::*;
|
||||
|
||||
|
612
src/librustc_const_eval/pattern.rs
Normal file
612
src/librustc_const_eval/pattern.rs
Normal file
@ -0,0 +1,612 @@
|
||||
// Copyright 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 eval;
|
||||
|
||||
use rustc::middle::const_val::ConstVal;
|
||||
use rustc::mir::repr::{Field, BorrowKind, Mutability};
|
||||
use rustc::ty::{self, TyCtxt, AdtDef, Ty, Region};
|
||||
use rustc::hir::{self, PatKind};
|
||||
use rustc::hir::def::Def;
|
||||
use rustc::hir::def_id::DefId;
|
||||
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
|
||||
|
||||
use rustc_data_structures::indexed_vec::Idx;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::Span;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum PatternError {
|
||||
StaticInPattern(Span),
|
||||
BadConstInPattern(Span, DefId),
|
||||
ConstEval(eval::ConstEvalErr),
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum BindingMode<'tcx> {
|
||||
ByValue,
|
||||
ByRef(&'tcx Region, BorrowKind),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FieldPattern<'tcx> {
|
||||
pub field: Field,
|
||||
pub pattern: Pattern<'tcx>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pattern<'tcx> {
|
||||
pub ty: Ty<'tcx>,
|
||||
pub span: Span,
|
||||
pub kind: Box<PatternKind<'tcx>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum PatternKind<'tcx> {
|
||||
Wild,
|
||||
|
||||
/// x, ref x, x @ P, etc
|
||||
Binding {
|
||||
mutability: Mutability,
|
||||
name: ast::Name,
|
||||
mode: BindingMode<'tcx>,
|
||||
var: ast::NodeId,
|
||||
ty: Ty<'tcx>,
|
||||
subpattern: Option<Pattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// Foo(...) or Foo{...} or Foo, where `Foo` is a variant name from an adt with >1 variants
|
||||
Variant {
|
||||
adt_def: AdtDef<'tcx>,
|
||||
variant_index: usize,
|
||||
subpatterns: Vec<FieldPattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// (...), Foo(...), Foo{...}, or Foo, where `Foo` is a variant name from an adt with 1 variant
|
||||
Leaf {
|
||||
subpatterns: Vec<FieldPattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// box P, &P, &mut P, etc
|
||||
Deref {
|
||||
subpattern: Pattern<'tcx>,
|
||||
},
|
||||
|
||||
Constant {
|
||||
value: ConstVal,
|
||||
},
|
||||
|
||||
Range {
|
||||
lo: ConstVal,
|
||||
hi: ConstVal,
|
||||
},
|
||||
|
||||
/// matches against a slice, checking the length and extracting elements
|
||||
Slice {
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
suffix: Vec<Pattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// fixed match against an array, irrefutable
|
||||
Array {
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
suffix: Vec<Pattern<'tcx>>,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct PatternContext<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
||||
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
||||
pub errors: Vec<PatternError>,
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx> Pattern<'tcx> {
|
||||
pub fn from_hir(tcx: TyCtxt<'a, 'gcx, 'tcx>, pat: &hir::Pat) -> Self {
|
||||
let mut pcx = PatternContext::new(tcx);
|
||||
let result = pcx.lower_pattern(pat);
|
||||
if !pcx.errors.is_empty() {
|
||||
span_bug!(pat.span, "encountered errors lowering pattern: {:?}", pcx.errors)
|
||||
}
|
||||
debug!("Pattern::from_hir({:?}) = {:?}", pat, result);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx> PatternContext<'a, 'gcx, 'tcx> {
|
||||
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self {
|
||||
PatternContext { tcx: tcx, errors: vec![] }
|
||||
}
|
||||
|
||||
pub fn lower_pattern(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
|
||||
let mut ty = self.tcx.node_id_to_type(pat.id);
|
||||
|
||||
let kind = match pat.node {
|
||||
PatKind::Wild => PatternKind::Wild,
|
||||
|
||||
PatKind::Lit(ref value) => {
|
||||
match eval::eval_const_expr_checked(self.tcx.global_tcx(), value) {
|
||||
Ok(value) => {
|
||||
PatternKind::Constant { value: value }
|
||||
}
|
||||
Err(e) => {
|
||||
self.errors.push(PatternError::ConstEval(e));
|
||||
PatternKind::Wild
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Range(ref lo, ref hi) => {
|
||||
let r_lo = eval::eval_const_expr_checked(self.tcx.global_tcx(), lo);
|
||||
if let Err(ref e_lo) = r_lo {
|
||||
self.errors.push(PatternError::ConstEval(e_lo.clone()));
|
||||
}
|
||||
|
||||
let r_hi = eval::eval_const_expr_checked(self.tcx.global_tcx(), hi);
|
||||
if let Err(ref e_hi) = r_hi {
|
||||
self.errors.push(PatternError::ConstEval(e_hi.clone()));
|
||||
}
|
||||
|
||||
if let (Ok(lo), Ok(hi)) = (r_lo, r_hi) {
|
||||
PatternKind::Range { lo: lo, hi: hi }
|
||||
} else {
|
||||
PatternKind::Wild
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Path(..) => {
|
||||
match self.tcx.expect_def(pat.id) {
|
||||
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
|
||||
let tcx = self.tcx.global_tcx();
|
||||
let substs = Some(self.tcx.node_id_item_substs(pat.id).substs);
|
||||
match eval::lookup_const_by_id(tcx, def_id, substs) {
|
||||
Some((const_expr, _const_ty)) => {
|
||||
match eval::const_expr_to_pat(
|
||||
tcx, const_expr, pat.id, pat.span)
|
||||
{
|
||||
Ok(pat) => return self.lower_pattern(&pat),
|
||||
Err(_) => {
|
||||
self.errors.push(PatternError::BadConstInPattern(
|
||||
pat.span, def_id));
|
||||
PatternKind::Wild
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.errors.push(PatternError::StaticInPattern(pat.span));
|
||||
PatternKind::Wild
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => self.lower_variant_or_leaf(pat, vec![])
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Ref(ref subpattern, _) |
|
||||
PatKind::Box(ref subpattern) => {
|
||||
PatternKind::Deref { subpattern: self.lower_pattern(subpattern) }
|
||||
}
|
||||
|
||||
PatKind::Slice(ref prefix, ref slice, ref suffix) => {
|
||||
let ty = self.tcx.node_id_to_type(pat.id);
|
||||
match ty.sty {
|
||||
ty::TyRef(_, mt) =>
|
||||
PatternKind::Deref {
|
||||
subpattern: Pattern {
|
||||
ty: mt.ty,
|
||||
span: pat.span,
|
||||
kind: Box::new(self.slice_or_array_pattern(
|
||||
pat.span, mt.ty, prefix, slice, suffix))
|
||||
},
|
||||
},
|
||||
|
||||
ty::TySlice(..) |
|
||||
ty::TyArray(..) =>
|
||||
self.slice_or_array_pattern(pat.span, ty, prefix, slice, suffix),
|
||||
|
||||
ref sty =>
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"unexpanded type for vector pattern: {:?}",
|
||||
sty),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Tuple(ref subpatterns, ddpos) => {
|
||||
match self.tcx.node_id_to_type(pat.id).sty {
|
||||
ty::TyTuple(ref tys) => {
|
||||
let subpatterns =
|
||||
subpatterns.iter()
|
||||
.enumerate_and_adjust(tys.len(), ddpos)
|
||||
.map(|(i, subpattern)| FieldPattern {
|
||||
field: Field::new(i),
|
||||
pattern: self.lower_pattern(subpattern)
|
||||
})
|
||||
.collect();
|
||||
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
|
||||
ref sty => span_bug!(pat.span, "unexpected type for tuple pattern: {:?}", sty),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Binding(bm, ref ident, ref sub) => {
|
||||
let def_id = self.tcx.expect_def(pat.id).def_id();
|
||||
let id = self.tcx.map.as_local_node_id(def_id).unwrap();
|
||||
let var_ty = self.tcx.node_id_to_type(pat.id);
|
||||
let region = match var_ty.sty {
|
||||
ty::TyRef(r, _) => Some(r),
|
||||
_ => None,
|
||||
};
|
||||
let (mutability, mode) = match bm {
|
||||
hir::BindByValue(hir::MutMutable) =>
|
||||
(Mutability::Mut, BindingMode::ByValue),
|
||||
hir::BindByValue(hir::MutImmutable) =>
|
||||
(Mutability::Not, BindingMode::ByValue),
|
||||
hir::BindByRef(hir::MutMutable) =>
|
||||
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Mut)),
|
||||
hir::BindByRef(hir::MutImmutable) =>
|
||||
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Shared)),
|
||||
};
|
||||
|
||||
// A ref x pattern is the same node used for x, and as such it has
|
||||
// x's type, which is &T, where we want T (the type being matched).
|
||||
if let hir::BindByRef(_) = bm {
|
||||
if let ty::TyRef(_, mt) = ty.sty {
|
||||
ty = mt.ty;
|
||||
} else {
|
||||
bug!("`ref {}` has wrong type {}", ident.node, ty);
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Binding {
|
||||
mutability: mutability,
|
||||
mode: mode,
|
||||
name: ident.node,
|
||||
var: id,
|
||||
ty: var_ty,
|
||||
subpattern: self.lower_opt_pattern(sub),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::TupleStruct(_, ref subpatterns, ddpos) => {
|
||||
let pat_ty = self.tcx.node_id_to_type(pat.id);
|
||||
let adt_def = match pat_ty.sty {
|
||||
ty::TyAdt(adt_def, _) => adt_def,
|
||||
_ => span_bug!(pat.span, "tuple struct pattern not applied to an ADT"),
|
||||
};
|
||||
let variant_def = adt_def.variant_of_def(self.tcx.expect_def(pat.id));
|
||||
|
||||
let subpatterns =
|
||||
subpatterns.iter()
|
||||
.enumerate_and_adjust(variant_def.fields.len(), ddpos)
|
||||
.map(|(i, field)| FieldPattern {
|
||||
field: Field::new(i),
|
||||
pattern: self.lower_pattern(field),
|
||||
})
|
||||
.collect();
|
||||
self.lower_variant_or_leaf(pat, subpatterns)
|
||||
}
|
||||
|
||||
PatKind::Struct(_, ref fields, _) => {
|
||||
let pat_ty = self.tcx.node_id_to_type(pat.id);
|
||||
let adt_def = match pat_ty.sty {
|
||||
ty::TyAdt(adt_def, _) => adt_def,
|
||||
_ => {
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"struct pattern not applied to an ADT");
|
||||
}
|
||||
};
|
||||
let variant_def = adt_def.variant_of_def(self.tcx.expect_def(pat.id));
|
||||
|
||||
let subpatterns =
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
let index = variant_def.index_of_field_named(field.node.name);
|
||||
let index = index.unwrap_or_else(|| {
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"no field with name {:?}",
|
||||
field.node.name);
|
||||
});
|
||||
FieldPattern {
|
||||
field: Field::new(index),
|
||||
pattern: self.lower_pattern(&field.node.pat),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.lower_variant_or_leaf(pat, subpatterns)
|
||||
}
|
||||
};
|
||||
|
||||
Pattern {
|
||||
span: pat.span,
|
||||
ty: ty,
|
||||
kind: Box::new(kind),
|
||||
}
|
||||
}
|
||||
|
||||
fn lower_patterns(&mut self, pats: &[P<hir::Pat>]) -> Vec<Pattern<'tcx>> {
|
||||
pats.iter().map(|p| self.lower_pattern(p)).collect()
|
||||
}
|
||||
|
||||
fn lower_opt_pattern(&mut self, pat: &Option<P<hir::Pat>>) -> Option<Pattern<'tcx>>
|
||||
{
|
||||
pat.as_ref().map(|p| self.lower_pattern(p))
|
||||
}
|
||||
|
||||
fn flatten_nested_slice_patterns(
|
||||
&mut self,
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
suffix: Vec<Pattern<'tcx>>)
|
||||
-> (Vec<Pattern<'tcx>>, Option<Pattern<'tcx>>, Vec<Pattern<'tcx>>)
|
||||
{
|
||||
let orig_slice = match slice {
|
||||
Some(orig_slice) => orig_slice,
|
||||
None => return (prefix, slice, suffix)
|
||||
};
|
||||
let orig_prefix = prefix;
|
||||
let orig_suffix = suffix;
|
||||
|
||||
// dance because of intentional borrow-checker stupidity.
|
||||
let kind = *orig_slice.kind;
|
||||
match kind {
|
||||
PatternKind::Slice { prefix, slice, mut suffix } |
|
||||
PatternKind::Array { prefix, slice, mut suffix } => {
|
||||
let mut orig_prefix = orig_prefix;
|
||||
|
||||
orig_prefix.extend(prefix);
|
||||
suffix.extend(orig_suffix);
|
||||
|
||||
(orig_prefix, slice, suffix)
|
||||
}
|
||||
_ => {
|
||||
(orig_prefix, Some(Pattern {
|
||||
kind: box kind, ..orig_slice
|
||||
}), orig_suffix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn slice_or_array_pattern(
|
||||
&mut self,
|
||||
span: Span,
|
||||
ty: Ty<'tcx>,
|
||||
prefix: &[P<hir::Pat>],
|
||||
slice: &Option<P<hir::Pat>>,
|
||||
suffix: &[P<hir::Pat>])
|
||||
-> PatternKind<'tcx>
|
||||
{
|
||||
let prefix = self.lower_patterns(prefix);
|
||||
let slice = self.lower_opt_pattern(slice);
|
||||
let suffix = self.lower_patterns(suffix);
|
||||
let (prefix, slice, suffix) =
|
||||
self.flatten_nested_slice_patterns(prefix, slice, suffix);
|
||||
|
||||
match ty.sty {
|
||||
ty::TySlice(..) => {
|
||||
// matching a slice or fixed-length array
|
||||
PatternKind::Slice { prefix: prefix, slice: slice, suffix: suffix }
|
||||
}
|
||||
|
||||
ty::TyArray(_, len) => {
|
||||
// fixed-length array
|
||||
assert!(len >= prefix.len() + suffix.len());
|
||||
PatternKind::Array { prefix: prefix, slice: slice, suffix: suffix }
|
||||
}
|
||||
|
||||
_ => {
|
||||
span_bug!(span, "bad slice pattern type {:?}", ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lower_variant_or_leaf(
|
||||
&mut self,
|
||||
pat: &hir::Pat,
|
||||
subpatterns: Vec<FieldPattern<'tcx>>)
|
||||
-> PatternKind<'tcx>
|
||||
{
|
||||
match self.tcx.expect_def(pat.id) {
|
||||
Def::Variant(variant_id) | Def::VariantCtor(variant_id, ..) => {
|
||||
let enum_id = self.tcx.parent_def_id(variant_id).unwrap();
|
||||
let adt_def = self.tcx.lookup_adt_def(enum_id);
|
||||
if adt_def.variants.len() > 1 {
|
||||
PatternKind::Variant {
|
||||
adt_def: adt_def,
|
||||
variant_index: adt_def.variant_index_with_id(variant_id),
|
||||
subpatterns: subpatterns,
|
||||
}
|
||||
} else {
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
}
|
||||
|
||||
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
|
||||
Def::TyAlias(..) | Def::AssociatedTy(..) => {
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
|
||||
def => {
|
||||
span_bug!(pat.span, "inappropriate def for pattern: {:?}", def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PatternFoldable<'tcx> : Sized {
|
||||
fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
self.super_fold_with(folder)
|
||||
}
|
||||
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self;
|
||||
}
|
||||
|
||||
pub trait PatternFolder<'tcx> : Sized {
|
||||
fn fold_pattern(&mut self, pattern: &Pattern<'tcx>) -> Pattern<'tcx> {
|
||||
pattern.super_fold_with(self)
|
||||
}
|
||||
|
||||
fn fold_pattern_kind(&mut self, kind: &PatternKind<'tcx>) -> PatternKind<'tcx> {
|
||||
kind.super_fold_with(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Box<T> {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
let content: T = (**self).fold_with(folder);
|
||||
box content
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Vec<T> {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
self.iter().map(|t| t.fold_with(folder)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Option<T> {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self{
|
||||
self.as_ref().map(|t| t.fold_with(folder))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! CopyImpls {
|
||||
($($ty:ty),+) => {
|
||||
$(
|
||||
impl<'tcx> PatternFoldable<'tcx> for $ty {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, _: &mut F) -> Self {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! TcxCopyImpls {
|
||||
($($ty:ident),+) => {
|
||||
$(
|
||||
impl<'tcx> PatternFoldable<'tcx> for $ty<'tcx> {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, _: &mut F) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
CopyImpls!{ Span, Field, Mutability, ast::Name, ast::NodeId, usize, ConstVal }
|
||||
TcxCopyImpls!{ Ty, BindingMode, AdtDef }
|
||||
|
||||
impl<'tcx> PatternFoldable<'tcx> for FieldPattern<'tcx> {
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
FieldPattern {
|
||||
field: self.field.fold_with(folder),
|
||||
pattern: self.pattern.fold_with(folder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> PatternFoldable<'tcx> for Pattern<'tcx> {
|
||||
fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
folder.fold_pattern(self)
|
||||
}
|
||||
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
Pattern {
|
||||
ty: self.ty.fold_with(folder),
|
||||
span: self.span.fold_with(folder),
|
||||
kind: self.kind.fold_with(folder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> {
|
||||
fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
folder.fold_pattern_kind(self)
|
||||
}
|
||||
|
||||
fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
|
||||
match *self {
|
||||
PatternKind::Wild => PatternKind::Wild,
|
||||
PatternKind::Binding {
|
||||
mutability,
|
||||
name,
|
||||
mode,
|
||||
var,
|
||||
ty,
|
||||
ref subpattern,
|
||||
} => PatternKind::Binding {
|
||||
mutability: mutability.fold_with(folder),
|
||||
name: name.fold_with(folder),
|
||||
mode: mode.fold_with(folder),
|
||||
var: var.fold_with(folder),
|
||||
ty: ty.fold_with(folder),
|
||||
subpattern: subpattern.fold_with(folder),
|
||||
},
|
||||
PatternKind::Variant {
|
||||
adt_def,
|
||||
variant_index,
|
||||
ref subpatterns,
|
||||
} => PatternKind::Variant {
|
||||
adt_def: adt_def.fold_with(folder),
|
||||
variant_index: variant_index.fold_with(folder),
|
||||
subpatterns: subpatterns.fold_with(folder)
|
||||
},
|
||||
PatternKind::Leaf {
|
||||
ref subpatterns,
|
||||
} => PatternKind::Leaf {
|
||||
subpatterns: subpatterns.fold_with(folder),
|
||||
},
|
||||
PatternKind::Deref {
|
||||
ref subpattern,
|
||||
} => PatternKind::Deref {
|
||||
subpattern: subpattern.fold_with(folder),
|
||||
},
|
||||
PatternKind::Constant {
|
||||
ref value
|
||||
} => PatternKind::Constant {
|
||||
value: value.fold_with(folder)
|
||||
},
|
||||
PatternKind::Range {
|
||||
ref lo,
|
||||
ref hi
|
||||
} => PatternKind::Range {
|
||||
lo: lo.fold_with(folder),
|
||||
hi: hi.fold_with(folder)
|
||||
},
|
||||
PatternKind::Slice {
|
||||
ref prefix,
|
||||
ref slice,
|
||||
ref suffix,
|
||||
} => PatternKind::Slice {
|
||||
prefix: prefix.fold_with(folder),
|
||||
slice: slice.fold_with(folder),
|
||||
suffix: suffix.fold_with(folder)
|
||||
},
|
||||
PatternKind::Array {
|
||||
ref prefix,
|
||||
ref slice,
|
||||
ref suffix
|
||||
} => PatternKind::Array {
|
||||
prefix: prefix.fold_with(folder),
|
||||
slice: slice.fold_with(folder),
|
||||
suffix: suffix.fold_with(folder)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
@ -73,8 +73,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
||||
Test {
|
||||
span: match_pair.pattern.span,
|
||||
kind: TestKind::Range {
|
||||
lo: lo.clone(),
|
||||
hi: hi.clone(),
|
||||
lo: Literal::Value { value: lo.clone() },
|
||||
hi: Literal::Value { value: hi.clone() },
|
||||
ty: match_pair.pattern.ty.clone(),
|
||||
},
|
||||
}
|
||||
|
@ -9,6 +9,8 @@
|
||||
// except according to those terms.
|
||||
|
||||
use hair::cx::Cx;
|
||||
use hair::Pattern;
|
||||
|
||||
use rustc::middle::region::{CodeExtent, CodeExtentData, ROOT_CODE_EXTENT};
|
||||
use rustc::ty::{self, Ty};
|
||||
use rustc::mir::repr::*;
|
||||
@ -339,7 +341,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
||||
let lvalue = Lvalue::Local(Local::new(index + 1));
|
||||
|
||||
if let Some(pattern) = pattern {
|
||||
let pattern = self.hir.irrefutable_pat(pattern);
|
||||
let pattern = Pattern::from_hir(self.hir.tcx(), pattern);
|
||||
scope = self.declare_bindings(scope, ast_block.span, &pattern);
|
||||
unpack!(block = self.lvalue_into_pattern(block, pattern, &lvalue));
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
|
||||
let remainder_extent =
|
||||
cx.tcx.region_maps.lookup_code_extent(remainder_extent);
|
||||
|
||||
let pattern = cx.irrefutable_pat(&local.pat);
|
||||
let pattern = Pattern::from_hir(cx.tcx, &local.pat);
|
||||
result.push(StmtRef::Mirror(Box::new(Stmt {
|
||||
span: stmt.span,
|
||||
kind: StmtKind::Let {
|
||||
|
@ -657,7 +657,7 @@ fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
|
||||
fn convert_arm<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
|
||||
arm: &'tcx hir::Arm) -> Arm<'tcx> {
|
||||
Arm {
|
||||
patterns: arm.pats.iter().map(|p| cx.refutable_pat(p)).collect(),
|
||||
patterns: arm.pats.iter().map(|p| Pattern::from_hir(cx.tcx, p)).collect(),
|
||||
guard: arm.guard.to_ref(),
|
||||
body: arm.body.to_ref(),
|
||||
}
|
||||
|
@ -196,5 +196,4 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
|
||||
|
||||
mod block;
|
||||
mod expr;
|
||||
mod pattern;
|
||||
mod to_ref;
|
||||
|
@ -1,328 +0,0 @@
|
||||
// Copyright 2015 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 hair::*;
|
||||
use hair::cx::Cx;
|
||||
use rustc_data_structures::indexed_vec::Idx;
|
||||
use rustc_const_eval as const_eval;
|
||||
use rustc::hir::def::Def;
|
||||
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
|
||||
use rustc::ty::{self, Ty};
|
||||
use rustc::mir::repr::*;
|
||||
use rustc::hir::{self, PatKind};
|
||||
use syntax::ptr::P;
|
||||
use syntax_pos::Span;
|
||||
|
||||
/// When there are multiple patterns in a single arm, each one has its
|
||||
/// own node-ids for the bindings. References to the variables always
|
||||
/// use the node-ids from the first pattern in the arm, so we just
|
||||
/// remap the ids for all subsequent bindings to the first one.
|
||||
///
|
||||
/// Example:
|
||||
/// ```
|
||||
/// match foo {
|
||||
/// Test1(flavor /* def 1 */) |
|
||||
/// Test2(flavor /* def 2 */) if flavor /* ref 1 */.is_tasty() => { ... }
|
||||
/// _ => { ... }
|
||||
/// }
|
||||
/// ```
|
||||
struct PatCx<'patcx, 'cx: 'patcx, 'gcx: 'cx+'tcx, 'tcx: 'cx> {
|
||||
cx: &'patcx mut Cx<'cx, 'gcx, 'tcx>,
|
||||
}
|
||||
|
||||
impl<'cx, 'gcx, 'tcx> Cx<'cx, 'gcx, 'tcx> {
|
||||
pub fn irrefutable_pat(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
|
||||
PatCx::new(self).to_pattern(pat)
|
||||
}
|
||||
|
||||
pub fn refutable_pat(&mut self,
|
||||
pat: &hir::Pat)
|
||||
-> Pattern<'tcx> {
|
||||
PatCx::new(self).to_pattern(pat)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'patcx, 'cx, 'gcx, 'tcx> PatCx<'patcx, 'cx, 'gcx, 'tcx> {
|
||||
fn new(cx: &'patcx mut Cx<'cx, 'gcx, 'tcx>)
|
||||
-> PatCx<'patcx, 'cx, 'gcx, 'tcx> {
|
||||
PatCx {
|
||||
cx: cx,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_pattern(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
|
||||
let mut ty = self.cx.tcx.node_id_to_type(pat.id);
|
||||
|
||||
let kind = match pat.node {
|
||||
PatKind::Wild => PatternKind::Wild,
|
||||
|
||||
PatKind::Lit(ref value) => {
|
||||
let value = const_eval::eval_const_expr(self.cx.tcx.global_tcx(), value);
|
||||
PatternKind::Constant { value: value }
|
||||
}
|
||||
|
||||
PatKind::Range(ref lo, ref hi) => {
|
||||
let lo = const_eval::eval_const_expr(self.cx.tcx.global_tcx(), lo);
|
||||
let lo = Literal::Value { value: lo };
|
||||
let hi = const_eval::eval_const_expr(self.cx.tcx.global_tcx(), hi);
|
||||
let hi = Literal::Value { value: hi };
|
||||
PatternKind::Range { lo: lo, hi: hi }
|
||||
},
|
||||
|
||||
PatKind::Path(..) => {
|
||||
match self.cx.tcx.expect_def(pat.id) {
|
||||
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
|
||||
let tcx = self.cx.tcx.global_tcx();
|
||||
let substs = Some(self.cx.tcx.node_id_item_substs(pat.id).substs);
|
||||
match const_eval::lookup_const_by_id(tcx, def_id, substs) {
|
||||
Some((const_expr, _const_ty)) => {
|
||||
match const_eval::const_expr_to_pat(tcx,
|
||||
const_expr,
|
||||
pat.id,
|
||||
pat.span) {
|
||||
Ok(pat) =>
|
||||
return self.to_pattern(&pat),
|
||||
Err(_) =>
|
||||
span_bug!(
|
||||
pat.span, "illegal constant"),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"cannot eval constant: {:?}",
|
||||
def_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.variant_or_leaf(pat, vec![])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Ref(ref subpattern, _) |
|
||||
PatKind::Box(ref subpattern) => {
|
||||
PatternKind::Deref { subpattern: self.to_pattern(subpattern) }
|
||||
}
|
||||
|
||||
PatKind::Slice(ref prefix, ref slice, ref suffix) => {
|
||||
let ty = self.cx.tcx.node_id_to_type(pat.id);
|
||||
match ty.sty {
|
||||
ty::TyRef(_, mt) =>
|
||||
PatternKind::Deref {
|
||||
subpattern: Pattern {
|
||||
ty: mt.ty,
|
||||
span: pat.span,
|
||||
kind: Box::new(self.slice_or_array_pattern(pat.span, mt.ty, prefix,
|
||||
slice, suffix)),
|
||||
},
|
||||
},
|
||||
|
||||
ty::TySlice(..) |
|
||||
ty::TyArray(..) =>
|
||||
self.slice_or_array_pattern(pat.span, ty, prefix, slice, suffix),
|
||||
|
||||
ref sty =>
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"unexpanded type for vector pattern: {:?}",
|
||||
sty),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Tuple(ref subpatterns, ddpos) => {
|
||||
match self.cx.tcx.node_id_to_type(pat.id).sty {
|
||||
ty::TyTuple(ref tys) => {
|
||||
let subpatterns =
|
||||
subpatterns.iter()
|
||||
.enumerate_and_adjust(tys.len(), ddpos)
|
||||
.map(|(i, subpattern)| FieldPattern {
|
||||
field: Field::new(i),
|
||||
pattern: self.to_pattern(subpattern),
|
||||
})
|
||||
.collect();
|
||||
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
|
||||
ref sty => span_bug!(pat.span, "unexpected type for tuple pattern: {:?}", sty),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::Binding(bm, ref ident, ref sub) => {
|
||||
let def_id = self.cx.tcx.expect_def(pat.id).def_id();
|
||||
let id = self.cx.tcx.map.as_local_node_id(def_id).unwrap();
|
||||
let var_ty = self.cx.tcx.node_id_to_type(pat.id);
|
||||
let region = match var_ty.sty {
|
||||
ty::TyRef(r, _) => Some(r),
|
||||
_ => None,
|
||||
};
|
||||
let (mutability, mode) = match bm {
|
||||
hir::BindByValue(hir::MutMutable) =>
|
||||
(Mutability::Mut, BindingMode::ByValue),
|
||||
hir::BindByValue(hir::MutImmutable) =>
|
||||
(Mutability::Not, BindingMode::ByValue),
|
||||
hir::BindByRef(hir::MutMutable) =>
|
||||
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Mut)),
|
||||
hir::BindByRef(hir::MutImmutable) =>
|
||||
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Shared)),
|
||||
};
|
||||
|
||||
// A ref x pattern is the same node used for x, and as such it has
|
||||
// x's type, which is &T, where we want T (the type being matched).
|
||||
if let hir::BindByRef(_) = bm {
|
||||
if let ty::TyRef(_, mt) = ty.sty {
|
||||
ty = mt.ty;
|
||||
} else {
|
||||
bug!("`ref {}` has wrong type {}", ident.node, ty);
|
||||
}
|
||||
}
|
||||
|
||||
PatternKind::Binding {
|
||||
mutability: mutability,
|
||||
mode: mode,
|
||||
name: ident.node,
|
||||
var: id,
|
||||
ty: var_ty,
|
||||
subpattern: self.to_opt_pattern(sub),
|
||||
}
|
||||
}
|
||||
|
||||
PatKind::TupleStruct(_, ref subpatterns, ddpos) => {
|
||||
let pat_ty = self.cx.tcx.node_id_to_type(pat.id);
|
||||
let adt_def = match pat_ty.sty {
|
||||
ty::TyAdt(adt_def, _) => adt_def,
|
||||
_ => span_bug!(pat.span, "tuple struct pattern not applied to an ADT"),
|
||||
};
|
||||
let variant_def = adt_def.variant_of_def(self.cx.tcx.expect_def(pat.id));
|
||||
|
||||
let subpatterns =
|
||||
subpatterns.iter()
|
||||
.enumerate_and_adjust(variant_def.fields.len(), ddpos)
|
||||
.map(|(i, field)| FieldPattern {
|
||||
field: Field::new(i),
|
||||
pattern: self.to_pattern(field),
|
||||
})
|
||||
.collect();
|
||||
self.variant_or_leaf(pat, subpatterns)
|
||||
}
|
||||
|
||||
PatKind::Struct(_, ref fields, _) => {
|
||||
let pat_ty = self.cx.tcx.node_id_to_type(pat.id);
|
||||
let adt_def = match pat_ty.sty {
|
||||
ty::TyAdt(adt_def, _) => adt_def,
|
||||
_ => {
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"struct pattern not applied to an ADT");
|
||||
}
|
||||
};
|
||||
let variant_def = adt_def.variant_of_def(self.cx.tcx.expect_def(pat.id));
|
||||
|
||||
let subpatterns =
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
let index = variant_def.index_of_field_named(field.node.name);
|
||||
let index = index.unwrap_or_else(|| {
|
||||
span_bug!(
|
||||
pat.span,
|
||||
"no field with name {:?}",
|
||||
field.node.name);
|
||||
});
|
||||
FieldPattern {
|
||||
field: Field::new(index),
|
||||
pattern: self.to_pattern(&field.node.pat),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.variant_or_leaf(pat, subpatterns)
|
||||
}
|
||||
};
|
||||
|
||||
Pattern {
|
||||
span: pat.span,
|
||||
ty: ty,
|
||||
kind: Box::new(kind),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_patterns(&mut self, pats: &[P<hir::Pat>]) -> Vec<Pattern<'tcx>> {
|
||||
pats.iter().map(|p| self.to_pattern(p)).collect()
|
||||
}
|
||||
|
||||
fn to_opt_pattern(&mut self, pat: &Option<P<hir::Pat>>) -> Option<Pattern<'tcx>> {
|
||||
pat.as_ref().map(|p| self.to_pattern(p))
|
||||
}
|
||||
|
||||
fn slice_or_array_pattern(&mut self,
|
||||
span: Span,
|
||||
ty: Ty<'tcx>,
|
||||
prefix: &[P<hir::Pat>],
|
||||
slice: &Option<P<hir::Pat>>,
|
||||
suffix: &[P<hir::Pat>])
|
||||
-> PatternKind<'tcx> {
|
||||
match ty.sty {
|
||||
ty::TySlice(..) => {
|
||||
// matching a slice or fixed-length array
|
||||
PatternKind::Slice {
|
||||
prefix: self.to_patterns(prefix),
|
||||
slice: self.to_opt_pattern(slice),
|
||||
suffix: self.to_patterns(suffix),
|
||||
}
|
||||
}
|
||||
|
||||
ty::TyArray(_, len) => {
|
||||
// fixed-length array
|
||||
assert!(len >= prefix.len() + suffix.len());
|
||||
PatternKind::Array {
|
||||
prefix: self.to_patterns(prefix),
|
||||
slice: self.to_opt_pattern(slice),
|
||||
suffix: self.to_patterns(suffix),
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
span_bug!(span, "unexpanded macro or bad constant etc");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn variant_or_leaf(&mut self,
|
||||
pat: &hir::Pat,
|
||||
subpatterns: Vec<FieldPattern<'tcx>>)
|
||||
-> PatternKind<'tcx> {
|
||||
match self.cx.tcx.expect_def(pat.id) {
|
||||
Def::Variant(variant_id) | Def::VariantCtor(variant_id, ..) => {
|
||||
let enum_id = self.cx.tcx.parent_def_id(variant_id).unwrap();
|
||||
let adt_def = self.cx.tcx.lookup_adt_def(enum_id);
|
||||
if adt_def.variants.len() > 1 {
|
||||
PatternKind::Variant {
|
||||
adt_def: adt_def,
|
||||
variant_index: adt_def.variant_index_with_id(variant_id),
|
||||
subpatterns: subpatterns,
|
||||
}
|
||||
} else {
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
}
|
||||
|
||||
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
|
||||
Def::TyAlias(..) | Def::AssociatedTy(..) => {
|
||||
PatternKind::Leaf { subpatterns: subpatterns }
|
||||
}
|
||||
|
||||
def => {
|
||||
span_bug!(pat.span, "inappropriate def for pattern: {:?}", def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -14,9 +14,7 @@
|
||||
//! unit-tested and separated from the Rust source and compiler data
|
||||
//! structures.
|
||||
|
||||
use rustc::mir::repr::{BinOp, BorrowKind, Field, Literal, Mutability, UnOp,
|
||||
TypedConstVal};
|
||||
use rustc::middle::const_val::ConstVal;
|
||||
use rustc::mir::repr::{BinOp, BorrowKind, Field, Literal, UnOp, TypedConstVal};
|
||||
use rustc::hir::def_id::DefId;
|
||||
use rustc::middle::region::CodeExtent;
|
||||
use rustc::ty::subst::Substs;
|
||||
@ -28,6 +26,8 @@ use self::cx::Cx;
|
||||
|
||||
pub mod cx;
|
||||
|
||||
pub use rustc_const_eval::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Block<'tcx> {
|
||||
pub extent: CodeExtent,
|
||||
@ -266,86 +266,12 @@ pub struct Arm<'tcx> {
|
||||
pub body: ExprRef<'tcx>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Pattern<'tcx> {
|
||||
pub ty: Ty<'tcx>,
|
||||
pub span: Span,
|
||||
pub kind: Box<PatternKind<'tcx>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum LogicalOp {
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum PatternKind<'tcx> {
|
||||
Wild,
|
||||
|
||||
/// x, ref x, x @ P, etc
|
||||
Binding {
|
||||
mutability: Mutability,
|
||||
name: ast::Name,
|
||||
mode: BindingMode<'tcx>,
|
||||
var: ast::NodeId,
|
||||
ty: Ty<'tcx>,
|
||||
subpattern: Option<Pattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// Foo(...) or Foo{...} or Foo, where `Foo` is a variant name from an adt with >1 variants
|
||||
Variant {
|
||||
adt_def: AdtDef<'tcx>,
|
||||
variant_index: usize,
|
||||
subpatterns: Vec<FieldPattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// (...), Foo(...), Foo{...}, or Foo, where `Foo` is a variant name from an adt with 1 variant
|
||||
Leaf {
|
||||
subpatterns: Vec<FieldPattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// box P, &P, &mut P, etc
|
||||
Deref {
|
||||
subpattern: Pattern<'tcx>,
|
||||
},
|
||||
|
||||
Constant {
|
||||
value: ConstVal,
|
||||
},
|
||||
|
||||
Range {
|
||||
lo: Literal<'tcx>,
|
||||
hi: Literal<'tcx>,
|
||||
},
|
||||
|
||||
/// matches against a slice, checking the length and extracting elements
|
||||
Slice {
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
suffix: Vec<Pattern<'tcx>>,
|
||||
},
|
||||
|
||||
/// fixed match against an array, irrefutable
|
||||
Array {
|
||||
prefix: Vec<Pattern<'tcx>>,
|
||||
slice: Option<Pattern<'tcx>>,
|
||||
suffix: Vec<Pattern<'tcx>>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum BindingMode<'tcx> {
|
||||
ByValue,
|
||||
ByRef(&'tcx Region, BorrowKind),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FieldPattern<'tcx> {
|
||||
pub field: Field,
|
||||
pub pattern: Pattern<'tcx>,
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// The Mirror trait
|
||||
|
||||
|
@ -21,10 +21,6 @@ const NEG_128: i8 = -128;
|
||||
const NEG_NEG_128: i8 = -NEG_128;
|
||||
//~^ ERROR constant evaluation error
|
||||
//~| attempt to negate with overflow
|
||||
//~| ERROR constant evaluation error
|
||||
//~| attempt to negate with overflow
|
||||
//~| ERROR constant evaluation error
|
||||
//~| attempt to negate with overflow
|
||||
|
||||
fn main() {
|
||||
match -128i8 {
|
||||
|
@ -25,13 +25,11 @@ const fn foo() -> Cake {
|
||||
Marmor
|
||||
//~^ ERROR: constant evaluation error [E0080]
|
||||
//~| unimplemented constant expression: enum variants
|
||||
//~^^^ ERROR: constant evaluation error [E0080]
|
||||
//~| unimplemented constant expression: enum variants
|
||||
}
|
||||
|
||||
const WORKS: Cake = Marmor;
|
||||
|
||||
const GOO: Cake = foo(); //~ NOTE for expression here
|
||||
const GOO: Cake = foo();
|
||||
|
||||
fn main() {
|
||||
match BlackForest {
|
||||
|
16
src/test/compile-fail/issue-26158.rs
Normal file
16
src/test/compile-fail/issue-26158.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 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.
|
||||
|
||||
#![feature(slice_patterns)]
|
||||
|
||||
fn main() {
|
||||
let x: &[u32] = &[];
|
||||
let &[[ref _a, ref _b..]..] = x; //~ ERROR refutable pattern
|
||||
}
|
@ -11,26 +11,24 @@
|
||||
#![feature(rustc_attrs)]
|
||||
#![feature(slice_patterns)]
|
||||
#![allow(dead_code)]
|
||||
#![deny(illegal_floating_point_constant_pattern)]
|
||||
|
||||
// Matching against NaN should result in a warning
|
||||
|
||||
use std::f64::NAN;
|
||||
|
||||
#[rustc_error]
|
||||
fn main() { //~ ERROR compilation successful
|
||||
fn main() {
|
||||
let x = NAN;
|
||||
match x {
|
||||
NAN => {},
|
||||
NAN => {}, //~ ERROR floating point constants cannot be used
|
||||
//~| WARNING hard error
|
||||
_ => {},
|
||||
};
|
||||
//~^^^ WARNING unmatchable NaN in pattern, use the is_nan method in a guard instead
|
||||
//~| WARNING floating point constants cannot be used
|
||||
//~| WARNING this was previously accepted
|
||||
|
||||
match [x, 1.0] {
|
||||
[NAN, _] => {},
|
||||
[NAN, _] => {}, //~ ERROR floating point constants cannot be used
|
||||
//~| WARNING hard error
|
||||
_ => {},
|
||||
};
|
||||
//~^^^ WARNING unmatchable NaN in pattern, use the is_nan method in a guard instead
|
||||
//~| WARNING floating point constants cannot be used
|
||||
//~| WARNING this was previously accepted
|
||||
}
|
||||
|
73
src/test/compile-fail/match-byte-array-patterns.rs
Normal file
73
src/test/compile-fail/match-byte-array-patterns.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// Copyright 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.
|
||||
|
||||
#![feature(advanced_slice_patterns, slice_patterns)]
|
||||
|
||||
fn main() {
|
||||
let buf = &[0, 1, 2, 3];
|
||||
|
||||
match buf {
|
||||
b"AAAA" => {},
|
||||
&[0x41, 0x41, 0x41, 0x41] => {} //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[0x41, 0x41, 0x41, 0x41] => {}
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[_, 0x41, 0x41, 0x41] => {},
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[0x41, .., 0x41] => {}
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf { //~ ERROR non-exhaustive
|
||||
b"AAAA" => {}
|
||||
}
|
||||
|
||||
let buf: &[u8] = buf;
|
||||
|
||||
match buf {
|
||||
b"AAAA" => {},
|
||||
&[0x41, 0x41, 0x41, 0x41] => {} //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[0x41, 0x41, 0x41, 0x41] => {}
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[_, 0x41, 0x41, 0x41] => {},
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
&[0x41, .., 0x41] => {}
|
||||
b"AAAA" => {}, //~ ERROR unreachable pattern
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf { //~ ERROR non-exhaustive
|
||||
b"AAAA" => {}
|
||||
}
|
||||
}
|
54
src/test/run-pass/match-byte-array-patterns.rs
Normal file
54
src/test/run-pass/match-byte-array-patterns.rs
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright 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.
|
||||
|
||||
#![feature(slice_patterns)]
|
||||
|
||||
fn main() {
|
||||
let buf = &[0u8; 4];
|
||||
match buf {
|
||||
&[0, 1, 0, 0] => unimplemented!(),
|
||||
b"true" => unimplemented!(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
b"true" => unimplemented!(),
|
||||
&[0, 1, 0, 0] => unimplemented!(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
b"true" => unimplemented!(),
|
||||
&[0, x, 0, 0] => assert_eq!(x, 0),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
|
||||
let buf: &[u8] = buf;
|
||||
|
||||
match buf {
|
||||
&[0, 1, 0, 0] => unimplemented!(),
|
||||
&[_] => unimplemented!(),
|
||||
&[_, _, _, _, _, ..] => unimplemented!(),
|
||||
b"true" => unimplemented!(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
b"true" => unimplemented!(),
|
||||
&[0, 1, 0, 0] => unimplemented!(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match buf {
|
||||
b"true" => unimplemented!(),
|
||||
&[0, x, 0, 0] => assert_eq!(x, 0),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
@ -144,6 +144,20 @@ fn e() {
|
||||
assert_eq!(c, 1);
|
||||
}
|
||||
|
||||
fn f() {
|
||||
let x = &[1, 2, 3, 4, 5];
|
||||
let [a, [b, [c, ..].., d].., e] = *x;
|
||||
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5));
|
||||
|
||||
let x: &[isize] = x;
|
||||
let (a, b, c, d, e) = match *x {
|
||||
[a, [b, [c, ..].., d].., e] => (a, b, c, d, e),
|
||||
_ => unimplemented!()
|
||||
};
|
||||
|
||||
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
a();
|
||||
b();
|
||||
@ -151,4 +165,5 @@ pub fn main() {
|
||||
c();
|
||||
d();
|
||||
e();
|
||||
f();
|
||||
}
|
||||
|
53
src/test/ui/check_match/issue-35609.rs
Normal file
53
src/test/ui/check_match/issue-35609.rs
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright 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.
|
||||
|
||||
enum Enum {
|
||||
A, B, C, D, E, F
|
||||
}
|
||||
use Enum::*;
|
||||
|
||||
struct S(Enum, ());
|
||||
struct Sd { x: Enum, y: () }
|
||||
|
||||
fn main() {
|
||||
match (A, ()) {
|
||||
(A, _) => {}
|
||||
}
|
||||
|
||||
match (A, A) {
|
||||
(_, A) => {}
|
||||
}
|
||||
|
||||
match ((A, ()), ()) {
|
||||
((A, ()), _) => {}
|
||||
}
|
||||
|
||||
match ((A, ()), A) {
|
||||
((A, ()), _) => {}
|
||||
}
|
||||
|
||||
match ((A, ()), ()) {
|
||||
((A, _), _) => {}
|
||||
}
|
||||
|
||||
|
||||
match S(A, ()) {
|
||||
S(A, _) => {}
|
||||
}
|
||||
|
||||
match (Sd { x: A, y: () }) {
|
||||
Sd { x: A, y: _ } => {}
|
||||
}
|
||||
|
||||
match Some(A) {
|
||||
Some(A) => (),
|
||||
None => ()
|
||||
}
|
||||
}
|
50
src/test/ui/check_match/issue-35609.stderr
Normal file
50
src/test/ui/check_match/issue-35609.stderr
Normal file
@ -0,0 +1,50 @@
|
||||
error[E0004]: non-exhaustive patterns: `(B, _)`, `(C, _)`, `(D, _)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:20:11
|
||||
|
|
||||
20 | match (A, ()) {
|
||||
| ^^^^^^^ patterns `(B, _)`, `(C, _)`, `(D, _)` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `(A, B)`, `(B, B)`, `(C, B)` and 27 more not covered
|
||||
--> $DIR/issue-35609.rs:24:11
|
||||
|
|
||||
24 | match (A, A) {
|
||||
| ^^^^^^ patterns `(A, B)`, `(B, B)`, `(C, B)` and 27 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:28:11
|
||||
|
|
||||
28 | match ((A, ()), ()) {
|
||||
| ^^^^^^^^^^^^^ patterns `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:32:11
|
||||
|
|
||||
32 | match ((A, ()), A) {
|
||||
| ^^^^^^^^^^^^ patterns `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:36:11
|
||||
|
|
||||
36 | match ((A, ()), ()) {
|
||||
| ^^^^^^^^^^^^^ patterns `((B, _), _)`, `((C, _), _)`, `((D, _), _)` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `S(B, _)`, `S(C, _)`, `S(D, _)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:41:11
|
||||
|
|
||||
41 | match S(A, ()) {
|
||||
| ^^^^^^^^ patterns `S(B, _)`, `S(C, _)`, `S(D, _)` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Sd { x: B, .. }`, `Sd { x: C, .. }`, `Sd { x: D, .. }` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:45:11
|
||||
|
|
||||
45 | match (Sd { x: A, y: () }) {
|
||||
| ^^^^^^^^^^^^^^^^^^^^ patterns `Sd { x: B, .. }`, `Sd { x: C, .. }`, `Sd { x: D, .. }` and 2 more not covered
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Some(B)`, `Some(C)`, `Some(D)` and 2 more not covered
|
||||
--> $DIR/issue-35609.rs:49:11
|
||||
|
|
||||
49 | match Some(A) {
|
||||
| ^^^^^^^ patterns `Some(B)`, `Some(C)`, `Some(D)` and 2 more not covered
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user