.map(|subpattern| {
// e.g., `(x as Variant).0`
let lvalue = downcast_lvalue.clone().field(subpattern.field,
- subpattern.field_ty());
+ subpattern.pattern.ty);
// e.g., `(x as Variant).0 @ P1`
MatchPair::new(lvalue, &subpattern.pattern)
});
}
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,
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,
}
};
- let ty = self.cx.tcx.node_id_to_type(pat.id);
-
Pattern {
span: pat.span,
ty: ty,
}
}
}
-
-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
- }
-}