From ca5dc86c5a2e92f35c94e7a1f4592bb7fd0b271c Mon Sep 17 00:00:00 2001 From: Basile Desloges Date: Fri, 6 Oct 2017 17:26:53 +0200 Subject: [PATCH] mir-borrowck: Implement end-user output for field of reference, pointer and array --- src/librustc_mir/borrow_check.rs | 9 ++++-- .../borrowck/borrowck-describe-lvalue.rs | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs index 0d9dbfab1e8..eb05a815c73 100644 --- a/src/librustc_mir/borrow_check.rs +++ b/src/librustc_mir/borrow_check.rs @@ -1160,11 +1160,16 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> ty::TyTuple(_, _) => { format!("{}", field_index) }, + ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => { + self.describe_field_from_ty(&tnm.ty, field_index) + }, + ty::TyArray(ty, _) => { + self.describe_field_from_ty(&ty, field_index) + } _ => { // Might need a revision when the fields in trait RFC is implemented // (https://github.com/rust-lang/rfcs/pull/1546) - bug!("Field access unsupported for non-box types that are neither Adt (struct, \ - enum, union) nor tuples"); + bug!("End-user description not implemented for field access on `{:?}`", ty.sty); } } } diff --git a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs index 7ead9032c13..cff913b17be 100644 --- a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs +++ b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs @@ -276,6 +276,34 @@ fn main() { _ => panic!("other case"), } } + // Field of ref + { + struct Block<'a> { + current: &'a u8, + unrelated: &'a u8, + }; + + fn bump<'a>(mut block: &mut Block<'a>) { + let x = &mut block; + let p: &'a u8 = &*block.current; + //[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir) + // No errors in AST because of issue rust#38899 + } + } + // Field of ptr + { + struct Block2 { + current: *const u8, + unrelated: *const u8, + } + + unsafe fn bump2(mut block: *mut Block2) { + let x = &mut block; + let p : *const u8 = &*(*block).current; + //[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir) + // No errors in AST because of issue rust#38899 + } + } // Field of index { struct F {x: u32, y: u32};