Skip to content

Commit 396c6e4

Browse files
committed
mir: Get the right non-reference type for binding patterns.
1 parent 48217d6 commit 396c6e4

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/librustc_mir/build/matches/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
488488
.map(|subpattern| {
489489
// e.g., `(x as Variant).0`
490490
let lvalue = downcast_lvalue.clone().field(subpattern.field,
491-
subpattern.field_ty());
491+
subpattern.pattern.ty);
492492
// e.g., `(x as Variant).0 @ P1`
493493
MatchPair::new(lvalue, &subpattern.pattern)
494494
});

src/librustc_mir/build/matches/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
2222
subpatterns.iter()
2323
.map(|fieldpat| {
2424
let lvalue = lvalue.clone().field(fieldpat.field,
25-
fieldpat.field_ty());
25+
fieldpat.pattern.ty);
2626
MatchPair::new(lvalue, &fieldpat.pattern)
2727
})
2828
.collect()

src/librustc_mir/hair/cx/pattern.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
6363
}
6464

6565
fn to_pattern(&mut self, pat: &hir::Pat) -> Pattern<'tcx> {
66+
let mut ty = self.cx.tcx.node_id_to_type(pat.id);
67+
6668
let kind = match pat.node {
6769
PatKind::Wild => PatternKind::Wild,
6870

@@ -169,6 +171,17 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
169171
hir::BindByRef(hir::MutImmutable) =>
170172
(Mutability::Not, BindingMode::ByRef(region.unwrap(), BorrowKind::Shared)),
171173
};
174+
175+
// A ref x pattern is the same node used for x, and as such it has
176+
// x's type, which is &T, where we want T (the type being matched).
177+
if let hir::BindByRef(_) = bm {
178+
if let ty::TyRef(_, mt) = ty.sty {
179+
ty = mt.ty;
180+
} else {
181+
unreachable!("`ref {}` has wrong type {}", ident.node, ty);
182+
}
183+
}
184+
172185
PatternKind::Binding {
173186
mutability: mutability,
174187
mode: mode,
@@ -234,8 +247,6 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
234247
}
235248
};
236249

237-
let ty = self.cx.tcx.node_id_to_type(pat.id);
238-
239250
Pattern {
240251
span: pat.span,
241252
ty: ty,
@@ -314,20 +325,3 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
314325
}
315326
}
316327
}
317-
318-
impl<'tcx> FieldPattern<'tcx> {
319-
pub fn field_ty(&self) -> Ty<'tcx> {
320-
debug!("field_ty({:?},ty={:?})", self, self.pattern.ty);
321-
let r = match *self.pattern.kind {
322-
PatternKind::Binding { mode: BindingMode::ByRef(..), ..} => {
323-
match self.pattern.ty.sty {
324-
ty::TyRef(_, mt) => mt.ty,
325-
_ => unreachable!()
326-
}
327-
}
328-
_ => self.pattern.ty
329-
};
330-
debug!("field_ty -> {:?}", r);
331-
r
332-
}
333-
}

0 commit comments

Comments
 (0)