Skip to content

Commit e5a96a4

Browse files
committed
modify the error message- CR Comments
1 parent e18a83b commit e5a96a4

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

Diff for: src/librustc_borrowck/borrowck/mod.rs

+1-1
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, false);
842+
let mut db = self.cannot_assign(error_span, &descr, Origin::Ast);
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);

Diff for: src/librustc_mir/borrow_check/mod.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -1506,20 +1506,35 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15061506
}
15071507

15081508
fn specialized_description(&self, place:&Place<'tcx>) -> Option<String>{
1509-
if let Some(name) = self.describe_place(place) {
1510-
Some(format!("`&`-reference `{}`", name))
1509+
if let Some(_name) = self.describe_place(place) {
1510+
Some(format!("data in a `&` reference"))
15111511
} else {
15121512
None
15131513
}
15141514
}
15151515

1516-
fn get_main_error_message(&self, place:&Place<'tcx>) -> String{
1516+
fn get_default_err_msg(&self, place:&Place<'tcx>) -> String{
15171517
match self.describe_place(place) {
15181518
Some(name) => format!("immutable item `{}`", name),
15191519
None => "immutable item".to_owned(),
15201520
}
15211521
}
15221522

1523+
fn get_secondary_err_msg(&self, place:&Place<'tcx>) -> String{
1524+
match self.specialized_description(place) {
1525+
Some(_) => format!("data in a `&` reference"),
1526+
None => self.get_default_err_msg(place)
1527+
}
1528+
}
1529+
1530+
fn get_primary_err_msg(&self, place:&Place<'tcx>) -> String{
1531+
if let Some(name) = self.describe_place(place) {
1532+
format!("`{}` is a `&` reference, so the data it refers to cannot be written", name)
1533+
} else {
1534+
format!("cannot assign through `&`-reference")
1535+
}
1536+
}
1537+
15231538
/// Check the permissions for the given place and read or write kind
15241539
///
15251540
/// Returns true if an error is reported, false otherwise.
@@ -1546,7 +1561,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15461561
self.is_mutable(place, is_local_mutation_allowed)
15471562
{
15481563
error_reported = true;
1549-
let item_msg = self.get_main_error_message(place);
1564+
let item_msg = self.get_default_err_msg(place);
15501565
let mut err = self.tcx
15511566
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir);
15521567
err.span_label(span, "cannot borrow as mutable");
@@ -1576,18 +1591,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15761591
let locations = self.mir.find_assignments(local);
15771592
if locations.len() > 0 {
15781593
let item_msg = if error_reported {
1579-
match self.specialized_description(base){
1580-
Some(msg) => msg,
1581-
None => self.get_main_error_message(place)
1582-
}
1594+
self.get_secondary_err_msg(base)
15831595
} else {
1584-
self.get_main_error_message(place)
1596+
self.get_default_err_msg(place)
15851597
};
1598+
15861599
err_info = Some((
15871600
self.mir.source_info(locations[0]).span,
15881601
"consider changing this to be a \
15891602
mutable reference: `&mut`", item_msg,
1590-
"cannot assign through `&`-reference"));
1603+
self.get_primary_err_msg(base)));
15911604
}
15921605
},
15931606
_ => {},
@@ -1597,15 +1610,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15971610
}
15981611

15991612
if let Some((err_help_span, err_help_stmt, item_msg, sec_span)) = err_info {
1600-
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir, true);
1613+
let mut err = self.tcx.cannot_assign(span, &item_msg, Origin::Mir);
16011614
err.span_suggestion(err_help_span, err_help_stmt, format!(""));
16021615
if place != place_err {
16031616
err.span_label(span, sec_span);
16041617
}
16051618
err.emit()
16061619
} else {
1607-
let item_msg_ = self.get_main_error_message(place);
1608-
let mut err = self.tcx.cannot_assign(span, &item_msg_, Origin::Mir, false);
1620+
let item_msg_ = self.get_default_err_msg(place);
1621+
let mut err = self.tcx.cannot_assign(span, &item_msg_, Origin::Mir);
16091622
err.span_label(span, "cannot mutate");
16101623
if place != place_err {
16111624
if let Some(name) = self.describe_place(place_err) {

Diff for: src/librustc_mir/util/borrowck_errors.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,19 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
284284
self.cancel_if_wrong_origin(err, o)
285285
}
286286

287-
fn cannot_assign(self, span: Span, desc: &str, o: Origin, is_reference: bool)
287+
fn cannot_assign(self, span: Span, desc: &str, o: Origin)
288288
-> DiagnosticBuilder<'cx>
289289
{
290-
let msg = if is_reference {
291-
"through"
292-
} else {
293-
"to"
294-
};
295-
296290
let err = struct_span_err!(self, span, E0594,
297-
"cannot assign {} {}{OGN}",
298-
msg, desc, OGN=o);
291+
"cannot assign to {}{OGN}",
292+
desc, OGN=o);
299293
self.cancel_if_wrong_origin(err, o)
300294
}
301295

302296
fn cannot_assign_static(self, span: Span, desc: &str, o: Origin)
303297
-> DiagnosticBuilder<'cx>
304298
{
305-
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o, false)
299+
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
306300
}
307301

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

Diff for: src/test/compile-fail/borrowck/borrowck-issue-14498.rs

+1-1
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 through `&`-reference `p`
30+
//[mir]~^ ERROR cannot assign to data in a `&` reference
3131
drop(p);
3232
}
3333

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

+1-1
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 through immutable item
73+
//[mir]~^^ ERROR cannot assign to immutable item
7474
}

Diff for: src/test/ui/nll/issue-47388.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0594]: cannot assign through `&`-reference `fancy_ref`
1+
error[E0594]: cannot assign to data in a `&` reference
22
--> $DIR/issue-47388.rs:18:5
33
|
44
LL | let fancy_ref = &(&mut fancy);
55
| ------------- help: consider changing this to be a mutable reference: `&mut`
66
LL | fancy_ref.num = 6; //~ ERROR E0594
7-
| ^^^^^^^^^^^^^^^^^ cannot assign through `&`-reference
7+
| ^^^^^^^^^^^^^^^^^ `fancy_ref` is a `&` reference, so the data it refers to cannot be written
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)