diff --git a/crates/hir-ty/src/mir/lower/as_place.rs b/crates/hir-ty/src/mir/lower/as_place.rs index 86fee99b78..91163e5bd6 100644 --- a/crates/hir-ty/src/mir/lower/as_place.rs +++ b/crates/hir-ty/src/mir/lower/as_place.rs @@ -216,8 +216,15 @@ impl MirLowerCtx<'_> { index_fn, ); } + let adjusts = self + .infer + .expr_adjustments + .get(base) + .and_then(|x| x.split_last()) + .map(|x| x.1) + .unwrap_or(&[]); let Some((mut p_base, current)) = - self.lower_expr_as_place_without_adjust(current, *base, true)? + self.lower_expr_as_place_with_adjust(current, *base, true, adjusts)? else { return Ok(None); }; diff --git a/crates/ide-diagnostics/src/handlers/mutability_errors.rs b/crates/ide-diagnostics/src/handlers/mutability_errors.rs index ecd1db7ea8..564b756402 100644 --- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs +++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs @@ -564,6 +564,25 @@ fn f(x: [(i32, u8); 10]) { ); } + #[test] + fn index() { + check_diagnostics( + r#" +//- minicore: coerce_unsized, index, slice +fn f() { + let x = [1, 2, 3]; + x[2] = 5; + //^^^^^^^^ 💡 error: cannot mutate immutable variable `x` + let x = &mut x; + //^^^^^^ 💡 error: cannot mutate immutable variable `x` + let mut x = x; + //^^^^^ 💡 weak: variable does not need to be mutable + x[2] = 5; +} +"#, + ); + } + #[test] fn overloaded_index() { check_diagnostics(