From 9ce2f3af9379246909d3e0151b956ad428bbc175 Mon Sep 17 00:00:00 2001 From: Basile Desloges Date: Fri, 6 Oct 2017 17:26:14 +0200 Subject: [PATCH] mir-borrowck: Implement end-user output for field of index projection --- src/librustc_mir/borrow_check.rs | 12 ++-------- .../borrowck/borrowck-describe-lvalue.rs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs index 384b1fb418cf..0d9dbfab1e88 100644 --- a/src/librustc_mir/borrow_check.rs +++ b/src/librustc_mir/borrow_check.rs @@ -1125,20 +1125,12 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> match proj.elem { ProjectionElem::Deref => self.describe_field(&proj.base, field_index), - ProjectionElem::Index(..) => { - debug!("End-user description not implemented for field of projection {:?}", - proj); - format!("{}", field_index) - }, - ProjectionElem::ConstantIndex { .. } => { - debug!("End-user description not implemented for field of projection {:?}", - proj); - format!("{}", field_index) - }, ProjectionElem::Downcast(def, variant_index) => format!("{}", def.variants[variant_index].fields[field_index].name), ProjectionElem::Field(_, field_type) => self.describe_field_from_ty(&field_type, field_index), + ProjectionElem::Index(..) | ProjectionElem::ConstantIndex { .. } => + format!("{}", self.describe_field(&proj.base, field_index)), ProjectionElem::Subslice { .. } => { debug!("End-user description not implemented for field of projection {:?}", proj); diff --git a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs index 103aad693f78..7ead9032c135 100644 --- a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs +++ b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs @@ -276,4 +276,27 @@ fn main() { _ => panic!("other case"), } } + // Field of index + { + struct F {x: u32, y: u32}; + let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}]; + let _v = &mut v; + v[0].y; + //[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed + //[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed (Ast) + //[mir]~| ERROR cannot use `v[..].y` because it was mutably borrowed (Mir) + //[mir]~| ERROR cannot use `(*v)` because it was mutably borrowed (Mir) + } + // Field of constant index + { + struct F {x: u32, y: u32}; + let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}]; + let _v = &mut v; + match v { + &[_, F {x: ref xf, ..}] => println!("{}", xf), + //[mir]~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable (Mir) + // No errors in AST + _ => panic!("other case") + } + } }