From 718a3b1f2d59943c90cd02b9d0b90b315e003ec0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 25 Nov 2021 06:48:09 +0000 Subject: [PATCH 1/2] Fix issue 91206 --- .../src/diagnostics/mutability_errors.rs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 46e2a99a0d094..a0f8aabbe0e72 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -447,16 +447,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { // check if the RHS is from desugaring let opt_assignment_rhs_span = self.body.find_assignments(local).first().map(|&location| { - let stmt = &self.body[location.block].statements - [location.statement_index]; - match stmt.kind { - mir::StatementKind::Assign(box ( - _, - mir::Rvalue::Use(mir::Operand::Copy(place)), - )) => { - self.body.local_decls[place.local].source_info.span - } - _ => self.body.source_info(location).span, + if let Some(mir::Statement { + source_info: _, + kind: + mir::StatementKind::Assign(box ( + _, + mir::Rvalue::Use(mir::Operand::Copy(place)), + )), + }) = self.body[location.block] + .statements + .get(location.statement_index) + { + self.body.local_decls[place.local].source_info.span + } else { + self.body.source_info(location).span } }); match opt_assignment_rhs_span.and_then(|s| s.desugaring_kind()) { From 69d1917672d302e2da14ad6673e6a0478c62d1ad Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 25 Nov 2021 16:33:01 +0000 Subject: [PATCH 2/2] Add test demonstrating no more ICE --- src/test/ui/borrowck/issue-91206.rs | 15 +++++++++++++++ src/test/ui/borrowck/issue-91206.stderr | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/test/ui/borrowck/issue-91206.rs create mode 100644 src/test/ui/borrowck/issue-91206.stderr diff --git a/src/test/ui/borrowck/issue-91206.rs b/src/test/ui/borrowck/issue-91206.rs new file mode 100644 index 0000000000000..3b1fbf4b69902 --- /dev/null +++ b/src/test/ui/borrowck/issue-91206.rs @@ -0,0 +1,15 @@ +struct TestClient; + +impl TestClient { + fn get_inner_ref(&self) -> &Vec { + todo!() + } +} + +fn main() { + let client = TestClient; + let inner = client.get_inner_ref(); + //~^ HELP consider changing this to be a mutable reference + inner.clear(); + //~^ ERROR cannot borrow `*inner` as mutable, as it is behind a `&` reference [E0596] +} diff --git a/src/test/ui/borrowck/issue-91206.stderr b/src/test/ui/borrowck/issue-91206.stderr new file mode 100644 index 0000000000000..535d247452a59 --- /dev/null +++ b/src/test/ui/borrowck/issue-91206.stderr @@ -0,0 +1,12 @@ +error[E0596]: cannot borrow `*inner` as mutable, as it is behind a `&` reference + --> $DIR/issue-91206.rs:13:5 + | +LL | let inner = client.get_inner_ref(); + | ----- help: consider changing this to be a mutable reference: `&mut Vec` +LL | +LL | inner.clear(); + | ^^^^^^^^^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`.