mir-borrowck: Implement end-user output for field of index projection

This commit is contained in:
Basile Desloges 2017-10-06 17:26:14 +02:00
parent ce3b2e779e
commit 9ce2f3af93
2 changed files with 25 additions and 10 deletions

View file

@ -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!("<index>{}", field_index)
},
ProjectionElem::ConstantIndex { .. } => {
debug!("End-user description not implemented for field of projection {:?}",
proj);
format!("<constant_index>{}", 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);

View file

@ -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")
}
}
}