Skip to content

Commit 1890818

Browse files
committed
Assignment to immutable argument: diagnostic tweak
1 parent ee220da commit 1890818

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
776776
err.span_label(span, "cannot assign twice to immutable variable");
777777
if span != assign.span {
778778
err.span_label(assign.span, format!("first assignment to `{}`",
779-
self.loan_path_to_string(lp)));
779+
self.loan_path_to_string(lp)));
780780
}
781781
err.emit();
782782
}

src/librustc_mir/borrow_check/error_reporting.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use syntax_pos::Span;
1212
use rustc::middle::region::ScopeTree;
13-
use rustc::mir::{BorrowKind, Field, Local, Location, Operand};
13+
use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand};
1414
use rustc::mir::{Place, ProjectionElem, Rvalue, Statement, StatementKind};
1515
use rustc::ty::{self, RegionKind};
1616
use rustc_data_structures::indexed_vec::Idx;
@@ -573,14 +573,28 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
573573
&self.describe_place(place).unwrap_or("_".to_owned()),
574574
Origin::Mir,
575575
);
576-
err.span_label(span, "cannot assign twice to immutable variable");
576+
let mut msg = "cannot assign twice to immutable variable";
577577
if span != assigned_span {
578-
let value_msg = match self.describe_place(place) {
579-
Some(name) => format!("`{}`", name),
580-
None => "value".to_owned(),
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+
}
586+
} else {
587+
false
581588
};
582-
err.span_label(assigned_span, format!("first assignment to {}", value_msg));
589+
if !suggestion {
590+
let value_msg = match self.describe_place(place) {
591+
Some(name) => format!("`{}`", name),
592+
None => "value".to_owned(),
593+
};
594+
err.span_label(assigned_span, format!("first assignment to {}", value_msg));
595+
}
583596
}
597+
err.span_label(span, msg);
584598
err.emit();
585599
}
586600
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ fn test_call() {
3131
}
3232

3333
fn test_args(b: Box<i32>) { //[ast]~ NOTE first assignment
34-
//[mir]~^ NOTE first assignment
34+
//[mir]~^ NOTE argument not declared as `mut`
3535
b = Box::new(2); //[ast]~ ERROR cannot assign twice to immutable variable
3636
//[mir]~^ ERROR cannot assign twice to immutable variable `b`
3737
//[ast]~| NOTE cannot assign twice to immutable
38-
//[mir]~| NOTE cannot assign twice to immutable
38+
//[mir]~| NOTE cannot assign to immutable argument
3939
}
4040

4141
fn main() {}

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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//compile-flags: -Z emit-end-regions -Z borrowck=compare
12+
13+
fn foo(_x: u32) {
14+
_x = 4;
15+
//~^ ERROR cannot assign twice to immutable variable `_x` (Mir)
16+
//~^^ ERROR cannot assign twice to immutable variable `_x` (Ast)
17+
}
18+
19+
fn main() {}
20+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0384]: cannot assign twice to immutable variable `_x` (Ast)
2+
--> $DIR/immutable-arg.rs:14:5
3+
|
4+
13 | fn foo(_x: u32) {
5+
| -- first assignment to `_x`
6+
14 | _x = 4;
7+
| ^^^^^^ cannot assign twice to immutable variable
8+
9+
error[E0384]: cannot assign twice to immutable variable `_x` (Mir)
10+
--> $DIR/immutable-arg.rs:14:5
11+
|
12+
13 | fn foo(_x: u32) {
13+
| -- argument not declared as `mut`
14+
14 | _x = 4;
15+
| ^^^^^^ cannot assign to immutable argument
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)