diff options
Diffstat (limited to 'src/librustc_mir/hair/cx/pattern.rs')
-rw-r--r-- | src/librustc_mir/hair/cx/pattern.rs | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/src/librustc_mir/hair/cx/pattern.rs b/src/librustc_mir/hair/cx/pattern.rs index d1e3f08aff8..a9873778374 100644 --- a/src/librustc_mir/hair/cx/pattern.rs +++ b/src/librustc_mir/hair/cx/pattern.rs @@ -63,6 +63,8 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { } 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, @@ -84,9 +86,9 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { { let def = self.cx.tcx.def_map.borrow().get(&pat.id).unwrap().full_def(); match def { - Def::Const(def_id) | Def::AssociatedConst(def_id) => - match const_eval::lookup_const_by_id(self.cx.tcx, def_id, - Some(pat.id), None) { + Def::Const(def_id) | Def::AssociatedConst(def_id) => { + let substs = Some(self.cx.tcx.node_id_item_substs(pat.id).substs); + match const_eval::lookup_const_by_id(self.cx.tcx, def_id, substs) { Some((const_expr, _const_ty)) => { let pat = const_eval::const_expr_to_pat(self.cx.tcx, const_expr, pat.span); @@ -97,7 +99,8 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { pat.span, &format!("cannot eval constant: {:?}", def_id)) } - }, + } + } _ => self.cx.tcx.sess.span_bug( pat.span, @@ -169,6 +172,17 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { 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 { + unreachable!("`ref {}` has wrong type {}", ident.node, ty); + } + } + PatternKind::Binding { mutability: mutability, mode: mode, @@ -234,8 +248,6 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { } }; - let ty = self.cx.tcx.node_id_to_type(pat.id); - Pattern { span: pat.span, ty: ty, @@ -314,20 +326,3 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> { } } } - -impl<'tcx> FieldPattern<'tcx> { - pub fn field_ty(&self) -> Ty<'tcx> { - debug!("field_ty({:?},ty={:?})", self, self.pattern.ty); - let r = match *self.pattern.kind { - PatternKind::Binding { mode: BindingMode::ByRef(..), ..} => { - match self.pattern.ty.sty { - ty::TyRef(_, mt) => mt.ty, - _ => unreachable!() - } - } - _ => self.pattern.ty - }; - debug!("field_ty -> {:?}", r); - r - } -} |