|
10 | 10 |
|
11 | 11 | use syntax_pos::Span;
|
12 | 12 | use rustc::middle::region::ScopeTree;
|
13 |
| -use rustc::mir::{BorrowKind, Field, Local, Location, Operand}; |
| 13 | +use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand}; |
14 | 14 | use rustc::mir::{Place, ProjectionElem, Rvalue, Statement, StatementKind};
|
15 | 15 | use rustc::ty::{self, RegionKind};
|
16 | 16 | use rustc_data_structures::indexed_vec::Idx;
|
@@ -573,14 +573,28 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
573 | 573 | &self.describe_place(place).unwrap_or("_".to_owned()),
|
574 | 574 | Origin::Mir,
|
575 | 575 | );
|
576 |
| - err.span_label(span, "cannot assign twice to immutable variable"); |
| 576 | + let mut msg = "cannot assign twice to immutable variable"; |
577 | 577 | 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 |
581 | 588 | };
|
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 | + } |
583 | 596 | }
|
| 597 | + err.span_label(span, msg); |
584 | 598 | err.emit();
|
585 | 599 | }
|
586 | 600 | }
|
|
0 commit comments