Skip to content

Commit 1217b82

Browse files
committed
MemCategorizationContext::pat_ty(BindByRef) yields type of borrowed val now.
This is to fix a problem where I could not reliably map attach the type for each loan-path to the loan-path itself because the same loan-path was ending up associated with two different types, because the cmt's had diverged in their interpretation of the path.
1 parent 91b88c5 commit 1217b82

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/librustc/middle/mem_categorization.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,28 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
410410
}
411411

412412
fn pat_ty(&self, pat: &ast::Pat) -> McResult<Ty<'tcx>> {
413-
self.typer.node_ty(pat.id)
413+
let tcx = self.typer.tcx();
414+
let base_ty = self.typer.node_ty(pat.id);
415+
// FIXME (Issue #18207): This code detects whether we are
416+
// looking at a `ref x`, and if so, figures out what the type
417+
// *being borrowed* is. But ideally we would put in a more
418+
// fundamental fix to this conflated use of the node id.
419+
let ret_ty = match pat.node {
420+
ast::PatIdent(ast::BindByRef(_), _, _) => {
421+
// a bind-by-ref means that the base_ty will be the type of the ident itself,
422+
// but what we want here is the type of the underlying value being borrowed.
423+
// So peel off one-level, turning the &T into T.
424+
base_ty.map(|t| {
425+
ty::deref(t, false).unwrap_or_else(|| {
426+
panic!("encountered BindByRef with non &-type");
427+
}).ty
428+
})
429+
}
430+
_ => base_ty,
431+
};
432+
debug!("pat_ty(pat={}) base_ty={} ret_ty={}",
433+
pat.repr(tcx), base_ty.repr(tcx), ret_ty.repr(tcx));
434+
ret_ty
414435
}
415436

416437
pub fn cat_expr(&self, expr: &ast::Expr) -> McResult<cmt<'tcx>> {

0 commit comments

Comments
 (0)