From ea37000b565021086c560f3fd881508bfed6f531 Mon Sep 17 00:00:00 2001 From: dianne Date: Wed, 23 Oct 2024 10:31:58 -0700 Subject: [PATCH] Use a common subdiagnostic format for generic borrows This is setup for unifing the logic for suggestions to borrow arguments in generic positions. As of this commit, it's still special cases for `AsRef`/`Borrow`-like traits and `Fn`-like traits. --- .../src/diagnostics/conflict_errors.rs | 15 ++++++++++----- tests/ui/moves/moved-value-on-as-ref-arg.stderr | 8 ++++---- .../ui/moves/suggest-borrow-for-generic-arg.fixed | 8 ++++---- tests/ui/moves/suggest-borrow-for-generic-arg.rs | 8 ++++---- .../moves/suggest-borrow-for-generic-arg.stderr | 8 ++++---- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index ede1e815f4d..5ffd635f1cc 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -478,7 +478,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { return; } - let mut is_mut = false; + let mut mutbl = ty::Mutability::Not; if let Some(param) = arg_param && self .infcx @@ -504,7 +504,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { ] .contains(&Some(predicate.def_id())) { - is_mut = true; + mutbl = ty::Mutability::Mut; } true } else { @@ -514,13 +514,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { { // The type of the argument corresponding to the expression that got moved // is a type parameter `T`, which is has a `T: AsRef` obligation. + let place_desc = if let Some(desc) = self.describe_place(moved_place) { + format!("`{desc}`") + } else { + "here".to_owned() + }; err.span_suggestion_verbose( expr.span.shrink_to_lo(), - "borrow the value to avoid moving it", - format!("&{}", if is_mut { "mut " } else { "" }), + format!("consider {}borrowing {place_desc}", mutbl.mutably_str()), + mutbl.ref_prefix_str(), Applicability::MaybeIncorrect, ); - can_suggest_clone = is_mut; + can_suggest_clone = mutbl.is_mut(); } else if let Some(local_def_id) = def_id.as_local() && let node = self.infcx.tcx.hir_node_by_def_id(local_def_id) && let Some(fn_decl) = node.fn_decl() diff --git a/tests/ui/moves/moved-value-on-as-ref-arg.stderr b/tests/ui/moves/moved-value-on-as-ref-arg.stderr index 4004b7a43bc..a99bdb4fe9d 100644 --- a/tests/ui/moves/moved-value-on-as-ref-arg.stderr +++ b/tests/ui/moves/moved-value-on-as-ref-arg.stderr @@ -8,7 +8,7 @@ LL | foo(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider borrowing `bar` | LL | foo(&bar); | + @@ -31,7 +31,7 @@ LL | struct Bar; ... LL | qux(bar); | --- you could clone this value -help: borrow the value to avoid moving it +help: consider mutably borrowing `bar` | LL | qux(&mut bar); | ++++ @@ -46,7 +46,7 @@ LL | bat(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider borrowing `bar` | LL | bat(&bar); | + @@ -69,7 +69,7 @@ LL | struct Bar; ... LL | baz(bar); | --- you could clone this value -help: borrow the value to avoid moving it +help: consider mutably borrowing `bar` | LL | baz(&mut bar); | ++++ diff --git a/tests/ui/moves/suggest-borrow-for-generic-arg.fixed b/tests/ui/moves/suggest-borrow-for-generic-arg.fixed index 3f711924d1e..372a469dcd6 100644 --- a/tests/ui/moves/suggest-borrow-for-generic-arg.fixed +++ b/tests/ui/moves/suggest-borrow-for-generic-arg.fixed @@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux; pub fn main() { let bar = aux::Bar; - aux::foo(&bar); //~ HELP borrow the value + aux::foo(&bar); //~ HELP consider borrowing `bar` let _baa = bar; //~ ERROR use of moved value let mut bar = aux::Bar; - aux::qux(&mut bar); //~ HELP borrow the value + aux::qux(&mut bar); //~ HELP consider mutably borrowing `bar` let _baa = bar; //~ ERROR use of moved value let bar = aux::Bar; - aux::bat(&bar); //~ HELP borrow the value + aux::bat(&bar); //~ HELP consider borrowing `bar` let _baa = bar; //~ ERROR use of moved value let mut bar = aux::Bar; - aux::baz(&mut bar); //~ HELP borrow the value + aux::baz(&mut bar); //~ HELP consider mutably borrowing `bar` let _baa = bar; //~ ERROR use of moved value } diff --git a/tests/ui/moves/suggest-borrow-for-generic-arg.rs b/tests/ui/moves/suggest-borrow-for-generic-arg.rs index eb6abcdf0d5..c8b421fc03c 100644 --- a/tests/ui/moves/suggest-borrow-for-generic-arg.rs +++ b/tests/ui/moves/suggest-borrow-for-generic-arg.rs @@ -8,15 +8,15 @@ extern crate suggest_borrow_for_generic_arg_aux as aux; pub fn main() { let bar = aux::Bar; - aux::foo(bar); //~ HELP borrow the value + aux::foo(bar); //~ HELP consider borrowing `bar` let _baa = bar; //~ ERROR use of moved value let mut bar = aux::Bar; - aux::qux(bar); //~ HELP borrow the value + aux::qux(bar); //~ HELP consider mutably borrowing `bar` let _baa = bar; //~ ERROR use of moved value let bar = aux::Bar; - aux::bat(bar); //~ HELP borrow the value + aux::bat(bar); //~ HELP consider borrowing `bar` let _baa = bar; //~ ERROR use of moved value let mut bar = aux::Bar; - aux::baz(bar); //~ HELP borrow the value + aux::baz(bar); //~ HELP consider mutably borrowing `bar` let _baa = bar; //~ ERROR use of moved value } diff --git a/tests/ui/moves/suggest-borrow-for-generic-arg.stderr b/tests/ui/moves/suggest-borrow-for-generic-arg.stderr index 79602c4af4a..1a5106e6cd9 100644 --- a/tests/ui/moves/suggest-borrow-for-generic-arg.stderr +++ b/tests/ui/moves/suggest-borrow-for-generic-arg.stderr @@ -8,7 +8,7 @@ LL | aux::foo(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider borrowing `bar` | LL | aux::foo(&bar); | + @@ -23,7 +23,7 @@ LL | aux::qux(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider mutably borrowing `bar` | LL | aux::qux(&mut bar); | ++++ @@ -38,7 +38,7 @@ LL | aux::bat(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider borrowing `bar` | LL | aux::bat(&bar); | + @@ -53,7 +53,7 @@ LL | aux::baz(bar); LL | let _baa = bar; | ^^^ value used here after move | -help: borrow the value to avoid moving it +help: consider mutably borrowing `bar` | LL | aux::baz(&mut bar); | ++++