mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
mir: Get the right non-reference type for binding patterns.
This commit is contained in:
parent
cf4daf7889
commit
aca4f9396d
@ -488,7 +488,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
|
|||||||
.map(|subpattern| {
|
.map(|subpattern| {
|
||||||
// e.g., `(x as Variant).0`
|
// e.g., `(x as Variant).0`
|
||||||
let lvalue = downcast_lvalue.clone().field(subpattern.field,
|
let lvalue = downcast_lvalue.clone().field(subpattern.field,
|
||||||
subpattern.field_ty());
|
subpattern.pattern.ty);
|
||||||
// e.g., `(x as Variant).0 @ P1`
|
// e.g., `(x as Variant).0 @ P1`
|
||||||
MatchPair::new(lvalue, &subpattern.pattern)
|
MatchPair::new(lvalue, &subpattern.pattern)
|
||||||
});
|
});
|
||||||
|
@ -22,7 +22,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
|
|||||||
subpatterns.iter()
|
subpatterns.iter()
|
||||||
.map(|fieldpat| {
|
.map(|fieldpat| {
|
||||||
let lvalue = lvalue.clone().field(fieldpat.field,
|
let lvalue = lvalue.clone().field(fieldpat.field,
|
||||||
fieldpat.field_ty());
|
fieldpat.pattern.ty);
|
||||||
MatchPair::new(lvalue, &fieldpat.pattern)
|
MatchPair::new(lvalue, &fieldpat.pattern)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -63,6 +63,8 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn to_pattern(&mut self, pat: &hir::Pat) -> Pattern<'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 {
|
let kind = match pat.node {
|
||||||
PatKind::Wild => PatternKind::Wild,
|
PatKind::Wild => PatternKind::Wild,
|
||||||
|
|
||||||
@ -169,6 +171,17 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
|
|||||||
hir::BindByRef(hir::MutImmutable) =>
|
hir::BindByRef(hir::MutImmutable) =>
|
||||||
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Shared)),
|
(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 {
|
PatternKind::Binding {
|
||||||
mutability: mutability,
|
mutability: mutability,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
@ -234,8 +247,6 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let ty = self.cx.tcx.node_id_to_type(pat.id);
|
|
||||||
|
|
||||||
Pattern {
|
Pattern {
|
||||||
span: pat.span,
|
span: pat.span,
|
||||||
ty: ty,
|
ty: ty,
|
||||||
@ -314,20 +325,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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user