Skip to content

Commit 96e9cee

Browse files
committed
Auto merge of #46040 - zilbuz:mir-misc, r=nikomatsakis
MIR-borrowck: Some minor fixes - Remove parens when printing dereference (fix #45185) - Change argument type of `autoderef` to `bool` - Change argument type of `field_index` to `Field`
2 parents d6d09e0 + 2285e35 commit 96e9cee

6 files changed

+40
-44
lines changed

src/librustc_mir/borrow_check.rs

+27-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::hir::def_id::{DefId};
1515
use rustc::infer::{InferCtxt};
1616
use rustc::ty::{self, TyCtxt, ParamEnv};
1717
use rustc::ty::maps::Providers;
18-
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue, Local};
18+
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Field, Location, Lvalue, Local};
1919
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
2020
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
2121
use transform::nll;
@@ -1577,15 +1577,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15771577
// End-user visible description of `lvalue`
15781578
fn describe_lvalue(&self, lvalue: &Lvalue<'tcx>) -> String {
15791579
let mut buf = String::new();
1580-
self.append_lvalue_to_string(lvalue, &mut buf, None);
1580+
self.append_lvalue_to_string(lvalue, &mut buf, false);
15811581
buf
15821582
}
15831583

15841584
// Appends end-user visible description of `lvalue` to `buf`.
15851585
fn append_lvalue_to_string(&self,
15861586
lvalue: &Lvalue<'tcx>,
15871587
buf: &mut String,
1588-
autoderef: Option<bool>) {
1588+
mut autoderef: bool) {
15891589
match *lvalue {
15901590
Lvalue::Local(local) => {
15911591
self.append_local_to_string(local, buf, "_");
@@ -1594,38 +1594,35 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15941594
buf.push_str(&format!("{}", &self.tcx.item_name(static_.def_id)));
15951595
}
15961596
Lvalue::Projection(ref proj) => {
1597-
let mut autoderef = autoderef.unwrap_or(false);
1598-
15991597
match proj.elem {
16001598
ProjectionElem::Deref => {
16011599
if autoderef {
1602-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1600+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16031601
} else {
1604-
buf.push_str(&"(*");
1605-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1606-
buf.push_str(&")");
1602+
buf.push_str(&"*");
1603+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16071604
}
16081605
},
16091606
ProjectionElem::Downcast(..) => {
1610-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1607+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16111608
},
16121609
ProjectionElem::Field(field, _ty) => {
16131610
autoderef = true;
16141611
let is_projection_from_ty_closure = proj.base.ty(self.mir, self.tcx)
16151612
.to_ty(self.tcx).is_closure();
16161613

1617-
let field_name = self.describe_field(&proj.base, field.index());
1614+
let field_name = self.describe_field(&proj.base, field);
16181615
if is_projection_from_ty_closure {
16191616
buf.push_str(&format!("{}", field_name));
16201617
} else {
1621-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1618+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16221619
buf.push_str(&format!(".{}", field_name));
16231620
}
16241621
},
16251622
ProjectionElem::Index(index) => {
16261623
autoderef = true;
16271624

1628-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1625+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16291626
buf.push_str("[");
16301627
self.append_local_to_string(index, buf, "..");
16311628
buf.push_str("]");
@@ -1635,7 +1632,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16351632
// Since it isn't possible to borrow an element on a particular index and
16361633
// then use another while the borrow is held, don't output indices details
16371634
// to avoid confusing the end-user
1638-
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));
1635+
self.append_lvalue_to_string(&proj.base, buf, autoderef);
16391636
buf.push_str(&"[..]");
16401637
},
16411638
};
@@ -1653,66 +1650,65 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16531650
}
16541651
}
16551652

1656-
// FIXME Instead of passing usize, Field should be passed
1657-
// End-user visible description of the `field_index`nth field of `base`
1658-
fn describe_field(&self, base: &Lvalue, field_index: usize) -> String {
1653+
// End-user visible description of the `field`nth field of `base`
1654+
fn describe_field(&self, base: &Lvalue, field: Field) -> String {
16591655
match *base {
16601656
Lvalue::Local(local) => {
16611657
let local = &self.mir.local_decls[local];
1662-
self.describe_field_from_ty(&local.ty, field_index)
1658+
self.describe_field_from_ty(&local.ty, field)
16631659
},
16641660
Lvalue::Static(ref static_) => {
1665-
self.describe_field_from_ty(&static_.ty, field_index)
1661+
self.describe_field_from_ty(&static_.ty, field)
16661662
},
16671663
Lvalue::Projection(ref proj) => {
16681664
match proj.elem {
16691665
ProjectionElem::Deref =>
1670-
self.describe_field(&proj.base, field_index),
1666+
self.describe_field(&proj.base, field),
16711667
ProjectionElem::Downcast(def, variant_index) =>
1672-
format!("{}", def.variants[variant_index].fields[field_index].name),
1668+
format!("{}", def.variants[variant_index].fields[field.index()].name),
16731669
ProjectionElem::Field(_, field_type) =>
1674-
self.describe_field_from_ty(&field_type, field_index),
1670+
self.describe_field_from_ty(&field_type, field),
16751671
ProjectionElem::Index(..)
16761672
| ProjectionElem::ConstantIndex { .. }
16771673
| ProjectionElem::Subslice { .. } =>
1678-
format!("{}", self.describe_field(&proj.base, field_index)),
1674+
format!("{}", self.describe_field(&proj.base, field)),
16791675
}
16801676
}
16811677
}
16821678
}
16831679

16841680
// End-user visible description of the `field_index`nth field of `ty`
1685-
fn describe_field_from_ty(&self, ty: &ty::Ty, field_index: usize) -> String {
1681+
fn describe_field_from_ty(&self, ty: &ty::Ty, field: Field) -> String {
16861682
if ty.is_box() {
16871683
// If the type is a box, the field is described from the boxed type
1688-
self.describe_field_from_ty(&ty.boxed_ty(), field_index)
1684+
self.describe_field_from_ty(&ty.boxed_ty(), field)
16891685
}
16901686
else {
16911687
match ty.sty {
16921688
ty::TyAdt(def, _) => {
16931689
if def.is_enum() {
1694-
format!("{}", field_index)
1690+
format!("{}", field.index())
16951691
}
16961692
else {
1697-
format!("{}", def.struct_variant().fields[field_index].name)
1693+
format!("{}", def.struct_variant().fields[field.index()].name)
16981694
}
16991695
},
17001696
ty::TyTuple(_, _) => {
1701-
format!("{}", field_index)
1697+
format!("{}", field.index())
17021698
},
17031699
ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => {
1704-
self.describe_field_from_ty(&tnm.ty, field_index)
1700+
self.describe_field_from_ty(&tnm.ty, field)
17051701
},
17061702
ty::TyArray(ty, _) | ty::TySlice(ty) => {
1707-
self.describe_field_from_ty(&ty, field_index)
1703+
self.describe_field_from_ty(&ty, field)
17081704
},
17091705
ty::TyClosure(closure_def_id, _) => {
17101706
// Convert the def-id into a node-id. node-ids are only valid for
17111707
// the local code in the current crate, so this returns an `Option` in case
17121708
// the closure comes from another crate. But in that case we wouldn't
17131709
// be borrowck'ing it, so we can just unwrap:
17141710
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
1715-
let freevar = self.tcx.with_freevars(node_id, |fv| fv[field_index]);
1711+
let freevar = self.tcx.with_freevars(node_id, |fv| fv[field.index()]);
17161712

17171713
self.tcx.hir.name(freevar.var_id()).to_string()
17181714
}

src/test/compile-fail/borrowck/borrowck-closures-mut-and-imm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn f() {
7070
let c1 = || get(&*x);
7171
*x = 5; //[ast]~ ERROR cannot assign
7272
//[mir]~^ ERROR cannot assign to `*x` because it is borrowed (Ast)
73-
//[mir]~| ERROR cannot assign to `(*x)` because it is borrowed (Mir)
73+
//[mir]~| ERROR cannot assign to `*x` because it is borrowed (Mir)
7474
}
7575

7676
fn g() {
@@ -82,7 +82,7 @@ fn g() {
8282
let c1 = || get(&*x.f);
8383
*x.f = 5; //[ast]~ ERROR cannot assign to `*x.f`
8484
//[mir]~^ ERROR cannot assign to `*x.f` because it is borrowed (Ast)
85-
//[mir]~| ERROR cannot assign to `(*x.f)` because it is borrowed (Mir)
85+
//[mir]~| ERROR cannot assign to `*x.f` because it is borrowed (Mir)
8686
}
8787

8888
fn h() {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn main() {
252252
fn bump<'a>(mut block: &mut Block<'a>) {
253253
let x = &mut block;
254254
let p: &'a u8 = &*block.current;
255-
//[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir)
255+
//[mir]~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable (Mir)
256256
// No errors in AST because of issue rust#38899
257257
}
258258
}
@@ -266,7 +266,7 @@ fn main() {
266266
unsafe fn bump2(mut block: *mut Block2) {
267267
let x = &mut block;
268268
let p : *const u8 = &*(*block).current;
269-
//[mir]~^ ERROR cannot borrow `(*block.current)` as immutable because it is also borrowed as mutable (Mir)
269+
//[mir]~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable (Mir)
270270
// No errors in AST because of issue rust#38899
271271
}
272272
}
@@ -279,7 +279,7 @@ fn main() {
279279
//[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
280280
//[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed (Ast)
281281
//[mir]~| ERROR cannot use `v[..].y` because it was mutably borrowed (Mir)
282-
//[mir]~| ERROR cannot use `(*v)` because it was mutably borrowed (Mir)
282+
//[mir]~| ERROR cannot use `*v` because it was mutably borrowed (Mir)
283283
}
284284
// Field of constant index
285285
{
@@ -300,7 +300,7 @@ fn main() {
300300
let y = &mut x;
301301
&mut x; //[ast]~ ERROR cannot borrow `**x` as mutable more than once at a time
302302
//[mir]~^ ERROR cannot borrow `**x` as mutable more than once at a time (Ast)
303-
//[mir]~| ERROR cannot borrow `(*x)` as mutable more than once at a time (Mir)
303+
//[mir]~| ERROR cannot borrow `*x` as mutable more than once at a time (Mir)
304304
*y = 1;
305305
};
306306
}
@@ -312,7 +312,7 @@ fn main() {
312312
let y = &mut x;
313313
&mut x; //[ast]~ ERROR cannot borrow `**x` as mutable more than once at a time
314314
//[mir]~^ ERROR cannot borrow `**x` as mutable more than once at a time (Ast)
315-
//[mir]~| ERROR cannot borrow `(*x)` as mutable more than once at a time (Mir)
315+
//[mir]~| ERROR cannot borrow `*x` as mutable more than once at a time (Mir)
316316
*y = 1;
317317
}
318318
};

src/test/compile-fail/coerce-overloaded-autoderef.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn double_mut_borrow<T>(x: &mut Box<T>) {
2222
let z = borrow_mut(x);
2323
//[ast]~^ ERROR cannot borrow `*x` as mutable more than once at a time
2424
//[mir]~^^ ERROR cannot borrow `*x` as mutable more than once at a time (Ast)
25-
//[mir]~| ERROR cannot borrow `(*x)` as mutable more than once at a time (Mir)
25+
//[mir]~| ERROR cannot borrow `*x` as mutable more than once at a time (Mir)
2626
}
2727

2828
fn double_imm_borrow(x: &mut Box<i32>) {
@@ -31,21 +31,21 @@ fn double_imm_borrow(x: &mut Box<i32>) {
3131
**x += 1;
3232
//[ast]~^ ERROR cannot assign to `**x` because it is borrowed
3333
//[mir]~^^ ERROR cannot assign to `**x` because it is borrowed (Ast)
34-
//[mir]~| ERROR cannot assign to `(*(*x))` because it is borrowed (Mir)
34+
//[mir]~| ERROR cannot assign to `**x` because it is borrowed (Mir)
3535
}
3636

3737
fn double_mut_borrow2<T>(x: &mut Box<T>) {
3838
borrow_mut2(x, x);
3939
//[ast]~^ ERROR cannot borrow `*x` as mutable more than once at a time
4040
//[mir]~^^ ERROR cannot borrow `*x` as mutable more than once at a time (Ast)
41-
//[mir]~| ERROR cannot borrow `(*x)` as mutable more than once at a time (Mir)
41+
//[mir]~| ERROR cannot borrow `*x` as mutable more than once at a time (Mir)
4242
}
4343

4444
fn double_borrow2<T>(x: &mut Box<T>) {
4545
borrow2(x, x);
4646
//[ast]~^ ERROR cannot borrow `*x` as immutable because it is also borrowed as mutable
4747
//[mir]~^^ ERROR cannot borrow `*x` as immutable because it is also borrowed as mutable (Ast)
48-
//[mir]~| ERROR cannot borrow `(*x)` as immutable because it is also borrowed as mutable (Mir)
48+
//[mir]~| ERROR cannot borrow `*x` as immutable because it is also borrowed as mutable (Mir)
4949
}
5050

5151
pub fn main() {}

src/test/compile-fail/mut-pattern-internal-mutability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ fn main() {
2727
let &mut ref x = foo;
2828
*foo += 1; //[ast]~ ERROR cannot assign to `*foo` because it is borrowed
2929
//[mir]~^ ERROR cannot assign to `*foo` because it is borrowed (Ast)
30-
//[mir]~| ERROR cannot assign to `(*foo)` because it is borrowed (Mir)
30+
//[mir]~| ERROR cannot assign to `*foo` because it is borrowed (Mir)
3131
}

src/test/ui/nll/get_default.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ error[E0502]: cannot borrow `*map` as mutable because it is also borrowed as imm
3434
51 | }
3535
| - immutable borrow ends here
3636

37-
error[E0502]: cannot borrow `(*map)` as mutable because it is also borrowed as immutable (Mir)
37+
error[E0502]: cannot borrow `*map` as mutable because it is also borrowed as immutable (Mir)
3838
--> $DIR/get_default.rs:43:17
3939
|
4040
41 | match map.get() {

0 commit comments

Comments
 (0)