2016-09-24 17:45:24 +03:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
2015-08-18 17:59:21 -04:00
|
|
|
// 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.
|
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
use eval;
|
|
|
|
|
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::{Field, BorrowKind, Mutability};
|
2016-09-24 17:45:24 +03:00
|
|
|
use rustc::ty::{self, TyCtxt, AdtDef, Ty, Region};
|
|
|
|
use rustc::hir::{self, PatKind};
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def::Def;
|
2016-10-03 21:39:21 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-06-11 18:47:47 +03:00
|
|
|
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
|
2016-09-24 17:45:24 +03:00
|
|
|
|
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
|
|
|
|
use syntax::ast;
|
2015-10-05 12:31:48 -04:00
|
|
|
use syntax::ptr::P;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum PatternError {
|
|
|
|
StaticInPattern(Span),
|
|
|
|
BadConstInPattern(Span, DefId),
|
|
|
|
ConstEval(eval::ConstEvalErr),
|
|
|
|
}
|
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum BindingMode<'tcx> {
|
|
|
|
ByValue,
|
|
|
|
ByRef(&'tcx Region, BorrowKind),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct FieldPattern<'tcx> {
|
|
|
|
pub field: Field,
|
|
|
|
pub pattern: Pattern<'tcx>,
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Pattern<'tcx> {
|
|
|
|
pub ty: Ty<'tcx>,
|
|
|
|
pub span: Span,
|
|
|
|
pub kind: Box<PatternKind<'tcx>>,
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
#[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 {
|
2016-09-26 02:53:26 +03:00
|
|
|
lo: ConstVal,
|
|
|
|
hi: ConstVal,
|
2016-09-24 17:45:24 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/// 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>>,
|
|
|
|
},
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
pub struct PatternContext<'a, 'gcx: 'tcx, 'tcx: 'a> {
|
|
|
|
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
pub errors: Vec<PatternError>,
|
|
|
|
}
|
|
|
|
|
2016-09-24 17:45:24 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Pattern<'tcx> {
|
|
|
|
pub fn from_hir(tcx: TyCtxt<'a, 'gcx, 'tcx>, pat: &hir::Pat) -> Self {
|
2016-10-03 21:39:21 +03:00
|
|
|
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)
|
|
|
|
}
|
2016-10-26 23:52:03 +03:00
|
|
|
debug!("Pattern::from_hir({:?}) = {:?}", pat, result);
|
2016-10-03 21:39:21 +03:00
|
|
|
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> {
|
2016-10-27 04:52:10 +03:00
|
|
|
let mut ty = self.tcx.tables().node_id_to_type(pat.id);
|
2016-03-09 23:32:52 +02:00
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
let kind = match pat.node {
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Wild => PatternKind::Wild,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Lit(ref value) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 14:37:42 +02:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Range(ref lo, ref hi) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
PatKind::Path(ref qpath) => {
|
|
|
|
let def = self.tcx.tables().qpath_def(qpath, pat.id);
|
|
|
|
match def {
|
2016-03-10 02:04:55 +02:00
|
|
|
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
let tcx = self.tcx.global_tcx();
|
2016-10-27 04:52:10 +03:00
|
|
|
let substs = tcx.tables().node_id_item_substs(pat.id)
|
|
|
|
.unwrap_or_else(|| tcx.intern_substs(&[]));
|
|
|
|
match eval::lookup_const_by_id(tcx, def_id, Some(substs)) {
|
2016-03-03 14:48:08 +01:00
|
|
|
Some((const_expr, _const_ty)) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
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
|
|
|
|
}
|
2016-02-03 16:38:48 -05:00
|
|
|
}
|
2015-09-10 15:44:44 -04:00
|
|
|
}
|
|
|
|
None => {
|
2016-10-03 21:39:21 +03:00
|
|
|
self.errors.push(PatternError::StaticInPattern(pat.span));
|
|
|
|
PatternKind::Wild
|
2015-09-10 15:44:44 -04:00
|
|
|
}
|
2016-03-10 02:04:55 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
_ => self.lower_variant_or_leaf(def, vec![])
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Ref(ref subpattern, _) |
|
|
|
|
PatKind::Box(ref subpattern) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
PatternKind::Deref { subpattern: self.lower_pattern(subpattern) }
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-09-20 02:14:46 +02:00
|
|
|
PatKind::Slice(ref prefix, ref slice, ref suffix) => {
|
2016-10-27 04:52:10 +03:00
|
|
|
let ty = self.tcx.tables().node_id_to_type(pat.id);
|
2015-08-18 17:59:21 -04:00
|
|
|
match ty.sty {
|
|
|
|
ty::TyRef(_, mt) =>
|
|
|
|
PatternKind::Deref {
|
|
|
|
subpattern: Pattern {
|
|
|
|
ty: mt.ty,
|
2015-11-09 20:54:17 -05:00
|
|
|
span: pat.span,
|
2016-10-03 21:39:21 +03:00
|
|
|
kind: Box::new(self.slice_or_array_pattern(
|
|
|
|
pat.span, mt.ty, prefix, slice, suffix))
|
2015-11-09 20:54:17 -05:00
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
ty::TySlice(..) |
|
|
|
|
ty::TyArray(..) =>
|
2016-10-03 21:39:21 +03:00
|
|
|
self.slice_or_array_pattern(pat.span, ty, prefix, slice, suffix),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
ref sty =>
|
2016-03-28 22:22:14 +02:00
|
|
|
span_bug!(
|
2015-11-09 20:54:17 -05:00
|
|
|
pat.span,
|
2016-03-28 22:22:14 +02:00
|
|
|
"unexpanded type for vector pattern: {:?}",
|
|
|
|
sty),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
PatKind::Tuple(ref subpatterns, ddpos) => {
|
2016-11-10 13:09:34 +01:00
|
|
|
let ty = self.tcx.tables().node_id_to_type(pat.id);
|
|
|
|
match ty.sty {
|
2016-03-06 15:54:44 +03:00
|
|
|
ty::TyTuple(ref tys) => {
|
|
|
|
let subpatterns =
|
|
|
|
subpatterns.iter()
|
2016-03-06 15:54:44 +03:00
|
|
|
.enumerate_and_adjust(tys.len(), ddpos)
|
2016-03-06 15:54:44 +03:00
|
|
|
.map(|(i, subpattern)| FieldPattern {
|
2016-03-06 15:54:44 +03:00
|
|
|
field: Field::new(i),
|
2016-10-03 21:39:21 +03:00
|
|
|
pattern: self.lower_pattern(subpattern)
|
2016-03-06 15:54:44 +03:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
PatternKind::Leaf { subpatterns: subpatterns }
|
|
|
|
}
|
|
|
|
|
|
|
|
ref sty => span_bug!(pat.span, "unexpected type for tuple pattern: {:?}", sty),
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
PatKind::Binding(bm, def_id, ref ident, ref sub) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
let id = self.tcx.map.as_local_node_id(def_id).unwrap();
|
2016-10-27 04:52:10 +03:00
|
|
|
let var_ty = self.tcx.tables().node_id_to_type(pat.id);
|
2015-08-18 17:59:21 -04:00
|
|
|
let region = match var_ty.sty {
|
2016-08-25 23:58:52 +03:00
|
|
|
ty::TyRef(r, _) => Some(r),
|
2015-08-18 17:59:21 -04:00
|
|
|
_ => 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)),
|
|
|
|
};
|
2016-03-09 23:32:52 +02:00
|
|
|
|
|
|
|
// 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 {
|
2016-03-28 22:22:14 +02:00
|
|
|
bug!("`ref {}` has wrong type {}", ident.node, ty);
|
2016-03-09 23:32:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
PatternKind::Binding {
|
|
|
|
mutability: mutability,
|
|
|
|
mode: mode,
|
2016-03-06 15:54:44 +03:00
|
|
|
name: ident.node,
|
2015-08-18 17:59:21 -04:00
|
|
|
var: id,
|
|
|
|
ty: var_ty,
|
2016-10-03 21:39:21 +03:00
|
|
|
subpattern: self.lower_opt_pattern(sub),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
PatKind::TupleStruct(ref qpath, ref subpatterns, ddpos) => {
|
|
|
|
let def = self.tcx.tables().qpath_def(qpath, pat.id);
|
2016-10-27 04:52:10 +03:00
|
|
|
let pat_ty = self.tcx.tables().node_id_to_type(pat.id);
|
2016-03-06 15:54:44 +03:00
|
|
|
let adt_def = match pat_ty.sty {
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(adt_def, _) => adt_def,
|
|
|
|
_ => span_bug!(pat.span, "tuple struct pattern not applied to an ADT"),
|
2016-03-06 15:54:44 +03:00
|
|
|
};
|
2016-11-25 13:21:19 +02:00
|
|
|
let variant_def = adt_def.variant_of_def(def);
|
2016-03-06 15:54:44 +03:00
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
let subpatterns =
|
2016-03-06 15:54:44 +03:00
|
|
|
subpatterns.iter()
|
2016-03-06 15:54:44 +03:00
|
|
|
.enumerate_and_adjust(variant_def.fields.len(), ddpos)
|
2015-11-09 20:54:17 -05:00
|
|
|
.map(|(i, field)| FieldPattern {
|
2016-03-06 15:54:44 +03:00
|
|
|
field: Field::new(i),
|
2016-10-03 21:39:21 +03:00
|
|
|
pattern: self.lower_pattern(field),
|
2015-08-18 17:59:21 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2016-11-25 13:21:19 +02:00
|
|
|
self.lower_variant_or_leaf(def, subpatterns)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
PatKind::Struct(ref qpath, ref fields, _) => {
|
|
|
|
let def = self.tcx.tables().qpath_def(qpath, pat.id);
|
2016-10-27 04:52:10 +03:00
|
|
|
let pat_ty = self.tcx.tables().node_id_to_type(pat.id);
|
2015-10-21 17:24:05 -04:00
|
|
|
let adt_def = match pat_ty.sty {
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(adt_def, _) => adt_def,
|
2015-10-21 17:24:05 -04:00
|
|
|
_ => {
|
2016-03-28 22:22:14 +02:00
|
|
|
span_bug!(
|
2015-11-09 20:54:17 -05:00
|
|
|
pat.span,
|
2016-09-06 01:26:02 +03:00
|
|
|
"struct pattern not applied to an ADT");
|
2015-10-21 17:24:05 -04:00
|
|
|
}
|
|
|
|
};
|
2016-11-25 13:21:19 +02:00
|
|
|
let variant_def = adt_def.variant_of_def(def);
|
2015-10-21 17:24:05 -04:00
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
let subpatterns =
|
|
|
|
fields.iter()
|
2015-10-21 17:24:05 -04:00
|
|
|
.map(|field| {
|
|
|
|
let index = variant_def.index_of_field_named(field.node.name);
|
|
|
|
let index = index.unwrap_or_else(|| {
|
2016-03-28 22:22:14 +02:00
|
|
|
span_bug!(
|
2015-11-09 20:54:17 -05:00
|
|
|
pat.span,
|
2016-03-28 22:22:14 +02:00
|
|
|
"no field with name {:?}",
|
|
|
|
field.node.name);
|
2015-10-21 17:24:05 -04:00
|
|
|
});
|
2015-11-09 20:54:17 -05:00
|
|
|
FieldPattern {
|
2015-10-21 17:24:05 -04:00
|
|
|
field: Field::new(index),
|
2016-10-03 21:39:21 +03:00
|
|
|
pattern: self.lower_pattern(&field.node.pat),
|
2015-10-21 17:24:05 -04:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2015-10-21 17:24:05 -04:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
self.lower_variant_or_leaf(def, subpatterns)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-07 14:37:42 +02:00
|
|
|
Pattern {
|
2015-11-09 20:54:17 -05:00
|
|
|
span: pat.span,
|
2015-10-07 14:37:42 +02:00
|
|
|
ty: ty,
|
2015-11-09 20:54:17 -05:00
|
|
|
kind: Box::new(kind),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
fn lower_patterns(&mut self, pats: &[P<hir::Pat>]) -> Vec<Pattern<'tcx>> {
|
|
|
|
pats.iter().map(|p| self.lower_pattern(p)).collect()
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
fn lower_opt_pattern(&mut self, pat: &Option<P<hir::Pat>>) -> Option<Pattern<'tcx>>
|
2016-09-24 17:45:24 +03:00
|
|
|
{
|
2016-10-03 21:39:21 +03:00
|
|
|
pat.as_ref().map(|p| self.lower_pattern(p))
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:03 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
fn slice_or_array_pattern(
|
|
|
|
&mut self,
|
2016-09-24 17:45:24 +03:00
|
|
|
span: Span,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
prefix: &[P<hir::Pat>],
|
|
|
|
slice: &Option<P<hir::Pat>>,
|
|
|
|
suffix: &[P<hir::Pat>])
|
2016-10-03 21:39:21 +03:00
|
|
|
-> PatternKind<'tcx>
|
2016-09-24 17:45:24 +03:00
|
|
|
{
|
2016-10-26 23:52:03 +03:00
|
|
|
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);
|
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
match ty.sty {
|
|
|
|
ty::TySlice(..) => {
|
|
|
|
// matching a slice or fixed-length array
|
2016-10-26 23:52:03 +03:00
|
|
|
PatternKind::Slice { prefix: prefix, slice: slice, suffix: suffix }
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyArray(_, len) => {
|
|
|
|
// fixed-length array
|
|
|
|
assert!(len >= prefix.len() + suffix.len());
|
2016-10-26 23:52:03 +03:00
|
|
|
PatternKind::Array { prefix: prefix, slice: slice, suffix: suffix }
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
2016-10-03 21:39:21 +03:00
|
|
|
span_bug!(span, "bad slice pattern type {:?}", ty);
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:39:21 +03:00
|
|
|
fn lower_variant_or_leaf(
|
|
|
|
&mut self,
|
2016-11-25 13:21:19 +02:00
|
|
|
def: Def,
|
2016-09-24 17:45:24 +03:00
|
|
|
subpatterns: Vec<FieldPattern<'tcx>>)
|
2016-10-03 21:39:21 +03:00
|
|
|
-> PatternKind<'tcx>
|
2016-09-24 17:45:24 +03:00
|
|
|
{
|
2016-11-25 13:21:19 +02:00
|
|
|
match def {
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Variant(variant_id) | Def::VariantCtor(variant_id, ..) => {
|
2016-10-03 21:39:21 +03:00
|
|
|
let enum_id = self.tcx.parent_def_id(variant_id).unwrap();
|
|
|
|
let adt_def = self.tcx.lookup_adt_def(enum_id);
|
2015-11-09 20:54:17 -05:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
PatternKind::Leaf { subpatterns: subpatterns }
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
_ => bug!()
|
2015-10-07 14:37:42 +02:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 02:53:26 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|