From 065454dd1da7a759cd414468992b2afdfcf477b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 13 Mar 2024 05:26:03 +0000 Subject: [PATCH] More move error suggestions to clone ``` error[E0507]: cannot move out of `val`, a captured variable in an `FnMut` closure --> $DIR/issue-87456-point-to-closure.rs:10:28 | LL | let val = String::new(); | --- captured outer variable LL | LL | take_mut(|| { | -- captured by this `FnMut` closure LL | LL | let _foo: String = val; | ^^^ move occurs because `val` has type `String`, which does not implement the `Copy` trait | help: consider borrowing here | LL | let _foo: String = &val; | + help: consider cloning the value if the performance cost is acceptable | LL | let _foo: String = val.clone(); | ++++++++ ``` --- .../src/diagnostics/move_errors.rs | 4 ++ tests/ui/borrowck/borrowck-issue-2657-2.fixed | 2 +- .../ui/borrowck/borrowck-issue-2657-2.stderr | 5 +++ .../borrowck-move-from-unsafe-ptr.stderr | 5 +++ ...rrowck-move-out-of-overloaded-deref.stderr | 5 +++ ...owck-overloaded-index-move-from-vec.stderr | 4 ++ .../borrowck/borrowck-vec-pattern-nesting.rs | 3 ++ .../borrowck-vec-pattern-nesting.stderr | 20 +++++++-- .../issue-87456-point-to-closure.stderr | 4 ++ .../ui/check-static-values-constraints.stderr | 4 ++ .../issue-40402-1.stderr | 4 ++ tests/ui/nll/cannot-move-block-spans.stderr | 42 +++++++++++++++++++ 12 files changed, 97 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index 3a1fbab452e..17e43a5b39c 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -444,6 +444,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { None => "value".to_string(), }; + if let Some(expr) = self.find_expr(span) { + self.suggest_cloning(err, place_ty, expr, span); + } + err.subdiagnostic( self.dcx(), crate::session_diagnostics::TypeNoCopy::Label { diff --git a/tests/ui/borrowck/borrowck-issue-2657-2.fixed b/tests/ui/borrowck/borrowck-issue-2657-2.fixed index e5aaf7d2de7..e532aa3e68c 100644 --- a/tests/ui/borrowck/borrowck-issue-2657-2.fixed +++ b/tests/ui/borrowck/borrowck-issue-2657-2.fixed @@ -5,7 +5,7 @@ fn main() { match x { Some(ref y) => { - let _b = y; //~ ERROR cannot move out + let _b = y.clone(); //~ ERROR cannot move out } _ => {} } diff --git a/tests/ui/borrowck/borrowck-issue-2657-2.stderr b/tests/ui/borrowck/borrowck-issue-2657-2.stderr index 6fab19000fc..16186792b93 100644 --- a/tests/ui/borrowck/borrowck-issue-2657-2.stderr +++ b/tests/ui/borrowck/borrowck-issue-2657-2.stderr @@ -9,6 +9,11 @@ help: consider removing the dereference here LL - let _b = *y; LL + let _b = y; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let _b = *y; +LL + let _b = y.clone(); + | error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr b/tests/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr index 7213f85ad98..ebc3b6ebcac 100644 --- a/tests/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr +++ b/tests/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr @@ -9,6 +9,11 @@ help: consider removing the dereference here LL - let y = *x; LL + let y = x; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let y = *x; +LL + let y = x.clone(); + | error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrowck-move-out-of-overloaded-deref.stderr b/tests/ui/borrowck/borrowck-move-out-of-overloaded-deref.stderr index dce1f4d0775..774aac3ec9c 100644 --- a/tests/ui/borrowck/borrowck-move-out-of-overloaded-deref.stderr +++ b/tests/ui/borrowck/borrowck-move-out-of-overloaded-deref.stderr @@ -9,6 +9,11 @@ help: consider removing the dereference here LL - let _x = *Rc::new("hi".to_string()); LL + let _x = Rc::new("hi".to_string()); | +help: consider cloning the value if the performance cost is acceptable + | +LL - let _x = *Rc::new("hi".to_string()); +LL + let _x = Rc::new("hi".to_string()).clone(); + | error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrowck-overloaded-index-move-from-vec.stderr b/tests/ui/borrowck/borrowck-overloaded-index-move-from-vec.stderr index b4106702cd1..3e874ed1a2f 100644 --- a/tests/ui/borrowck/borrowck-overloaded-index-move-from-vec.stderr +++ b/tests/ui/borrowck/borrowck-overloaded-index-move-from-vec.stderr @@ -8,6 +8,10 @@ help: consider borrowing here | LL | let bad = &v[0]; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let bad = v[0].clone(); + | ++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/borrowck-vec-pattern-nesting.rs b/tests/ui/borrowck/borrowck-vec-pattern-nesting.rs index 1bda7a49713..ec074d2cf1c 100644 --- a/tests/ui/borrowck/borrowck-vec-pattern-nesting.rs +++ b/tests/ui/borrowck/borrowck-vec-pattern-nesting.rs @@ -47,6 +47,7 @@ fn c() { //~| NOTE cannot move out of here //~| NOTE move occurs because //~| HELP consider borrowing here + //~| HELP consider cloning } fn d() { @@ -66,6 +67,7 @@ fn d() { //~| NOTE cannot move out of here //~| NOTE move occurs because //~| HELP consider borrowing here + //~| HELP consider cloning } fn e() { @@ -86,6 +88,7 @@ fn e() { //~| NOTE cannot move out of here //~| NOTE move occurs because //~| HELP consider borrowing here + //~| HELP consider cloning } fn main() {} diff --git a/tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr b/tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr index 024cb006c26..fff997fd555 100644 --- a/tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr +++ b/tests/ui/borrowck/borrowck-vec-pattern-nesting.stderr @@ -53,9 +53,13 @@ help: consider borrowing here | LL | let a = &vec[0]; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let a = vec[0].clone(); + | ++++++++ error[E0508]: cannot move out of type `[Box]`, a non-copy slice - --> $DIR/borrowck-vec-pattern-nesting.rs:55:11 + --> $DIR/borrowck-vec-pattern-nesting.rs:56:11 | LL | match vec { | ^^^ cannot move out of here @@ -73,7 +77,7 @@ LL + [ | error[E0508]: cannot move out of type `[Box]`, a non-copy slice - --> $DIR/borrowck-vec-pattern-nesting.rs:65:13 + --> $DIR/borrowck-vec-pattern-nesting.rs:66:13 | LL | let a = vec[0]; | ^^^^^^ @@ -85,9 +89,13 @@ help: consider borrowing here | LL | let a = &vec[0]; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let a = vec[0].clone(); + | ++++++++ error[E0508]: cannot move out of type `[Box]`, a non-copy slice - --> $DIR/borrowck-vec-pattern-nesting.rs:74:11 + --> $DIR/borrowck-vec-pattern-nesting.rs:76:11 | LL | match vec { | ^^^ cannot move out of here @@ -106,7 +114,7 @@ LL + [_a, _b, _c] => {} | error[E0508]: cannot move out of type `[Box]`, a non-copy slice - --> $DIR/borrowck-vec-pattern-nesting.rs:85:13 + --> $DIR/borrowck-vec-pattern-nesting.rs:87:13 | LL | let a = vec[0]; | ^^^^^^ @@ -118,6 +126,10 @@ help: consider borrowing here | LL | let a = &vec[0]; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let a = vec[0].clone(); + | ++++++++ error: aborting due to 8 previous errors diff --git a/tests/ui/borrowck/issue-87456-point-to-closure.stderr b/tests/ui/borrowck/issue-87456-point-to-closure.stderr index a15909df07b..a0c7cac2add 100644 --- a/tests/ui/borrowck/issue-87456-point-to-closure.stderr +++ b/tests/ui/borrowck/issue-87456-point-to-closure.stderr @@ -14,6 +14,10 @@ help: consider borrowing here | LL | let _foo: String = &val; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let _foo: String = val.clone(); + | ++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/check-static-values-constraints.stderr b/tests/ui/check-static-values-constraints.stderr index dee1f2b1210..fe5f2a34272 100644 --- a/tests/ui/check-static-values-constraints.stderr +++ b/tests/ui/check-static-values-constraints.stderr @@ -160,6 +160,10 @@ help: consider borrowing here | LL | &x | + +help: consider cloning the value if the performance cost is acceptable + | +LL | x.clone() + | ++++++++ error: aborting due to 17 previous errors diff --git a/tests/ui/issues/issue-40402-ref-hints/issue-40402-1.stderr b/tests/ui/issues/issue-40402-ref-hints/issue-40402-1.stderr index 7976d090542..d27b6e6324f 100644 --- a/tests/ui/issues/issue-40402-ref-hints/issue-40402-1.stderr +++ b/tests/ui/issues/issue-40402-ref-hints/issue-40402-1.stderr @@ -8,6 +8,10 @@ help: consider borrowing here | LL | let e = &f.v[0]; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let e = f.v[0].clone(); + | ++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/nll/cannot-move-block-spans.stderr b/tests/ui/nll/cannot-move-block-spans.stderr index 0dc5c08ea5f..d96773e1edf 100644 --- a/tests/ui/nll/cannot-move-block-spans.stderr +++ b/tests/ui/nll/cannot-move-block-spans.stderr @@ -9,6 +9,11 @@ help: consider removing the dereference here LL - let x = { *r }; LL + let x = { r }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let x = { *r }; +LL + let x = { r.clone() }; + | error[E0507]: cannot move out of `*r` which is behind a shared reference --> $DIR/cannot-move-block-spans.rs:6:22 @@ -21,6 +26,11 @@ help: consider removing the dereference here LL - let y = unsafe { *r }; LL + let y = unsafe { r }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let y = unsafe { *r }; +LL + let y = unsafe { r.clone() }; + | error[E0507]: cannot move out of `*r` which is behind a shared reference --> $DIR/cannot-move-block-spans.rs:7:26 @@ -33,6 +43,11 @@ help: consider removing the dereference here LL - let z = loop { break *r; }; LL + let z = loop { break r; }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let z = loop { break *r; }; +LL + let z = loop { break r.clone(); }; + | error[E0508]: cannot move out of type `[String; 2]`, a non-copy array --> $DIR/cannot-move-block-spans.rs:11:15 @@ -47,6 +62,10 @@ help: consider borrowing here | LL | let x = { &arr[0] }; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let x = { arr[0].clone() }; + | ++++++++ error[E0508]: cannot move out of type `[String; 2]`, a non-copy array --> $DIR/cannot-move-block-spans.rs:12:22 @@ -61,6 +80,10 @@ help: consider borrowing here | LL | let y = unsafe { &arr[0] }; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let y = unsafe { arr[0].clone() }; + | ++++++++ error[E0508]: cannot move out of type `[String; 2]`, a non-copy array --> $DIR/cannot-move-block-spans.rs:13:26 @@ -75,6 +98,10 @@ help: consider borrowing here | LL | let z = loop { break &arr[0]; }; | + +help: consider cloning the value if the performance cost is acceptable + | +LL | let z = loop { break arr[0].clone(); }; + | ++++++++ error[E0507]: cannot move out of `*r` which is behind a shared reference --> $DIR/cannot-move-block-spans.rs:17:38 @@ -87,6 +114,11 @@ help: consider removing the dereference here LL - let x = { let mut u = 0; u += 1; *r }; LL + let x = { let mut u = 0; u += 1; r }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let x = { let mut u = 0; u += 1; *r }; +LL + let x = { let mut u = 0; u += 1; r.clone() }; + | error[E0507]: cannot move out of `*r` which is behind a shared reference --> $DIR/cannot-move-block-spans.rs:18:45 @@ -99,6 +131,11 @@ help: consider removing the dereference here LL - let y = unsafe { let mut u = 0; u += 1; *r }; LL + let y = unsafe { let mut u = 0; u += 1; r }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let y = unsafe { let mut u = 0; u += 1; *r }; +LL + let y = unsafe { let mut u = 0; u += 1; r.clone() }; + | error[E0507]: cannot move out of `*r` which is behind a shared reference --> $DIR/cannot-move-block-spans.rs:19:49 @@ -111,6 +148,11 @@ help: consider removing the dereference here LL - let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; LL + let z = loop { let mut u = 0; u += 1; break r; u += 2; }; | +help: consider cloning the value if the performance cost is acceptable + | +LL - let z = loop { let mut u = 0; u += 1; break *r; u += 2; }; +LL + let z = loop { let mut u = 0; u += 1; break r.clone(); u += 2; }; + | error: aborting due to 9 previous errors