Skip to content

Commit 0c7fc04

Browse files
committed
code refactor, modify compile-fail tests
1 parent 7a266a6 commit 0c7fc04

File tree

6 files changed

+92
-56
lines changed

6 files changed

+92
-56
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
839839

840840
let mut db = match err.cause {
841841
MutabilityViolation => {
842-
let mut db = self.cannot_assign(error_span, &descr, Origin::Ast);
842+
let mut db = self.cannot_assign(error_span, &descr, Origin::Ast, false);
843843
if let mc::NoteClosureEnv(upvar_id) = err.cmt.note {
844844
let node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
845845
let sp = self.tcx.hir.span(node_id);

src/librustc_mir/borrow_check/mod.rs

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,13 @@ 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+
14251432
/// Currently MoveData does not store entries for all places in
14261433
/// the input MIR. For example it will currently filter out
14271434
/// places that are Copy; thus we do not track places of shared
@@ -1536,15 +1543,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15361543
is_local_mutation_allowed: LocalMutationIsAllowed,
15371544
) -> bool {
15381545
debug!(
1539-
<<<<<<< HEAD
15401546
"check_access_permissions({:?}, {:?}, {:?})",
15411547
place, kind, is_local_mutation_allowed
1542-
=======
1543-
" ({:?}, {:?}, {:?})",
1544-
place,
1545-
kind,
1546-
is_local_mutation_allowed
1547-
>>>>>>> minor changes
15481548
);
15491549
let mut error_reported = false;
15501550
match kind {
@@ -1559,11 +1559,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15591559
self.is_mutable(place, is_local_mutation_allowed)
15601560
{
15611561
error_reported = true;
1562-
let item_msg = match self.describe_place(place) {
1563-
Some(name) => format!("immutable item `{}`", name),
1564-
None => "immutable item".to_owned(),
1565-
};
1566-
1562+
let item_msg = self.get_main_error_message(place);
15671563
let mut err = self.tcx
15681564
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir);
15691565
err.span_label(span, "cannot borrow as mutable");
@@ -1580,42 +1576,61 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15801576
if let Err(place_err) = self.is_mutable(place, is_local_mutation_allowed) {
15811577
error_reported = true;
15821578

1583-
let err_help = match *place {
1584-
Place::Local(local) => {
1585-
let locations = self.mir.find_assignments(local);
1586-
Some((self.mir.source_info(locations[0]).span, "consider changing this to be a mutable reference: `&mut `"))
1587-
}
1588-
_ => {
1589-
None
1579+
let err_info = match *place_err {
1580+
Place::Projection(ref proj) => {
1581+
match proj.elem {
1582+
ProjectionElem::Deref => {
1583+
match proj.base {
1584+
Place::Local(local) => {
1585+
let locations = self.mir.find_assignments(local);
1586+
if locations.len() > 0 {
1587+
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)
1594+
}
1595+
} else {
1596+
self.get_main_error_message(place)
1597+
};
1598+
Some((self.mir.source_info(locations[0]).span,
1599+
"consider changing this to be a \
1600+
mutable reference: `&mut`", item_msg,
1601+
"cannot assign through `&`-reference"))
1602+
} else {
1603+
None
1604+
}
1605+
}
1606+
_ => None,
1607+
}
1608+
}
1609+
_ => None,
15901610
}
1611+
}
1612+
_ => None,
15911613
};
15921614

1593-
let item_msg = if error_reported{
1594-
if let Some(name) = self.describe_place(place_err) {
1595-
format!("`&`-reference {}", name)
1596-
}else{
1597-
match self.describe_place(place) {
1598-
Some(name) => {format!("immutable item `{}`", name)}
1599-
None => {"immutable item".to_owned()}
1600-
}
1601-
}
1615+
if let Some((err_help_span, err_help_stmt, item_msg, sec_span)) = err_info {
1616+
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir, true);
1617+
err.span_suggestion(err_help_span, err_help_stmt, format!(""));
1618+
if place != place_err {
1619+
err.span_label(span, sec_span);
1620+
}
1621+
err.emit()
16021622
}else{
1603-
match self.describe_place(place) {
1604-
Some(name) => {format!("immutable item `{}`", name)}
1605-
None => {"immutable item".to_owned()}
1623+
let item_msg_ = self.get_main_error_message(place);
1624+
let mut err = self.tcx.cannot_assign(span, &item_msg_, Origin::Mir, false);
1625+
err.span_label(span, "cannot mutate");
1626+
if place != place_err {
1627+
if let Some(name) = self.describe_place(place_err) {
1628+
err.note(&format!("Value not mutable causing this error: `{}`",
1629+
name));
1630+
}
16061631
}
1607-
};
1608-
1609-
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir);
1610-
1611-
if place != place_err {
1612-
err.span_label(span, "cannot assign through `&`-reference");
1632+
err.emit();
16131633
}
1614-
1615-
if !err_help.is_none(){
1616-
let (err_help_span, err_help_stmt) = err_help.unwrap();
1617-
err.span_help(err_help_span, err_help_stmt);}
1618-
err.emit();
16191634
}
16201635
}
16211636
Reservation(WriteKind::Move)

src/librustc_mir/util/borrowck_errors.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,25 @@ pub trait BorrowckErrors {
284284
self.cancel_if_wrong_origin(err, o)
285285
}
286286

287-
fn cannot_assign(&self, span: Span, desc: &str, o: Origin) -> DiagnosticBuilder
287+
fn cannot_assign(&self, span: Span, desc: &str, o: Origin, is_reference:bool)
288+
-> DiagnosticBuilder
288289
{
290+
let msg = if is_reference {
291+
"through"
292+
} else {
293+
"to"
294+
};
295+
289296
let err = struct_span_err!(self, span, E0594,
290-
"cannot assign to {}{OGN}",
291-
desc, OGN=o);
297+
"cannot assign {} {}{OGN}",
298+
msg, desc, OGN=o);
292299
self.cancel_if_wrong_origin(err, o)
293300
}
294301

295302
fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
296303
-> DiagnosticBuilder
297304
{
298-
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
305+
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o, false)
299306
}
300307

301308
fn cannot_move_out_of(&self, move_from_span: Span, move_from_desc: &str, o: Origin)

src/librustc_mir/util/collect_writes.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
17
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
28
// option. This file may not be copied, modified, or distributed
39
// except according to those terms.
@@ -7,6 +13,8 @@ use rustc::mir::Mir;
713
use rustc::mir::visit::PlaceContext;
814
use rustc::mir::visit::Visitor;
915

16+
// The Visitor walks the MIR to return the assignment statements corresponding
17+
// to a Local.
1018
pub struct FindLocalAssignmentVisitor {
1119
needle: Local,
1220
locations: Vec<Location>,
@@ -19,25 +27,32 @@ impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
1927
location: Location) {
2028
if self.needle != *local {
2129
return;
22-
}
30+
}
2331

2432
match place_context {
2533
PlaceContext::Store | PlaceContext::Call => {
2634
self.locations.push(location);
2735
}
28-
PlaceContext::AsmOutput | PlaceContext::Drop| PlaceContext::Inspect |
29-
PlaceContext::Borrow{..}| PlaceContext::Projection(..)| PlaceContext::Copy|
30-
PlaceContext::Move| PlaceContext::StorageLive| PlaceContext::StorageDead|
36+
PlaceContext::AsmOutput |
37+
PlaceContext::Drop |
38+
PlaceContext::Inspect |
39+
PlaceContext::Borrow { .. } |
40+
PlaceContext::Projection(..) |
41+
PlaceContext::Copy |
42+
PlaceContext::Move |
43+
PlaceContext::StorageLive |
44+
PlaceContext::StorageDead |
3145
PlaceContext::Validate => {
46+
// TO-DO
3247
// self.super_local(local)
3348
}
3449
}
3550
}
36-
51+
// TO-DO
3752
// fn super_local()
3853
}
3954

40-
crate trait FindAssignments {
55+
crate trait FindAssignments {
4156
fn find_assignments(&self, local: Local) -> Vec<Location>;
4257
}
4358

@@ -48,4 +63,3 @@ impl<'tcx> FindAssignments for Mir<'tcx>{
4863
visitor.locations
4964
}
5065
}
51-

src/test/compile-fail/borrowck/borrowck-issue-14498.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn indirect_write_to_imm_box() {
2727
let y: Box<_> = box &mut x;
2828
let p = &y;
2929
***p = 2; //[ast]~ ERROR cannot assign to data in a `&` reference
30-
//[mir]~^ ERROR cannot assign to immutable item `***p`
30+
//[mir]~^ ERROR cannot assign through `&`-reference `p`
3131
drop(p);
3232
}
3333

src/test/compile-fail/borrowck/borrowck-overloaded-index-ref-index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ fn main() {
7070
};
7171
s[2] = 20;
7272
//[ast]~^ ERROR cannot assign to immutable indexed content
73-
//[mir]~^^ ERROR cannot assign to immutable item
73+
//[mir]~^^ ERROR cannot assign through immutable item
7474
}

0 commit comments

Comments
 (0)