Skip to content

Commit ca5dc86

Browse files
committed
mir-borrowck: Implement end-user output for field of reference, pointer and array
1 parent 9ce2f3a commit ca5dc86

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/librustc_mir/borrow_check.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1160,11 +1160,16 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
11601160
ty::TyTuple(_, _) => {
11611161
format!("{}", field_index)
11621162
},
1163+
ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => {
1164+
self.describe_field_from_ty(&tnm.ty, field_index)
1165+
},
1166+
ty::TyArray(ty, _) => {
1167+
self.describe_field_from_ty(&ty, field_index)
1168+
}
11631169
_ => {
11641170
// Might need a revision when the fields in trait RFC is implemented
11651171
// (https://github.com/rust-lang/rfcs/pull/1546)
1166-
bug!("Field access unsupported for non-box types that are neither Adt (struct, \
1167-
enum, union) nor tuples");
1172+
bug!("End-user description not implemented for field access on `{:?}`", ty.sty);
11681173
}
11691174
}
11701175
}

src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs

+28
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,34 @@ fn main() {
276276
_ => panic!("other case"),
277277
}
278278
}
279+
// Field of ref
280+
{
281+
struct Block<'a> {
282+
current: &'a u8,
283+
unrelated: &'a u8,
284+
};
285+
286+
fn bump<'a>(mut block: &mut Block<'a>) {
287+
let x = &mut block;
288+
let p: &'a u8 = &*block.current;
289+
//[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir)
290+
// No errors in AST because of issue rust#38899
291+
}
292+
}
293+
// Field of ptr
294+
{
295+
struct Block2 {
296+
current: *const u8,
297+
unrelated: *const u8,
298+
}
299+
300+
unsafe fn bump2(mut block: *mut Block2) {
301+
let x = &mut block;
302+
let p : *const u8 = &*(*block).current;
303+
//[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir)
304+
// No errors in AST because of issue rust#38899
305+
}
306+
}
279307
// Field of index
280308
{
281309
struct F {x: u32, y: u32};

0 commit comments

Comments
 (0)