Skip to content

Commit 13c6066

Browse files
authored
Rollup merge of #91212 - compiler-errors:issue91206, r=oli-obk
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 #91206 Cc ``@camsteffen`` r? ``@oli-obk``
2 parents fdc305d + 69d1917 commit 13c6066

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -447,16 +447,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
447447
// check if the RHS is from desugaring
448448
let opt_assignment_rhs_span =
449449
self.body.find_assignments(local).first().map(|&location| {
450-
let stmt = &self.body[location.block].statements
451-
[location.statement_index];
452-
match stmt.kind {
453-
mir::StatementKind::Assign(box (
454-
_,
455-
mir::Rvalue::Use(mir::Operand::Copy(place)),
456-
)) => {
457-
self.body.local_decls[place.local].source_info.span
458-
}
459-
_ => self.body.source_info(location).span,
450+
if let Some(mir::Statement {
451+
source_info: _,
452+
kind:
453+
mir::StatementKind::Assign(box (
454+
_,
455+
mir::Rvalue::Use(mir::Operand::Copy(place)),
456+
)),
457+
}) = self.body[location.block]
458+
.statements
459+
.get(location.statement_index)
460+
{
461+
self.body.local_decls[place.local].source_info.span
462+
} else {
463+
self.body.source_info(location).span
460464
}
461465
});
462466
match opt_assignment_rhs_span.and_then(|s| s.desugaring_kind()) {

src/test/ui/borrowck/issue-91206.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct TestClient;
2+
3+
impl TestClient {
4+
fn get_inner_ref(&self) -> &Vec<usize> {
5+
todo!()
6+
}
7+
}
8+
9+
fn main() {
10+
let client = TestClient;
11+
let inner = client.get_inner_ref();
12+
//~^ HELP consider changing this to be a mutable reference
13+
inner.clear();
14+
//~^ ERROR cannot borrow `*inner` as mutable, as it is behind a `&` reference [E0596]
15+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow `*inner` as mutable, as it is behind a `&` reference
2+
--> $DIR/issue-91206.rs:13:5
3+
|
4+
LL | let inner = client.get_inner_ref();
5+
| ----- help: consider changing this to be a mutable reference: `&mut Vec<usize>`
6+
LL |
7+
LL | inner.clear();
8+
| ^^^^^^^^^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)