]> git.lizzy.rs Git - rust.git/commitdiff
Provide structured suggestion for binding needing type on E0594
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 3 Feb 2023 18:53:27 +0000 (18:53 +0000)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 3 Feb 2023 18:53:27 +0000 (18:53 +0000)
Partially address #45405.

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
tests/ui/borrowck/borrowck-borrow-mut-base-ptr-in-aliasable-loc.stderr
tests/ui/borrowck/issue-85765.rs
tests/ui/borrowck/issue-85765.stderr
tests/ui/borrowck/issue-91206.rs
tests/ui/borrowck/issue-91206.stderr
tests/ui/borrowck/issue-92015.stderr
tests/ui/issues/issue-51515.rs
tests/ui/issues/issue-51515.stderr

index 40f518b33cfb66dbcf4b485a2d32ab3a71b2aa1a..cf3240226fb3fee7903032e6c6ddda2b7ce4efdd 100644 (file)
@@ -606,12 +606,60 @@ pub(crate) fn report_mutability_error(
                                 }
                             }
                             Some((false, err_label_span, message)) => {
-                                err.span_label(
-                                    err_label_span,
-                                    &format!(
-                                        "consider changing this binding's type to be: `{message}`"
-                                    ),
-                                );
+                                struct V {
+                                    span: Span,
+                                    hir_id: Option<hir::HirId>,
+                                }
+
+                                impl<'tcx> Visitor<'tcx> for V {
+                                    fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
+                                        if let hir::StmtKind::Local(local) = s.kind {
+                                            if local.pat.span == self.span {
+                                                self.hir_id = Some(local.hir_id);
+                                            }
+                                        }
+                                        hir::intravisit::walk_stmt(self, s);
+                                    }
+                                }
+                                let hir_map = self.infcx.tcx.hir();
+                                let pat = loop {
+                                    // Poor man's try block
+                                    let def_id = self.body.source.def_id();
+                                    let hir_id =
+                                        hir_map.local_def_id_to_hir_id(def_id.as_local().unwrap());
+                                    let node = hir_map.find(hir_id);
+                                    let Some(hir::Node::Item(item)) = node else { break None; };
+                                    let hir::ItemKind::Fn(.., body_id) = item.kind else { break None; };
+                                    let body = self.infcx.tcx.hir().body(body_id);
+                                    let mut v = V { span: err_label_span, hir_id: None };
+                                    v.visit_body(body);
+                                    break v.hir_id;
+                                };
+                                if let Some(hir_id) = pat
+                                    && let Some(hir::Node::Local(local)) = hir_map.find(hir_id)
+                                {
+                                    let (changing, span, sugg) = match local.ty {
+                                        Some(ty) => ("changing", ty.span, message),
+                                        None => (
+                                            "specifying",
+                                            local.pat.span.shrink_to_hi(),
+                                            format!(": {message}"),
+                                        ),
+                                    };
+                                    err.span_suggestion_verbose(
+                                        span,
+                                        &format!("consider {changing} this binding's type"),
+                                        sugg,
+                                        Applicability::HasPlaceholders,
+                                    );
+                                } else {
+                                    err.span_label(
+                                        err_label_span,
+                                        &format!(
+                                            "consider changing this binding's type to be: `{message}`"
+                                        ),
+                                    );
+                                }
                             }
                             None => {}
                         }
index ce9f7aa050a0aca647a80a16cfc7471afa4784f4..dd0817ff2331368ac12daa488cafed47f33477ad 100644 (file)
@@ -1,11 +1,13 @@
 error[E0594]: cannot assign to `**t1`, which is behind a `&` reference
   --> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:9:5
    |
-LL |     let t1 = t0;
-   |         -- consider changing this binding's type to be: `&mut &mut isize`
-LL |     let p: &isize = &**t0;
 LL |     **t1 = 22;
    |     ^^^^^^^^^ `t1` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider specifying this binding's type
+   |
+LL |     let t1: &mut &mut isize = t0;
+   |           +++++++++++++++++
 
 error[E0502]: cannot borrow `**t0` as immutable because it is also borrowed as mutable
   --> $DIR/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs:14:21
index 1598cd5d3c86fa98c005b47f2f725775a809d0f6..76e0b517354168c32ec4b4462bf417e948c9c6f9 100644 (file)
@@ -1,7 +1,7 @@
 fn main() {
     let mut test = Vec::new();
     let rofl: &Vec<Vec<i32>> = &mut test;
-    //~^ NOTE consider changing this binding's type to be
+    //~^ HELP consider changing this binding's type
     rofl.push(Vec::new());
     //~^ ERROR cannot borrow `*rofl` as mutable, as it is behind a `&` reference
     //~| NOTE `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
@@ -15,14 +15,14 @@ fn main() {
 
     #[rustfmt::skip]
     let x: &usize = &mut{0};
-    //~^ NOTE consider changing this binding's type to be
+    //~^ HELP consider changing this binding's type
     *x = 1;
     //~^ ERROR cannot assign to `*x`, which is behind a `&` reference
     //~| NOTE `x` is a `&` reference, so the data it refers to cannot be written
 
     #[rustfmt::skip]
     let y: &usize = &mut(0);
-    //~^ NOTE consider changing this binding's type to be
+    //~^ HELP consider changing this binding's type
     *y = 1;
     //~^ ERROR cannot assign to `*y`, which is behind a `&` reference
     //~| NOTE `y` is a `&` reference, so the data it refers to cannot be written
index 7da7dba68ab7aaf537a8a24ff100797e264f3f5c..b4bb128cbb420ffc881ecf1cec67f3ec5366f8b4 100644 (file)
@@ -1,11 +1,13 @@
 error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
   --> $DIR/issue-85765.rs:5:5
    |
-LL |     let rofl: &Vec<Vec<i32>> = &mut test;
-   |         ---- consider changing this binding's type to be: `&mut Vec<Vec<i32>>`
-LL |
 LL |     rofl.push(Vec::new());
    |     ^^^^^^^^^^^^^^^^^^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable
+   |
+help: consider changing this binding's type
+   |
+LL |     let rofl: &mut Vec<Vec<i32>> = &mut test;
+   |               ~~~~~~~~~~~~~~~~~~
 
 error[E0594]: cannot assign to `*r`, which is behind a `&` reference
   --> $DIR/issue-85765.rs:12:5
@@ -21,20 +23,24 @@ LL |     let r = &mut mutvar;
 error[E0594]: cannot assign to `*x`, which is behind a `&` reference
   --> $DIR/issue-85765.rs:19:5
    |
-LL |     let x: &usize = &mut{0};
-   |         - consider changing this binding's type to be: `&mut usize`
-LL |
 LL |     *x = 1;
    |     ^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider changing this binding's type
+   |
+LL |     let x: &mut usize = &mut{0};
+   |            ~~~~~~~~~~
 
 error[E0594]: cannot assign to `*y`, which is behind a `&` reference
   --> $DIR/issue-85765.rs:26:5
    |
-LL |     let y: &usize = &mut(0);
-   |         - consider changing this binding's type to be: `&mut usize`
-LL |
 LL |     *y = 1;
    |     ^^^^^^ `y` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider changing this binding's type
+   |
+LL |     let y: &mut usize = &mut(0);
+   |            ~~~~~~~~~~
 
 error: aborting due to 4 previous errors
 
index 67407c1eae3cfe2bf2144e0e97609146d26de3b2..e062a253767ded6ab491ae70d47ed53eb8fd10e4 100644 (file)
@@ -9,7 +9,7 @@ fn get_inner_ref(&self) -> &Vec<usize> {
 fn main() {
     let client = TestClient;
     let inner = client.get_inner_ref();
-    //~^ NOTE consider changing this binding's type to be
+    //~^ HELP consider specifying this binding's type
     inner.clear();
     //~^ ERROR cannot borrow `*inner` as mutable, as it is behind a `&` reference [E0596]
     //~| NOTE `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable
index 12d8d27c5f0264906276f2753ff25ce4260e8d4e..6653d497873e79d058a20ac347d94d93d7520ea5 100644 (file)
@@ -1,11 +1,13 @@
 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();
-   |         ----- consider changing this binding's type to be: `&mut Vec<usize>`
-LL |
 LL |     inner.clear();
    |     ^^^^^^^^^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable
+   |
+help: consider specifying this binding's type
+   |
+LL |     let inner: &mut Vec<usize> = client.get_inner_ref();
+   |              +++++++++++++++++
 
 error: aborting due to previous error
 
index 62b1183e71b4beb03ce8fa9de7fc83b69533cbd5..ea4f9abb87d9adeab8fafe633bc6a09591ee8197 100644 (file)
@@ -1,10 +1,13 @@
 error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
   --> $DIR/issue-92015.rs:6:5
    |
-LL |     let foo = Some(&0).unwrap();
-   |         --- consider changing this binding's type to be: `&mut i32`
 LL |     *foo = 1;
    |     ^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider specifying this binding's type
+   |
+LL |     let foo: &mut i32 = Some(&0).unwrap();
+   |            ++++++++++
 
 error: aborting due to previous error
 
index 797c1085d517b1b44b9159f6252a29399d92c328..84e09afac0a2dca67beff200753c983da299d3b7 100644 (file)
@@ -5,6 +5,7 @@ fn main() {
     *foo = 32;
     //~^ ERROR cannot assign to `*foo`, which is behind a `&` reference
     let bar = foo;
+    //~^ HELP consider specifying this binding's type
     *bar = 64;
     //~^ ERROR cannot assign to `*bar`, which is behind a `&` reference
 }
index c4e61e719539280b1bc96b9e87fae6ae4c180ffd..94e5c9f1b832a6e019e1c8273ba76f4c7c448c45 100644 (file)
@@ -10,12 +10,15 @@ LL |     let foo = &mut 16;
    |               ~~~~~~~
 
 error[E0594]: cannot assign to `*bar`, which is behind a `&` reference
-  --> $DIR/issue-51515.rs:8:5
+  --> $DIR/issue-51515.rs:9:5
    |
-LL |     let bar = foo;
-   |         --- consider changing this binding's type to be: `&mut i32`
 LL |     *bar = 64;
    |     ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider specifying this binding's type
+   |
+LL |     let bar: &mut i32 = foo;
+   |            ++++++++++
 
 error: aborting due to 2 previous errors