Skip to content

Commit 9ce2f3a

Browse files
committed
mir-borrowck: Implement end-user output for field of index projection
1 parent ce3b2e7 commit 9ce2f3a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,20 +1125,12 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
11251125
match proj.elem {
11261126
ProjectionElem::Deref =>
11271127
self.describe_field(&proj.base, field_index),
1128-
ProjectionElem::Index(..) => {
1129-
debug!("End-user description not implemented for field of projection {:?}",
1130-
proj);
1131-
format!("<index>{}", field_index)
1132-
},
1133-
ProjectionElem::ConstantIndex { .. } => {
1134-
debug!("End-user description not implemented for field of projection {:?}",
1135-
proj);
1136-
format!("<constant_index>{}", field_index)
1137-
},
11381128
ProjectionElem::Downcast(def, variant_index) =>
11391129
format!("{}", def.variants[variant_index].fields[field_index].name),
11401130
ProjectionElem::Field(_, field_type) =>
11411131
self.describe_field_from_ty(&field_type, field_index),
1132+
ProjectionElem::Index(..) | ProjectionElem::ConstantIndex { .. } =>
1133+
format!("{}", self.describe_field(&proj.base, field_index)),
11421134
ProjectionElem::Subslice { .. } => {
11431135
debug!("End-user description not implemented for field of projection {:?}",
11441136
proj);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,27 @@ fn main() {
276276
_ => panic!("other case"),
277277
}
278278
}
279+
// Field of index
280+
{
281+
struct F {x: u32, y: u32};
282+
let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
283+
let _v = &mut v;
284+
v[0].y;
285+
//[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
286+
//[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed (Ast)
287+
//[mir]~| ERROR cannot use `v[..].y` because it was mutably borrowed (Mir)
288+
//[mir]~| ERROR cannot use `(*v)` because it was mutably borrowed (Mir)
289+
}
290+
// Field of constant index
291+
{
292+
struct F {x: u32, y: u32};
293+
let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
294+
let _v = &mut v;
295+
match v {
296+
&[_, F {x: ref xf, ..}] => println!("{}", xf),
297+
//[mir]~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable (Mir)
298+
// No errors in AST
299+
_ => panic!("other case")
300+
}
301+
}
279302
}

0 commit comments

Comments
 (0)