Skip to content

Commit 6c649fb

Browse files
committed
address code review comments
1 parent 311a8be commit 6c649fb

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,13 +1422,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
14221422
}
14231423
}
14241424

1425-
fn get_main_error_message(&self, place:&Place<'tcx>) -> String{
1426-
match self.describe_place(place) {
1427-
Some(name) => format!("immutable item `{}`", name),
1428-
None => "immutable item".to_owned(),
1429-
}
1430-
}
1431-
14321425
/// Currently MoveData does not store entries for all places in
14331426
/// the input MIR. For example it will currently filter out
14341427
/// places that are Copy; thus we do not track places of shared
@@ -1533,6 +1526,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15331526
}
15341527
}
15351528

1529+
fn specialized_description(&self, place:&Place<'tcx>) -> Option<String>{
1530+
if let Some(name) = self.describe_place(place) {
1531+
Some(format!("`&`-reference `{}`", name))
1532+
} else {
1533+
None
1534+
}
1535+
}
1536+
1537+
fn get_main_error_message(&self, place:&Place<'tcx>) -> String{
1538+
match self.describe_place(place) {
1539+
Some(name) => format!("immutable item `{}`", name),
1540+
None => "immutable item".to_owned(),
1541+
}
1542+
}
1543+
15361544
/// Check the permissions for the given place and read or write kind
15371545
///
15381546
/// Returns true if an error is reported, false otherwise.
@@ -1566,7 +1574,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15661574

15671575
if place != place_err {
15681576
if let Some(name) = self.describe_place(place_err) {
1569-
err.note(&format!("value not mutable causing this error: `{}`", name));
1577+
err.note(&format!("the value which is causing this path not to be mutable is...: `{}`", name));
15701578
}
15711579
}
15721580

@@ -1576,7 +1584,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15761584
if let Err(place_err) = self.is_mutable(place, is_local_mutation_allowed) {
15771585
error_reported = true;
15781586

1579-
let err_info = match *place_err {
1587+
let mut err_info = None;
1588+
match *place_err {
15801589
Place::Projection(ref proj) => {
15811590
match proj.elem {
15821591
ProjectionElem::Deref => {
@@ -1585,32 +1594,27 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15851594
let locations = self.mir.find_assignments(local);
15861595
if locations.len() > 0 {
15871596
let item_msg = if error_reported {
1588-
if let Some(name) =
1589-
self.describe_place(place_err) {
1590-
let var = str::replace(&name, "*", "");
1591-
format!("`&`-reference `{}`", var)
1592-
} else {
1593-
self.get_main_error_message(place)
1597+
match self.specialized_description(&proj.base){
1598+
Some(msg) => msg,
1599+
None => self.get_main_error_message(place)
15941600
}
15951601
} else {
15961602
self.get_main_error_message(place)
15971603
};
1598-
Some((self.mir.source_info(locations[0]).span,
1604+
err_info = Some((self.mir.source_info(locations[0]).span,
15991605
"consider changing this to be a \
16001606
mutable reference: `&mut`", item_msg,
1601-
"cannot assign through `&`-reference"))
1602-
} else {
1603-
None
1607+
"cannot assign through `&`-reference"));
16041608
}
16051609
}
1606-
_ => None,
1610+
_ => {},
16071611
}
16081612
}
1609-
_ => None,
1613+
_ => {}
16101614
}
16111615
}
1612-
_ => None,
1613-
};
1616+
_ => {}
1617+
}
16141618

16151619
if let Some((err_help_span, err_help_stmt, item_msg, sec_span)) = err_info {
16161620
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir, true);
@@ -1625,7 +1629,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16251629
err.span_label(span, "cannot mutate");
16261630
if place != place_err {
16271631
if let Some(name) = self.describe_place(place_err) {
1628-
err.note(&format!("value not mutable causing this error: `{}`",
1632+
err.note(&format!("the value which is causing this path not to be mutable is...: `{}`",
16291633
name));
16301634
}
16311635
}

src/librustc_mir/util/collect_writes.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@ use rustc::mir::Mir;
1313
use rustc::mir::visit::PlaceContext;
1414
use rustc::mir::visit::Visitor;
1515

16+
crate trait FindAssignments {
17+
// Finds all statements that assign directly to local (i.e., X = ...)
18+
// and returns their locations.
19+
fn find_assignments(&self, local: Local) -> Vec<Location>;
20+
}
21+
22+
impl<'tcx> FindAssignments for Mir<'tcx>{
23+
fn find_assignments(&self, local: Local) -> Vec<Location>{
24+
let mut visitor = FindLocalAssignmentVisitor{ needle: local, locations: vec![]};
25+
visitor.visit_mir(self);
26+
visitor.locations
27+
}
28+
}
29+
1630
// The Visitor walks the MIR to return the assignment statements corresponding
1731
// to a Local.
18-
pub struct FindLocalAssignmentVisitor {
32+
struct FindLocalAssignmentVisitor {
1933
needle: Local,
2034
locations: Vec<Location>,
2135
}
@@ -51,15 +65,3 @@ impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
5165
// TO-DO
5266
// fn super_local()
5367
}
54-
55-
crate trait FindAssignments {
56-
fn find_assignments(&self, local: Local) -> Vec<Location>;
57-
}
58-
59-
impl<'tcx> FindAssignments for Mir<'tcx>{
60-
fn find_assignments(&self, local: Local) -> Vec<Location>{
61-
let mut visitor = FindLocalAssignmentVisitor{ needle: local, locations: vec![]};
62-
visitor.visit_mir(self);
63-
visitor.locations
64-
}
65-
}

0 commit comments

Comments
 (0)