Skip to content

Commit 1ce25f1

Browse files
committed
Auto merge of rust-lang#14464 - HKalbasi:dev, r=HKalbasi
lower adjusts in simple index except the last one fix rust-lang#14463
2 parents ffb04ae + d7fe4e2 commit 1ce25f1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

crates/hir-ty/src/mir/lower/as_place.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,15 @@ impl MirLowerCtx<'_> {
216216
index_fn,
217217
);
218218
}
219+
let adjusts = self
220+
.infer
221+
.expr_adjustments
222+
.get(base)
223+
.and_then(|x| x.split_last())
224+
.map(|x| x.1)
225+
.unwrap_or(&[]);
219226
let Some((mut p_base, current)) =
220-
self.lower_expr_as_place_without_adjust(current, *base, true)?
227+
self.lower_expr_as_place_with_adjust(current, *base, true, adjusts)?
221228
else {
222229
return Ok(None);
223230
};

crates/ide-diagnostics/src/handlers/mutability_errors.rs

+19
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,25 @@ fn f(x: [(i32, u8); 10]) {
564564
);
565565
}
566566

567+
#[test]
568+
fn index() {
569+
check_diagnostics(
570+
r#"
571+
//- minicore: coerce_unsized, index, slice
572+
fn f() {
573+
let x = [1, 2, 3];
574+
x[2] = 5;
575+
//^^^^^^^^ 💡 error: cannot mutate immutable variable `x`
576+
let x = &mut x;
577+
//^^^^^^ 💡 error: cannot mutate immutable variable `x`
578+
let mut x = x;
579+
//^^^^^ 💡 weak: variable does not need to be mutable
580+
x[2] = 5;
581+
}
582+
"#,
583+
);
584+
}
585+
567586
#[test]
568587
fn overloaded_index() {
569588
check_diagnostics(

0 commit comments

Comments
 (0)