Skip to content

Commit 4890ae2

Browse files
committed
Do not ICE on missing access place description during mutability error reporting
1 parent 02f5786 commit 4890ae2

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/librustc_mir/borrow_check/mutability_errors.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
4141
);
4242

4343
let mut err;
44-
let item_msg;
44+
let mut item_msg;
4545
let reason;
4646
let access_place_desc = self.describe_place(access_place);
4747
debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);
4848

49+
item_msg = match &access_place_desc {
50+
Some(desc) => format!("`{}`", desc),
51+
None => "temporary place".to_string(),
52+
};
4953
match the_place_err {
5054
Place::Base(PlaceBase::Local(local)) => {
51-
item_msg = format!("`{}`", access_place_desc.unwrap());
5255
if let Place::Base(PlaceBase::Local(_)) = access_place {
5356
reason = ", as it is not declared as mutable".to_string();
5457
} else {
@@ -67,7 +70,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
6770
base.ty(self.mir, self.infcx.tcx).ty
6871
));
6972

70-
item_msg = format!("`{}`", access_place_desc.unwrap());
7173
if self.is_upvar_field_projection(access_place).is_some() {
7274
reason = ", as it is not declared as mutable".to_string();
7375
} else {
@@ -82,7 +84,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
8284
}) => {
8385
if *base == Place::Base(PlaceBase::Local(Local::new(1))) &&
8486
!self.upvars.is_empty() {
85-
item_msg = format!("`{}`", access_place_desc.unwrap());
8687
debug_assert!(self.mir.local_decls[Local::new(1)].ty.is_region_ptr());
8788
debug_assert!(is_closure_or_generator(
8889
the_place_err.ty(self.mir, self.infcx.tcx).ty
@@ -105,7 +106,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
105106
false
106107
}
107108
} {
108-
item_msg = format!("`{}`", access_place_desc.unwrap());
109109
reason = ", as it is immutable for the pattern guard".to_string();
110110
} else {
111111
let pointer_type =
@@ -114,8 +114,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
114114
} else {
115115
"`*const` pointer"
116116
};
117-
if let Some(desc) = access_place_desc {
118-
item_msg = format!("`{}`", desc);
117+
if access_place_desc.is_some() {
119118
reason = match error_access {
120119
AccessKind::Move |
121120
AccessKind::Mutate => format!(" which is behind a {}", pointer_type),
@@ -135,10 +134,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
135134

136135
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })) => {
137136
if let Place::Base(PlaceBase::Static(_)) = access_place {
138-
item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
137+
item_msg = format!("immutable static item {}", item_msg);
139138
reason = String::new();
140139
} else {
141-
item_msg = format!("`{}`", access_place_desc.unwrap());
142140
let static_name = &self.infcx.tcx.item_name(*def_id);
143141
reason = format!(", as `{}` is an immutable static item", static_name);
144142
}

src/test/ui/issues/issue-61187.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
#![feature(async_await)]
3+
4+
fn main() {
5+
}
6+
7+
async fn response(data: Vec<u8>) {
8+
data.reverse(); //~ ERROR E0596
9+
}

src/test/ui/issues/issue-61187.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0596]: cannot borrow temporary place as mutable, as it is not declared as mutable
2+
--> $DIR/issue-61187.rs:8:5
3+
|
4+
LL | async fn response(data: Vec<u8>) {
5+
| ---- help: consider changing this to be mutable: `mut data`
6+
LL | data.reverse();
7+
| ^^^^ cannot borrow as mutable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)