Skip to content

Commit 90bc98c

Browse files
committed
Modify message to match label
1 parent 1890818 commit 90bc98c

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

src/librustc_borrowck/borrowck/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
772772
&move_data::Assignment) {
773773
let mut err = self.cannot_reassign_immutable(span,
774774
&self.loan_path_to_string(lp),
775+
false,
775776
Origin::Ast);
776777
err.span_label(span, "cannot assign twice to immutable variable");
777778
if span != assign.span {

src/librustc_mir/borrow_check/error_reporting.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -568,25 +568,31 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
568568
(place, span): (&Place<'tcx>, Span),
569569
assigned_span: Span,
570570
) {
571+
let is_arg = if let Place::Local(local) = place {
572+
if let LocalKind::Arg = self.mir.local_kind(*local) {
573+
true
574+
} else {
575+
false
576+
}
577+
} else {
578+
false
579+
};
580+
571581
let mut err = self.tcx.cannot_reassign_immutable(
572582
span,
573583
&self.describe_place(place).unwrap_or("_".to_owned()),
584+
is_arg,
574585
Origin::Mir,
575586
);
576-
let mut msg = "cannot assign twice to immutable variable";
587+
let msg = if is_arg {
588+
"cannot assign to immutable argument"
589+
} else {
590+
"cannot assign twice to immutable variable"
591+
};
577592
if span != assigned_span {
578-
let suggestion = if let Place::Local(local) = place {
579-
if let LocalKind::Arg = self.mir.local_kind(*local) {
580-
msg = "cannot assign to immutable argument";
581-
err.span_label(assigned_span, "argument not declared as `mut`");
582-
true
583-
} else {
584-
false
585-
}
593+
if is_arg {
594+
err.span_label(assigned_span, "argument not declared as `mut`");
586595
} else {
587-
false
588-
};
589-
if !suggestion {
590596
let value_msg = match self.describe_place(place) {
591597
Some(name) => format!("`{}`", name),
592598
None => "value".to_owned(),

src/librustc_mir/util/borrowck_errors.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,17 @@ pub trait BorrowckErrors {
269269
self.cancel_if_wrong_origin(err, o)
270270
}
271271

272-
fn cannot_reassign_immutable(&self, span: Span, desc: &str, o: Origin)
272+
fn cannot_reassign_immutable(&self, span: Span, desc: &str, is_arg: bool, o: Origin)
273273
-> DiagnosticBuilder
274274
{
275+
let msg = if is_arg {
276+
"to immutable argument"
277+
} else {
278+
"twice to immutable variable"
279+
};
275280
let err = struct_span_err!(self, span, E0384,
276-
"cannot assign twice to immutable variable `{}`{OGN}",
277-
desc, OGN=o);
281+
"cannot assign {} `{}`{OGN}",
282+
msg, desc, OGN=o);
278283

279284
self.cancel_if_wrong_origin(err, o)
280285
}

src/test/compile-fail/issue-45199.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn test_call() {
3333
fn test_args(b: Box<i32>) { //[ast]~ NOTE first assignment
3434
//[mir]~^ NOTE argument not declared as `mut`
3535
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
36-
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
36+
//[mir]~^ ERROR cannot assign to immutable argument `b`
3737
//[ast]~| NOTE cannot assign twice to immutable
3838
//[mir]~| NOTE cannot assign to immutable argument
3939
}

src/test/ui/borrowck/immutable-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
fn foo(_x: u32) {
1414
_x = 4;
15-
//~^ ERROR cannot assign twice to immutable variable `_x` (Mir)
15+
//~^ ERROR cannot assign to immutable argument `_x` (Mir)
1616
//~^^ ERROR cannot assign twice to immutable variable `_x` (Ast)
1717
}
1818

src/test/ui/borrowck/immutable-arg.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ error[E0384]: cannot assign twice to immutable variable `_x` (Ast)
66
14 | _x = 4;
77
| ^^^^^^ cannot assign twice to immutable variable
88

9-
error[E0384]: cannot assign twice to immutable variable `_x` (Mir)
9+
error[E0384]: cannot assign to immutable argument `_x` (Mir)
1010
--> $DIR/immutable-arg.rs:14:5
1111
|
1212
13 | fn foo(_x: u32) {

0 commit comments

Comments
 (0)