1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use rustc::middle::const_val::{ConstEvalErr, ConstVal};
14 use rustc::mir::{Field, BorrowKind, Mutability};
15 use rustc::ty::{self, TyCtxt, AdtDef, Ty, Region};
16 use rustc::ty::subst::{Substs, Kind};
17 use rustc::hir::{self, PatKind, RangeEnd};
18 use rustc::hir::def::{Def, CtorKind};
19 use rustc::hir::pat_util::EnumerateAndAdjustIterator;
21 use rustc_data_structures::indexed_vec::Idx;
28 #[derive(Clone, Debug)]
29 pub enum PatternError<'tcx> {
30 AssociatedConstInPattern(Span),
31 StaticInPattern(Span),
32 ConstEval(ConstEvalErr<'tcx>),
35 #[derive(Copy, Clone, Debug)]
36 pub enum BindingMode<'tcx> {
38 ByRef(Region<'tcx>, BorrowKind),
41 #[derive(Clone, Debug)]
42 pub struct FieldPattern<'tcx> {
44 pub pattern: Pattern<'tcx>,
47 #[derive(Clone, Debug)]
48 pub struct Pattern<'tcx> {
51 pub kind: Box<PatternKind<'tcx>>,
54 #[derive(Clone, Debug)]
55 pub enum PatternKind<'tcx> {
58 /// x, ref x, x @ P, etc
60 mutability: Mutability,
62 mode: BindingMode<'tcx>,
65 subpattern: Option<Pattern<'tcx>>,
68 /// Foo(...) or Foo{...} or Foo, where `Foo` is a variant name from an adt with >1 variants
70 adt_def: &'tcx AdtDef,
71 substs: &'tcx Substs<'tcx>,
73 subpatterns: Vec<FieldPattern<'tcx>>,
76 /// (...), Foo(...), Foo{...}, or Foo, where `Foo` is a variant name from an adt with 1 variant
78 subpatterns: Vec<FieldPattern<'tcx>>,
81 /// box P, &P, &mut P, etc
83 subpattern: Pattern<'tcx>,
87 value: &'tcx ty::Const<'tcx>,
91 lo: &'tcx ty::Const<'tcx>,
92 hi: &'tcx ty::Const<'tcx>,
96 /// matches against a slice, checking the length and extracting elements.
97 /// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
98 /// e.g. `&[ref xs..]`.
100 prefix: Vec<Pattern<'tcx>>,
101 slice: Option<Pattern<'tcx>>,
102 suffix: Vec<Pattern<'tcx>>,
105 /// fixed match against an array, irrefutable
107 prefix: Vec<Pattern<'tcx>>,
108 slice: Option<Pattern<'tcx>>,
109 suffix: Vec<Pattern<'tcx>>,
113 fn print_const_val(value: &ConstVal, f: &mut fmt::Formatter) -> fmt::Result {
115 ConstVal::Float(ref x) => write!(f, "{}", x),
116 ConstVal::Integral(ref i) => write!(f, "{}", i),
117 ConstVal::Str(ref s) => write!(f, "{:?}", &s[..]),
118 ConstVal::ByteStr(b) => write!(f, "{:?}", b.data),
119 ConstVal::Bool(b) => write!(f, "{:?}", b),
120 ConstVal::Char(c) => write!(f, "{:?}", c),
121 ConstVal::Variant(_) |
122 ConstVal::Function(..) |
123 ConstVal::Aggregate(_) |
124 ConstVal::Unevaluated(..) => bug!("{:?} not printable in a pattern", value)
128 impl<'tcx> fmt::Display for Pattern<'tcx> {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131 PatternKind::Wild => write!(f, "_"),
132 PatternKind::Binding { mutability, name, mode, ref subpattern, .. } => {
133 let is_mut = match mode {
134 BindingMode::ByValue => mutability == Mutability::Mut,
135 BindingMode::ByRef(_, bk) => {
137 match bk { BorrowKind::Mut { .. } => true, _ => false }
143 write!(f, "{}", name)?;
144 if let Some(ref subpattern) = *subpattern {
145 write!(f, " @ {}", subpattern)?;
149 PatternKind::Variant { ref subpatterns, .. } |
150 PatternKind::Leaf { ref subpatterns } => {
151 let variant = match *self.kind {
152 PatternKind::Variant { adt_def, variant_index, .. } => {
153 Some(&adt_def.variants[variant_index])
155 _ => if let ty::TyAdt(adt, _) = self.ty.sty {
157 Some(&adt.variants[0])
166 let mut first = true;
167 let mut start_or_continue = || if first { first = false; "" } else { ", " };
169 if let Some(variant) = variant {
170 write!(f, "{}", variant.name)?;
172 // Only for TyAdt we can have `S {...}`,
173 // which we handle separately here.
174 if variant.ctor_kind == CtorKind::Fictive {
178 for p in subpatterns {
179 if let PatternKind::Wild = *p.pattern.kind {
182 let name = variant.fields[p.field.index()].name;
183 write!(f, "{}{}: {}", start_or_continue(), name, p.pattern)?;
187 if printed < variant.fields.len() {
188 write!(f, "{}..", start_or_continue())?;
191 return write!(f, " }}");
195 let num_fields = variant.map_or(subpatterns.len(), |v| v.fields.len());
196 if num_fields != 0 || variant.is_none() {
198 for i in 0..num_fields {
199 write!(f, "{}", start_or_continue())?;
201 // Common case: the field is where we expect it.
202 if let Some(p) = subpatterns.get(i) {
203 if p.field.index() == i {
204 write!(f, "{}", p.pattern)?;
209 // Otherwise, we have to go looking for it.
210 if let Some(p) = subpatterns.iter().find(|p| p.field.index() == i) {
211 write!(f, "{}", p.pattern)?;
221 PatternKind::Deref { ref subpattern } => {
223 ty::TyAdt(def, _) if def.is_box() => write!(f, "box ")?,
224 ty::TyRef(_, mt) => {
226 if mt.mutbl == hir::MutMutable {
230 _ => bug!("{} is a bad Deref pattern type", self.ty)
232 write!(f, "{}", subpattern)
234 PatternKind::Constant { value } => {
235 print_const_val(&value.val, f)
237 PatternKind::Range { lo, hi, end } => {
238 print_const_val(&lo.val, f)?;
240 RangeEnd::Included => write!(f, "...")?,
241 RangeEnd::Excluded => write!(f, "..")?,
243 print_const_val(&hi.val, f)
245 PatternKind::Slice { ref prefix, ref slice, ref suffix } |
246 PatternKind::Array { ref prefix, ref slice, ref suffix } => {
247 let mut first = true;
248 let mut start_or_continue = || if first { first = false; "" } else { ", " };
251 write!(f, "{}{}", start_or_continue(), p)?;
253 if let Some(ref slice) = *slice {
254 write!(f, "{}", start_or_continue())?;
256 PatternKind::Wild => {}
257 _ => write!(f, "{}", slice)?
262 write!(f, "{}{}", start_or_continue(), p)?;
270 pub struct PatternContext<'a, 'tcx: 'a> {
271 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
272 pub param_env: ty::ParamEnv<'tcx>,
273 pub tables: &'a ty::TypeckTables<'tcx>,
274 pub substs: &'tcx Substs<'tcx>,
275 pub errors: Vec<PatternError<'tcx>>,
278 impl<'a, 'tcx> Pattern<'tcx> {
279 pub fn from_hir(tcx: TyCtxt<'a, 'tcx, 'tcx>,
280 param_env_and_substs: ty::ParamEnvAnd<'tcx, &'tcx Substs<'tcx>>,
281 tables: &'a ty::TypeckTables<'tcx>,
282 pat: &'tcx hir::Pat) -> Self {
283 let mut pcx = PatternContext::new(tcx, param_env_and_substs, tables);
284 let result = pcx.lower_pattern(pat);
285 if !pcx.errors.is_empty() {
286 let msg = format!("encountered errors lowering pattern: {:?}", pcx.errors);
287 tcx.sess.delay_span_bug(pat.span, &msg);
289 debug!("Pattern::from_hir({:?}) = {:?}", pat, result);
294 impl<'a, 'tcx> PatternContext<'a, 'tcx> {
295 pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
296 param_env_and_substs: ty::ParamEnvAnd<'tcx, &'tcx Substs<'tcx>>,
297 tables: &'a ty::TypeckTables<'tcx>) -> Self {
300 param_env: param_env_and_substs.param_env,
302 substs: param_env_and_substs.value,
307 pub fn lower_pattern(&mut self, pat: &'tcx hir::Pat) -> Pattern<'tcx> {
308 // When implicit dereferences have been inserted in this pattern, the unadjusted lowered
309 // pattern has the type that results *after* dereferencing. For example, in this code:
312 // match &&Some(0i32) {
313 // Some(n) => { ... },
318 // the type assigned to `Some(n)` in `unadjusted_pat` would be `Option<i32>` (this is
319 // determined in rustc_typeck::check::match). The adjustments would be
321 // `vec![&&Option<i32>, &Option<i32>]`.
323 // Applying the adjustments, we want to instead output `&&Some(n)` (as a HAIR pattern). So
324 // we wrap the unadjusted pattern in `PatternKind::Deref` repeatedly, consuming the
325 // adjustments in *reverse order* (last-in-first-out, so that the last `Deref` inserted
326 // gets the least-dereferenced type).
327 let unadjusted_pat = self.lower_pattern_unadjusted(pat);
334 .fold(unadjusted_pat, |pat, ref_ty| {
335 debug!("{:?}: wrapping pattern with type {:?}", pat, ref_ty);
339 kind: Box::new(PatternKind::Deref { subpattern: pat }),
345 fn lower_pattern_unadjusted(&mut self, pat: &'tcx hir::Pat) -> Pattern<'tcx> {
346 let mut ty = self.tables.node_id_to_type(pat.hir_id);
348 let kind = match pat.node {
349 PatKind::Wild => PatternKind::Wild,
351 PatKind::Lit(ref value) => self.lower_lit(value),
353 PatKind::Range(ref lo, ref hi, end) => {
354 match (self.lower_lit(lo), self.lower_lit(hi)) {
355 (PatternKind::Constant { value: lo },
356 PatternKind::Constant { value: hi }) => {
357 PatternKind::Range { lo, hi, end }
359 _ => PatternKind::Wild
363 PatKind::Path(ref qpath) => {
364 return self.lower_path(qpath, pat.hir_id, pat.id, pat.span);
367 PatKind::Ref(ref subpattern, _) |
368 PatKind::Box(ref subpattern) => {
369 PatternKind::Deref { subpattern: self.lower_pattern(subpattern) }
372 PatKind::Slice(ref prefix, ref slice, ref suffix) => {
373 let ty = self.tables.node_id_to_type(pat.hir_id);
377 subpattern: Pattern {
380 kind: Box::new(self.slice_or_array_pattern(
381 pat.span, mt.ty, prefix, slice, suffix))
387 self.slice_or_array_pattern(pat.span, ty, prefix, slice, suffix),
392 "unexpanded type for vector pattern: {:?}",
397 PatKind::Tuple(ref subpatterns, ddpos) => {
398 let ty = self.tables.node_id_to_type(pat.hir_id);
400 ty::TyTuple(ref tys, _) => {
403 .enumerate_and_adjust(tys.len(), ddpos)
404 .map(|(i, subpattern)| FieldPattern {
405 field: Field::new(i),
406 pattern: self.lower_pattern(subpattern)
410 PatternKind::Leaf { subpatterns: subpatterns }
413 ref sty => span_bug!(pat.span, "unexpected type for tuple pattern: {:?}", sty),
417 PatKind::Binding(_, id, ref ident, ref sub) => {
418 let var_ty = self.tables.node_id_to_type(pat.hir_id);
419 let region = match var_ty.sty {
420 ty::TyRef(r, _) => Some(r),
423 let bm = *self.tables.pat_binding_modes().get(pat.hir_id)
424 .expect("missing binding mode");
425 let (mutability, mode) = match bm {
426 ty::BindByValue(hir::MutMutable) =>
427 (Mutability::Mut, BindingMode::ByValue),
428 ty::BindByValue(hir::MutImmutable) =>
429 (Mutability::Not, BindingMode::ByValue),
430 ty::BindByReference(hir::MutMutable) =>
431 (Mutability::Not, BindingMode::ByRef(
432 region.unwrap(), BorrowKind::Mut { allow_two_phase_borrow: false })),
433 ty::BindByReference(hir::MutImmutable) =>
434 (Mutability::Not, BindingMode::ByRef(
435 region.unwrap(), BorrowKind::Shared)),
438 // A ref x pattern is the same node used for x, and as such it has
439 // x's type, which is &T, where we want T (the type being matched).
440 if let ty::BindByReference(_) = bm {
441 if let ty::TyRef(_, mt) = ty.sty {
444 bug!("`ref {}` has wrong type {}", ident.node, ty);
448 PatternKind::Binding {
454 subpattern: self.lower_opt_pattern(sub),
458 PatKind::TupleStruct(ref qpath, ref subpatterns, ddpos) => {
459 let def = self.tables.qpath_def(qpath, pat.hir_id);
460 let adt_def = match ty.sty {
461 ty::TyAdt(adt_def, _) => adt_def,
462 _ => span_bug!(pat.span, "tuple struct pattern not applied to an ADT"),
464 let variant_def = adt_def.variant_of_def(def);
468 .enumerate_and_adjust(variant_def.fields.len(), ddpos)
469 .map(|(i, field)| FieldPattern {
470 field: Field::new(i),
471 pattern: self.lower_pattern(field),
474 self.lower_variant_or_leaf(def, ty, subpatterns)
477 PatKind::Struct(ref qpath, ref fields, _) => {
478 let def = self.tables.qpath_def(qpath, pat.hir_id);
479 let adt_def = match ty.sty {
480 ty::TyAdt(adt_def, _) => adt_def,
484 "struct pattern not applied to an ADT");
487 let variant_def = adt_def.variant_of_def(def);
492 let index = variant_def.index_of_field_named(field.node.name);
493 let index = index.unwrap_or_else(|| {
496 "no field with name {:?}",
500 field: Field::new(index),
501 pattern: self.lower_pattern(&field.node.pat),
506 self.lower_variant_or_leaf(def, ty, subpatterns)
513 kind: Box::new(kind),
517 fn lower_patterns(&mut self, pats: &'tcx [P<hir::Pat>]) -> Vec<Pattern<'tcx>> {
518 pats.iter().map(|p| self.lower_pattern(p)).collect()
521 fn lower_opt_pattern(&mut self, pat: &'tcx Option<P<hir::Pat>>) -> Option<Pattern<'tcx>>
523 pat.as_ref().map(|p| self.lower_pattern(p))
526 fn flatten_nested_slice_patterns(
528 prefix: Vec<Pattern<'tcx>>,
529 slice: Option<Pattern<'tcx>>,
530 suffix: Vec<Pattern<'tcx>>)
531 -> (Vec<Pattern<'tcx>>, Option<Pattern<'tcx>>, Vec<Pattern<'tcx>>)
533 let orig_slice = match slice {
534 Some(orig_slice) => orig_slice,
535 None => return (prefix, slice, suffix)
537 let orig_prefix = prefix;
538 let orig_suffix = suffix;
540 // dance because of intentional borrow-checker stupidity.
541 let kind = *orig_slice.kind;
543 PatternKind::Slice { prefix, slice, mut suffix } |
544 PatternKind::Array { prefix, slice, mut suffix } => {
545 let mut orig_prefix = orig_prefix;
547 orig_prefix.extend(prefix);
548 suffix.extend(orig_suffix);
550 (orig_prefix, slice, suffix)
553 (orig_prefix, Some(Pattern {
554 kind: box kind, ..orig_slice
560 fn slice_or_array_pattern(
564 prefix: &'tcx [P<hir::Pat>],
565 slice: &'tcx Option<P<hir::Pat>>,
566 suffix: &'tcx [P<hir::Pat>])
569 let prefix = self.lower_patterns(prefix);
570 let slice = self.lower_opt_pattern(slice);
571 let suffix = self.lower_patterns(suffix);
572 let (prefix, slice, suffix) =
573 self.flatten_nested_slice_patterns(prefix, slice, suffix);
577 // matching a slice or fixed-length array
578 PatternKind::Slice { prefix: prefix, slice: slice, suffix: suffix }
581 ty::TyArray(_, len) => {
582 // fixed-length array
583 let len = len.val.to_const_int().unwrap().to_u64().unwrap();
584 assert!(len >= prefix.len() as u64 + suffix.len() as u64);
585 PatternKind::Array { prefix: prefix, slice: slice, suffix: suffix }
589 span_bug!(span, "bad slice pattern type {:?}", ty);
594 fn lower_variant_or_leaf(
598 subpatterns: Vec<FieldPattern<'tcx>>)
602 Def::Variant(variant_id) | Def::VariantCtor(variant_id, ..) => {
603 let enum_id = self.tcx.parent_def_id(variant_id).unwrap();
604 let adt_def = self.tcx.adt_def(enum_id);
605 if adt_def.is_enum() {
606 let substs = match ty.sty {
607 ty::TyAdt(_, substs) |
608 ty::TyFnDef(_, substs) => substs,
609 _ => bug!("inappropriate type for def: {:?}", ty.sty),
611 PatternKind::Variant {
614 variant_index: adt_def.variant_index_with_id(variant_id),
618 PatternKind::Leaf { subpatterns: subpatterns }
622 Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
623 Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => {
624 PatternKind::Leaf { subpatterns: subpatterns }
631 fn lower_path(&mut self,
637 let ty = self.tables.node_id_to_type(id);
638 let def = self.tables.qpath_def(qpath, id);
639 let is_associated_const = match def {
640 Def::AssociatedConst(_) => true,
643 let kind = match def {
644 Def::Const(def_id) | Def::AssociatedConst(def_id) => {
645 let substs = self.tables.node_substs(id);
646 match eval::lookup_const_by_id(self.tcx, self.param_env.and((def_id, substs))) {
647 Some((def_id, substs)) => {
648 // Enter the inlined constant's tables&substs temporarily.
649 let old_tables = self.tables;
650 let old_substs = self.substs;
651 self.tables = self.tcx.typeck_tables_of(def_id);
652 self.substs = substs;
653 let body = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
654 self.tcx.hir.body(self.tcx.hir.body_owned_by(id))
656 self.tcx.extern_const_body(def_id).body
658 let pat = self.lower_const_expr(&body.value, pat_id, span);
659 self.tables = old_tables;
660 self.substs = old_substs;
664 self.errors.push(if is_associated_const {
665 PatternError::AssociatedConstInPattern(span)
667 PatternError::StaticInPattern(span)
673 _ => self.lower_variant_or_leaf(def, ty, vec![]),
679 kind: Box::new(kind),
683 fn lower_lit(&mut self, expr: &'tcx hir::Expr) -> PatternKind<'tcx> {
684 let const_cx = eval::ConstContext::new(self.tcx,
685 self.param_env.and(self.substs),
687 match const_cx.eval(expr) {
689 if let ConstVal::Variant(def_id) = value.val {
690 let ty = self.tables.expr_ty(expr);
691 self.lower_variant_or_leaf(Def::Variant(def_id), ty, vec![])
693 PatternKind::Constant { value }
697 self.errors.push(PatternError::ConstEval(e));
703 fn lower_const_expr(&mut self,
704 expr: &'tcx hir::Expr,
708 let pat_ty = self.tables.expr_ty(expr);
709 debug!("expr={:?} pat_ty={:?} pat_id={}", expr, pat_ty, pat_id);
712 self.tcx.sess.span_err(span, "floating point constants cannot be used in patterns");
714 ty::TyAdt(adt_def, _) if adt_def.is_union() => {
715 // Matching on union fields is unsafe, we can't hide it in constants
716 self.tcx.sess.span_err(span, "cannot use unions in constant patterns");
718 ty::TyAdt(adt_def, _) => {
719 if !self.tcx.has_attr(adt_def.did, "structural_match") {
720 let msg = format!("to use a constant of type `{}` in a pattern, \
721 `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
722 self.tcx.item_path_str(adt_def.did),
723 self.tcx.item_path_str(adt_def.did));
724 self.tcx.sess.span_err(span, &msg);
729 let kind = match expr.node {
730 hir::ExprTup(ref exprs) => {
732 subpatterns: exprs.iter().enumerate().map(|(i, expr)| {
734 field: Field::new(i),
735 pattern: self.lower_const_expr(expr, pat_id, span)
741 hir::ExprCall(ref callee, ref args) => {
742 let qpath = match callee.node {
743 hir::ExprPath(ref qpath) => qpath,
746 let ty = self.tables.node_id_to_type(callee.hir_id);
747 let def = self.tables.qpath_def(qpath, callee.hir_id);
749 Def::Fn(..) | Def::Method(..) => self.lower_lit(expr),
751 let subpatterns = args.iter().enumerate().map(|(i, expr)| {
753 field: Field::new(i),
754 pattern: self.lower_const_expr(expr, pat_id, span)
757 self.lower_variant_or_leaf(def, ty, subpatterns)
762 hir::ExprStruct(ref qpath, ref fields, None) => {
763 let def = self.tables.qpath_def(qpath, expr.hir_id);
764 let adt_def = match pat_ty.sty {
765 ty::TyAdt(adt_def, _) => adt_def,
769 "struct expr without ADT type");
772 let variant_def = adt_def.variant_of_def(def);
777 let index = variant_def.index_of_field_named(field.name.node);
778 let index = index.unwrap_or_else(|| {
781 "no field with name {:?}",
785 field: Field::new(index),
786 pattern: self.lower_const_expr(&field.expr, pat_id, span),
791 self.lower_variant_or_leaf(def, pat_ty, subpatterns)
794 hir::ExprArray(ref exprs) => {
795 let pats = exprs.iter()
796 .map(|expr| self.lower_const_expr(expr, pat_id, span))
805 hir::ExprPath(ref qpath) => {
806 return self.lower_path(qpath, expr.hir_id, pat_id, span);
809 _ => self.lower_lit(expr)
815 kind: Box::new(kind),
820 pub trait PatternFoldable<'tcx> : Sized {
821 fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
822 self.super_fold_with(folder)
825 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self;
828 pub trait PatternFolder<'tcx> : Sized {
829 fn fold_pattern(&mut self, pattern: &Pattern<'tcx>) -> Pattern<'tcx> {
830 pattern.super_fold_with(self)
833 fn fold_pattern_kind(&mut self, kind: &PatternKind<'tcx>) -> PatternKind<'tcx> {
834 kind.super_fold_with(self)
839 impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Box<T> {
840 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
841 let content: T = (**self).fold_with(folder);
846 impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Vec<T> {
847 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
848 self.iter().map(|t| t.fold_with(folder)).collect()
852 impl<'tcx, T: PatternFoldable<'tcx>> PatternFoldable<'tcx> for Option<T> {
853 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self{
854 self.as_ref().map(|t| t.fold_with(folder))
858 macro_rules! CloneImpls {
859 (<$lt_tcx:tt> $($ty:ty),+) => {
861 impl<$lt_tcx> PatternFoldable<$lt_tcx> for $ty {
862 fn super_fold_with<F: PatternFolder<$lt_tcx>>(&self, _: &mut F) -> Self {
871 Span, Field, Mutability, ast::Name, ast::NodeId, usize, &'tcx ty::Const<'tcx>,
872 Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
873 &'tcx Substs<'tcx>, &'tcx Kind<'tcx>
876 impl<'tcx> PatternFoldable<'tcx> for FieldPattern<'tcx> {
877 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
879 field: self.field.fold_with(folder),
880 pattern: self.pattern.fold_with(folder)
885 impl<'tcx> PatternFoldable<'tcx> for Pattern<'tcx> {
886 fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
887 folder.fold_pattern(self)
890 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
892 ty: self.ty.fold_with(folder),
893 span: self.span.fold_with(folder),
894 kind: self.kind.fold_with(folder)
899 impl<'tcx> PatternFoldable<'tcx> for PatternKind<'tcx> {
900 fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
901 folder.fold_pattern_kind(self)
904 fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
906 PatternKind::Wild => PatternKind::Wild,
907 PatternKind::Binding {
914 } => PatternKind::Binding {
915 mutability: mutability.fold_with(folder),
916 name: name.fold_with(folder),
917 mode: mode.fold_with(folder),
918 var: var.fold_with(folder),
919 ty: ty.fold_with(folder),
920 subpattern: subpattern.fold_with(folder),
922 PatternKind::Variant {
927 } => PatternKind::Variant {
928 adt_def: adt_def.fold_with(folder),
929 substs: substs.fold_with(folder),
930 variant_index: variant_index.fold_with(folder),
931 subpatterns: subpatterns.fold_with(folder)
935 } => PatternKind::Leaf {
936 subpatterns: subpatterns.fold_with(folder),
940 } => PatternKind::Deref {
941 subpattern: subpattern.fold_with(folder),
943 PatternKind::Constant {
945 } => PatternKind::Constant {
946 value: value.fold_with(folder)
952 } => PatternKind::Range {
953 lo: lo.fold_with(folder),
954 hi: hi.fold_with(folder),
961 } => PatternKind::Slice {
962 prefix: prefix.fold_with(folder),
963 slice: slice.fold_with(folder),
964 suffix: suffix.fold_with(folder)
970 } => PatternKind::Array {
971 prefix: prefix.fold_with(folder),
972 slice: slice.fold_with(folder),
973 suffix: suffix.fold_with(folder)