about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhkalbasi <hamidrezakalbasi@protonmail.com>2023-04-01 16:49:32 +0330
committerhkalbasi <hamidrezakalbasi@protonmail.com>2023-04-01 16:49:32 +0330
commitd7fe4e2fa88a82b3692536f8a9bb433752a1a642 (patch)
tree20dc050039152bd937e3f268a9e825ccb59a0f48
parent8a6ca86247c227ba27fb38470c1ca7ddc9ceb511 (diff)
downloadrust-d7fe4e2fa88a82b3692536f8a9bb433752a1a642.tar.gz
rust-d7fe4e2fa88a82b3692536f8a9bb433752a1a642.zip
lower adjusts in simple index except the last one
-rw-r--r--crates/hir-ty/src/mir/lower/as_place.rs9
-rw-r--r--crates/ide-diagnostics/src/handlers/mutability_errors.rs19
2 files changed, 27 insertions, 1 deletions
diff --git a/crates/hir-ty/src/mir/lower/as_place.rs b/crates/hir-ty/src/mir/lower/as_place.rs
index 86fee99b787..91163e5bd61 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 ecd1db7ea87..564b756402d 100644
--- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs
+++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs
@@ -565,6 +565,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(
             r#"