2015-08-18 17:59:21 -04:00
|
|
|
// 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::*;
|
2015-10-21 17:24:05 -04:00
|
|
|
use hair::cx::Cx;
|
2015-08-18 17:59:21 -04:00
|
|
|
use rustc_data_structures::fnv::FnvHashMap;
|
2015-10-05 12:31:48 -04:00
|
|
|
use rustc::middle::const_eval;
|
|
|
|
use rustc::middle::def;
|
|
|
|
use rustc::middle::pat_util::{pat_is_resolved_const, pat_is_binding};
|
|
|
|
use rustc::middle::ty::{self, Ty};
|
2015-11-19 16:37:34 +01:00
|
|
|
use rustc::mir::repr::*;
|
2015-10-05 12:31:48 -04:00
|
|
|
use rustc_front::hir;
|
|
|
|
use syntax::ast;
|
2015-12-30 22:21:13 +02:00
|
|
|
use syntax::codemap::Span;
|
2015-10-05 12:31:48 -04:00
|
|
|
use syntax::ptr::P;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
/// 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() => { ... }
|
|
|
|
/// _ => { ... }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-09 20:54:17 -05:00
|
|
|
struct PatCx<'patcx, 'cx: 'patcx, 'tcx: 'cx> {
|
|
|
|
cx: &'patcx mut Cx<'cx, 'tcx>,
|
|
|
|
binding_map: Option<&'patcx FnvHashMap<ast::Name, ast::NodeId>>,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
impl<'cx, 'tcx> Cx<'cx, 'tcx> {
|
2015-12-30 22:21:13 +02:00
|
|
|
pub fn irrefutable_pat(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
|
|
|
|
PatCx::new(self, None).to_pattern(pat)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
pub fn refutable_pat(&mut self,
|
|
|
|
binding_map: Option<&FnvHashMap<ast::Name, ast::NodeId>>,
|
2015-12-30 22:21:13 +02:00
|
|
|
pat: &hir::Pat)
|
2015-11-09 20:54:17 -05:00
|
|
|
-> Pattern<'tcx> {
|
2015-12-30 22:21:13 +02:00
|
|
|
PatCx::new(self, binding_map).to_pattern(pat)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
|
|
|
|
fn new(cx: &'patcx mut Cx<'cx, 'tcx>,
|
|
|
|
binding_map: Option<&'patcx FnvHashMap<ast::Name, ast::NodeId>>)
|
|
|
|
-> PatCx<'patcx, 'cx, 'tcx> {
|
|
|
|
PatCx {
|
|
|
|
cx: cx,
|
|
|
|
binding_map: binding_map,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 22:21:13 +02:00
|
|
|
fn to_pattern(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
|
2015-11-09 20:54:17 -05:00
|
|
|
let kind = match pat.node {
|
2015-10-31 03:44:43 +03:00
|
|
|
hir::PatWild => PatternKind::Wild,
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-09-10 15:44:44 -04:00
|
|
|
hir::PatLit(ref value) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
let value = const_eval::eval_const_expr(self.cx.tcx, value);
|
2015-09-10 15:44:44 -04:00
|
|
|
PatternKind::Constant { value: value }
|
2015-10-07 14:37:42 +02:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-09-10 15:44:44 -04:00
|
|
|
hir::PatRange(ref lo, ref hi) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
let lo = const_eval::eval_const_expr(self.cx.tcx, lo);
|
2015-09-10 15:44:44 -04:00
|
|
|
let lo = Literal::Value { value: lo };
|
2015-11-09 20:54:17 -05:00
|
|
|
let hi = const_eval::eval_const_expr(self.cx.tcx, hi);
|
2015-09-10 15:44:44 -04:00
|
|
|
let hi = Literal::Value { value: hi };
|
|
|
|
PatternKind::Range { lo: lo, hi: hi }
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
hir::PatEnum(..) | hir::PatIdent(..) | hir::PatQPath(..)
|
2015-11-09 20:54:17 -05:00
|
|
|
if pat_is_resolved_const(&self.cx.tcx.def_map.borrow(), pat) =>
|
2015-08-18 17:59:21 -04:00
|
|
|
{
|
2015-11-09 20:54:17 -05:00
|
|
|
let def = self.cx.tcx.def_map.borrow().get(&pat.id).unwrap().full_def();
|
2015-08-18 17:59:21 -04:00
|
|
|
match def {
|
|
|
|
def::DefConst(def_id) | def::DefAssociatedConst(def_id) =>
|
2015-08-03 22:16:24 -04:00
|
|
|
match const_eval::lookup_const_by_id(self.cx.tcx, def_id,
|
|
|
|
Some(pat.id), None) {
|
2015-09-10 15:44:44 -04:00
|
|
|
Some(const_expr) => {
|
2015-12-30 22:21:13 +02:00
|
|
|
let pat = const_eval::const_expr_to_pat(self.cx.tcx, const_expr,
|
|
|
|
pat.span);
|
|
|
|
return self.to_pattern(&*pat);
|
2015-09-10 15:44:44 -04:00
|
|
|
}
|
|
|
|
None => {
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2015-09-10 15:44:44 -04:00
|
|
|
&format!("cannot eval constant: {:?}", def_id))
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
},
|
|
|
|
_ =>
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2015-08-18 17:59:21 -04:00
|
|
|
&format!("def not a constant: {:?}", def)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatRegion(ref subpattern, _) |
|
|
|
|
hir::PatBox(ref subpattern) => {
|
2015-12-30 22:21:13 +02:00
|
|
|
PatternKind::Deref { subpattern: self.to_pattern(subpattern) }
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatVec(ref prefix, ref slice, ref suffix) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
let ty = self.cx.tcx.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,
|
2015-12-30 22:21:13 +02:00
|
|
|
kind: Box::new(self.slice_or_array_pattern(pat.span, mt.ty, prefix,
|
2015-11-09 20:54:17 -05:00
|
|
|
slice, suffix)),
|
|
|
|
},
|
2015-08-18 17:59:21 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
ty::TySlice(..) |
|
|
|
|
ty::TyArray(..) =>
|
2015-12-30 22:21:13 +02:00
|
|
|
self.slice_or_array_pattern(pat.span, ty, prefix, slice, suffix),
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
ref sty =>
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2015-08-18 17:59:21 -04:00
|
|
|
&format!("unexpanded type for vector pattern: {:?}", sty)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatTup(ref subpatterns) => {
|
|
|
|
let subpatterns =
|
|
|
|
subpatterns.iter()
|
|
|
|
.enumerate()
|
2015-11-09 20:54:17 -05:00
|
|
|
.map(|(i, subpattern)| FieldPattern {
|
2015-10-21 17:24:05 -04:00
|
|
|
field: Field::new(i),
|
2015-12-30 22:21:13 +02:00
|
|
|
pattern: self.to_pattern(subpattern),
|
2015-08-18 17:59:21 -04:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
PatternKind::Leaf { subpatterns: subpatterns }
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatIdent(bm, ref ident, ref sub)
|
2015-11-09 20:54:17 -05:00
|
|
|
if pat_is_binding(&self.cx.tcx.def_map.borrow(), pat) =>
|
2015-08-18 17:59:21 -04:00
|
|
|
{
|
|
|
|
let id = match self.binding_map {
|
2015-11-09 20:54:17 -05:00
|
|
|
None => pat.id,
|
2015-12-01 20:38:40 +03:00
|
|
|
Some(ref map) => map[&ident.node.name],
|
2015-08-18 17:59:21 -04:00
|
|
|
};
|
2015-11-09 20:54:17 -05:00
|
|
|
let var_ty = self.cx.tcx.node_id_to_type(pat.id);
|
2015-08-18 17:59:21 -04:00
|
|
|
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)),
|
|
|
|
};
|
|
|
|
PatternKind::Binding {
|
|
|
|
mutability: mutability,
|
|
|
|
mode: mode,
|
2015-09-23 20:04:49 +03:00
|
|
|
name: ident.node.name,
|
2015-08-18 17:59:21 -04:00
|
|
|
var: id,
|
|
|
|
ty: var_ty,
|
2015-12-30 22:21:13 +02:00
|
|
|
subpattern: self.to_opt_pattern(sub),
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatIdent(..) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
self.variant_or_leaf(pat, vec![])
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatEnum(_, ref opt_subpatterns) => {
|
|
|
|
let subpatterns =
|
|
|
|
opt_subpatterns.iter()
|
|
|
|
.flat_map(|v| v.iter())
|
|
|
|
.enumerate()
|
2015-11-09 20:54:17 -05:00
|
|
|
.map(|(i, field)| FieldPattern {
|
2015-10-21 17:24:05 -04:00
|
|
|
field: Field::new(i),
|
2015-12-30 22:21:13 +02:00
|
|
|
pattern: self.to_pattern(field),
|
2015-08-18 17:59:21 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2015-11-09 20:54:17 -05:00
|
|
|
self.variant_or_leaf(pat, subpatterns)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatStruct(_, ref fields, _) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
let pat_ty = self.cx.tcx.node_id_to_type(pat.id);
|
2015-10-21 17:24:05 -04:00
|
|
|
let adt_def = match pat_ty.sty {
|
|
|
|
ty::TyStruct(adt_def, _) | ty::TyEnum(adt_def, _) => adt_def,
|
|
|
|
_ => {
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2015-10-21 17:24:05 -04:00
|
|
|
"struct pattern not applied to struct or enum");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
let def = self.cx.tcx.def_map.borrow().get(&pat.id).unwrap().full_def();
|
2015-10-21 17:24:05 -04:00
|
|
|
let variant_def = adt_def.variant_of_def(def);
|
|
|
|
|
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(|| {
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2015-10-21 17:24:05 -04:00
|
|
|
&format!("no field with name {:?}", field.node.name));
|
|
|
|
});
|
2015-11-09 20:54:17 -05:00
|
|
|
FieldPattern {
|
2015-10-21 17:24:05 -04:00
|
|
|
field: Field::new(index),
|
2015-12-30 22:21:13 +02:00
|
|
|
pattern: self.to_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
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
self.variant_or_leaf(pat, subpatterns)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::PatQPath(..) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
self.cx.tcx.sess.span_bug(pat.span, "unexpanded macro or bad constant etc");
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-09 20:54:17 -05:00
|
|
|
let ty = self.cx.tcx.node_id_to_type(pat.id);
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 22:21:13 +02:00
|
|
|
fn to_patterns(&mut self, pats: &[P<hir::Pat>]) -> Vec<Pattern<'tcx>> {
|
|
|
|
pats.iter().map(|p| self.to_pattern(p)).collect()
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 22:21:13 +02:00
|
|
|
fn to_opt_pattern(&mut self, pat: &Option<P<hir::Pat>>) -> Option<Pattern<'tcx>> {
|
|
|
|
pat.as_ref().map(|p| self.to_pattern(p))
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn slice_or_array_pattern(&mut self,
|
2015-12-30 22:21:13 +02:00
|
|
|
span: Span,
|
2015-11-09 20:54:17 -05:00
|
|
|
ty: Ty<'tcx>,
|
2015-12-30 22:21:13 +02:00
|
|
|
prefix: &[P<hir::Pat>],
|
|
|
|
slice: &Option<P<hir::Pat>>,
|
|
|
|
suffix: &[P<hir::Pat>])
|
2015-11-09 20:54:17 -05:00
|
|
|
-> PatternKind<'tcx> {
|
|
|
|
match ty.sty {
|
|
|
|
ty::TySlice(..) => {
|
|
|
|
// matching a slice or fixed-length array
|
|
|
|
PatternKind::Slice {
|
2015-12-30 22:21:13 +02:00
|
|
|
prefix: self.to_patterns(prefix),
|
|
|
|
slice: self.to_opt_pattern(slice),
|
|
|
|
suffix: self.to_patterns(suffix),
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyArray(_, len) => {
|
|
|
|
// fixed-length array
|
|
|
|
assert!(len >= prefix.len() + suffix.len());
|
|
|
|
PatternKind::Array {
|
2015-12-30 22:21:13 +02:00
|
|
|
prefix: self.to_patterns(prefix),
|
|
|
|
slice: self.to_opt_pattern(slice),
|
|
|
|
suffix: self.to_patterns(suffix),
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
2015-12-30 22:21:13 +02:00
|
|
|
self.cx.tcx.sess.span_bug(span, "unexpanded macro or bad constant etc");
|
2015-11-09 20:54:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn variant_or_leaf(&mut self,
|
2015-12-30 22:21:13 +02:00
|
|
|
pat: &hir::Pat,
|
2015-11-09 20:54:17 -05:00
|
|
|
subpatterns: Vec<FieldPattern<'tcx>>)
|
|
|
|
-> PatternKind<'tcx> {
|
|
|
|
let def = self.cx.tcx.def_map.borrow().get(&pat.id).unwrap().full_def();
|
|
|
|
match def {
|
2016-01-17 22:57:54 +03:00
|
|
|
def::DefVariant(enum_id, variant_id) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 22:57:54 +03:00
|
|
|
def::DefStruct(..) | def::DefTyAlias(..) => {
|
2015-11-09 20:54:17 -05:00
|
|
|
PatternKind::Leaf { subpatterns: subpatterns }
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
self.cx.tcx.sess.span_bug(pat.span,
|
|
|
|
&format!("inappropriate def for pattern: {:?}", def));
|
|
|
|
}
|
2015-10-07 14:37:42 +02:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
}
|