Skip to content

Commit

Permalink
Rollup merge of rust-lang#91212 - compiler-errors:issue91206, r=oli-obk
Browse files Browse the repository at this point in the history
Fix ICE due to out-of-bounds statement index when reporting borrowck error

Replace an `[index]` with a `.get` when `statement_index` points to a basic-block terminator (and is therefore out-of-bounds in the statements list).

Fixes rust-lang#91206
Cc `@camsteffen`
r? `@oli-obk`
  • Loading branch information
matthiaskrgr authored Nov 26, 2021
2 parents 82e24e9 + 69d1917 commit 7690946
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
24 changes: 14 additions & 10 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/borrowck/issue-91206.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct TestClient;

impl TestClient {
fn get_inner_ref(&self) -> &Vec<usize> {
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]
}
12 changes: 12 additions & 0 deletions src/test/ui/borrowck/issue-91206.stderr
Original file line number Diff line number Diff line change
@@ -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<usize>`
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`.

0 comments on commit 7690946

Please sign in to comment.